Remove dependency on pkg/errors

This commit is contained in:
Trevor Slocum 2020-01-27 16:18:44 -08:00
parent 7379d02282
commit b24275acaa
7 changed files with 46 additions and 25 deletions

View File

@ -1,3 +1,6 @@
0.1.4:
- Remove dependency on pkg/errors
0.1.3:
- Do not abort scan when encountering a file not found error

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2019 Trevor Slocum <trevor@rocketnine.space>
Copyright (c) 2020 Trevor Slocum <trevor@rocketnine.space>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -13,5 +13,5 @@ Documentation is available via [gdooc](https://godoc.org/gitlab.com/tslocum/desk
## Support
Please share issues/suggestions [here](https://gitlab.com/tslocum/desktop/issues).
Please share issues and suggestions [here](https://gitlab.com/tslocum/desktop/issues).

View File

@ -3,11 +3,11 @@ package desktop
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/pkg/errors"
)
// EntryType may be Application, Link or Directory.
@ -21,6 +21,8 @@ const (
Directory // Open file manager
)
const sectionHeaderNotFoundError = "section header not found"
func (t EntryType) String() string {
switch t {
case Unknown:
@ -119,12 +121,11 @@ func Parse(content io.Reader, buf []byte) (*Entry, error) {
scannedBytes []byte
scannedBytesLen int
entry = &Entry{}
entry Entry
foundHeader bool
)
scanner.Buffer(buf, len(buf))
for scanner.Scan() {
scannedBytes = bytes.TrimSpace(scanner.Bytes())
scannedBytesLen = len(scannedBytes)
@ -134,7 +135,7 @@ func Parse(content io.Reader, buf []byte) (*Entry, error) {
} else if scannedBytes[0] == byte('[') {
if !foundHeader {
if scannedBytesLen < 15 || !bytes.EqualFold(scannedBytes[0:15], entryHeader) {
return nil, errors.New("invalid desktop entry: section header not found")
return nil, errors.New(sectionHeaderNotFoundError)
}
foundHeader = true
@ -171,11 +172,14 @@ func Parse(content io.Reader, buf []byte) (*Entry, error) {
return nil, nil
}
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "failed to parse desktop entry")
} else if !foundHeader {
return nil, errors.Wrap(err, "invalid desktop entry")
err := scanner.Err()
if err == nil && !foundHeader {
err = errors.New(sectionHeaderNotFoundError)
}
if err != nil {
return nil, fmt.Errorf("failed to parse desktop entry: %s", err)
}
return entry, nil
return &entry, nil
}

2
go.mod
View File

@ -1,5 +1,3 @@
module gitlab.com/tslocum/desktop
go 1.12
require github.com/pkg/errors v0.8.1

2
go.sum
View File

@ -1,2 +0,0 @@
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

View File

@ -1,29 +1,47 @@
package desktop
import (
"fmt"
"os"
"path"
"testing"
)
func TestScan(t *testing.T) {
dirs := DataDirs()
_, err := Scan(dirs)
dirs, err := getTestScanDirs()
if err != nil {
t.Fatal(err)
t.Fatalf("failed to get test scan dirs: %s", err)
}
entries, err := Scan(dirs)
if err != nil {
t.Fatalf("failed to scan %s: %s", dirs[0], err)
}
_ = entries
}
func BenchmarkScan(b *testing.B) {
var (
dirs = DataDirs()
err error
)
dirs, err := getTestScanDirs()
if err != nil {
b.Fatalf("failed to get test scan dirs: %s", err)
}
var entries [][]*Entry
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = Scan(dirs)
entries, err = Scan(dirs)
if err != nil {
b.Fatal(err)
}
}
_ = entries
}
func getTestScanDirs() ([]string, error) {
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get current working directory: %s", err)
}
return []string{path.Join(wd, "test")}, nil
}