diff --git a/CHANGELOG b/CHANGELOG index 83068b5..db9b58d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/LICENSE b/LICENSE index ccdcdde..7c20f89 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Trevor Slocum +Copyright (c) 2020 Trevor Slocum Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index c9f821b..4797030 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/entry.go b/entry.go index 94a218f..61adbad 100644 --- a/entry.go +++ b/entry.go @@ -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 } diff --git a/go.mod b/go.mod index b7bd516..b9a5985 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module gitlab.com/tslocum/desktop go 1.12 - -require github.com/pkg/errors v0.8.1 diff --git a/go.sum b/go.sum deleted file mode 100644 index f29ab35..0000000 --- a/go.sum +++ /dev/null @@ -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= diff --git a/scan_test.go b/scan_test.go index da26fda..f4e5631 100644 --- a/scan_test.go +++ b/scan_test.go @@ -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 }