Skip to content

Commit 1019851

Browse files
zimbatmBrian McGee
authored and
Brian McGee
committed
chore: remove dependency on juju/errors (#11)
The package was created before Go introduced their own "errors" package. Trade the better juju errors semantic for a smaller dependency tree. Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/11 Co-authored-by: zimbatm <[email protected]> Co-committed-by: zimbatm <[email protected]>
1 parent 60a1d78 commit 1019851

File tree

6 files changed

+22
-28
lines changed

6 files changed

+22
-28
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/otiai10/copy v1.14.0
1313
github.com/stretchr/testify v1.8.4
1414
github.com/vmihailenco/msgpack/v5 v5.4.1
15-
github.com/ztrue/shutdown v0.1.1
1615
go.etcd.io/bbolt v1.3.8
1716
golang.org/x/sync v0.5.0
1817
)

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
5757
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
5858
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
5959
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
60-
github.com/ztrue/shutdown v0.1.1 h1:GKR2ye2OSQlq1GNVE/s2NbrIMsFdmL+NdR6z6t1k+Tg=
61-
github.com/ztrue/shutdown v0.1.1/go.mod h1:hcMWcM2SwIsQk7Wb49aYme4tX66x6iLzs07w1OYAQLw=
6260
go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA=
6361
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
6462
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=

internal/cache/cache.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"context"
55
"crypto/sha1"
66
"encoding/base32"
7+
"errors"
78
"fmt"
89
"io/fs"
910
"os"
1011
"path/filepath"
1112
"time"
1213

1314
"github.com/adrg/xdg"
14-
"github.com/juju/errors"
1515
"github.com/vmihailenco/msgpack/v5"
1616
bolt "go.etcd.io/bbolt"
1717
)
@@ -41,24 +41,23 @@ func Open(treeRoot string, clean bool) (err error) {
4141

4242
name := base32.StdEncoding.EncodeToString(digest)
4343
path, err := xdg.CacheFile(fmt.Sprintf("treefmt/eval-cache/%v.db", name))
44+
if err != nil {
45+
return fmt.Errorf("%w: could not resolve local path for the cache", err)
46+
}
4447

4548
// force a clean of the cache if specified
4649
if clean {
4750
err := os.Remove(path)
4851
if errors.Is(err, os.ErrNotExist) {
4952
err = nil
5053
} else if err != nil {
51-
return errors.Annotate(err, "failed to clear cache")
54+
return fmt.Errorf("%w: failed to clear cache", err)
5255
}
5356
}
5457

55-
if err != nil {
56-
return errors.Annotate(err, "could not resolve local path for the cache")
57-
}
58-
5958
db, err = bolt.Open(path, 0o600, nil)
6059
if err != nil {
61-
return errors.Annotate(err, "failed to open cache")
60+
return fmt.Errorf("%w: failed to open cache", err)
6261
}
6362

6463
err = db.Update(func(tx *bolt.Tx) error {
@@ -86,7 +85,7 @@ func getEntry(bucket *bolt.Bucket, path string) (*Entry, error) {
8685
if b != nil {
8786
var cached Entry
8887
if err := msgpack.Unmarshal(b, &cached); err != nil {
89-
return nil, errors.Annotatef(err, "failed to unmarshal cache info for path '%v'", path)
88+
return nil, fmt.Errorf("%w: failed to unmarshal cache info for path '%v'", err, path)
9089
}
9190
return &cached, nil
9291
} else {
@@ -102,7 +101,7 @@ func ChangeSet(ctx context.Context, root string, pathsCh chan<- string) error {
102101

103102
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
104103
if err != nil {
105-
return errors.Annotate(err, "failed to walk path")
104+
return fmt.Errorf("%w: failed to walk path", err)
106105
} else if ctx.Err() != nil {
107106
return ctx.Err()
108107
} else if info.IsDir() {
@@ -174,11 +173,11 @@ func Update(paths []string) (int, error) {
174173

175174
bytes, err := msgpack.Marshal(cacheInfo)
176175
if err != nil {
177-
return errors.Annotate(err, "failed to marshal mod time")
176+
return fmt.Errorf("%w: failed to marshal mod time", err)
178177
}
179178

180179
if err = bucket.Put([]byte(path), bytes); err != nil {
181-
return errors.Annotate(err, "failed to put mode time")
180+
return fmt.Errorf("%w: failed to put mode time", err)
182181
}
183182
}
184183

internal/cli/format.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"git.numtide.com/numtide/treefmt/internal/format"
1313

1414
"github.com/charmbracelet/log"
15-
"github.com/juju/errors"
1615
"golang.org/x/sync/errgroup"
1716
)
1817

@@ -38,15 +37,15 @@ func (f *Format) Run() error {
3837
// read config
3938
cfg, err := format.ReadConfigFile(Cli.ConfigFile)
4039
if err != nil {
41-
return errors.Annotate(err, "failed to read config file")
40+
return fmt.Errorf("%w: failed to read config file", err)
4241
}
4342

4443
// create optional formatter filter set
4544
formatterSet := make(map[string]bool)
4645
for _, name := range Cli.Formatters {
4746
_, ok := cfg.Formatters[name]
4847
if !ok {
49-
return errors.Errorf("formatter not found in config: %v", name)
48+
return fmt.Errorf("%w: formatter not found in config: %v", err, name)
5049
}
5150
formatterSet[name] = true
5251
}
@@ -75,7 +74,7 @@ func (f *Format) Run() error {
7574
// remove this formatter
7675
delete(cfg.Formatters, name)
7776
} else if err != nil {
78-
return errors.Annotatef(err, "failed to initialise formatter: %v", name)
77+
return fmt.Errorf("%w: failed to initialise formatter: %v", err, name)
7978
}
8079
}
8180

internal/cli/format_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67
"path/filepath"
@@ -9,7 +10,6 @@ import (
910
"git.numtide.com/numtide/treefmt/internal/format"
1011
"github.com/BurntSushi/toml"
1112
"github.com/alecthomas/kong"
12-
"github.com/juju/errors"
1313
cp "github.com/otiai10/copy"
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
@@ -78,12 +78,12 @@ func cmd(t *testing.T, args ...string) ([]byte, error) {
7878

7979
// reset and read the temporary output
8080
if _, err = tempOut.Seek(0, 0); err != nil {
81-
return nil, errors.Annotate(err, "failed to reset temp output for reading")
81+
return nil, fmt.Errorf("%w: failed to reset temp output for reading", err)
8282
}
8383

8484
out, err := io.ReadAll(tempOut)
8585
if err != nil {
86-
return nil, errors.Annotate(err, "failed to read temp output")
86+
return nil, fmt.Errorf("%w: failed to read temp output", err)
8787
}
8888

8989
// swap outputs back

internal/format/format.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package format
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"os/exec"
68
"time"
79

810
"github.com/charmbracelet/log"
911
"github.com/gobwas/glob"
10-
"github.com/juju/errors"
1112
)
1213

13-
const (
14-
// ErrFormatterNotFound is returned when the Command for a Formatter is not available.
15-
ErrFormatterNotFound = errors.ConstError("formatter not found")
16-
)
14+
// ErrFormatterNotFound is returned when the Command for a Formatter is not available.
15+
var ErrFormatterNotFound = errors.New("formatter not found")
1716

1817
// Formatter represents a command which should be applied to a filesystem.
1918
type Formatter struct {
@@ -63,7 +62,7 @@ func (f *Formatter) Init(name string) error {
6362
for _, pattern := range f.Includes {
6463
g, err := glob.Compile("**/" + pattern)
6564
if err != nil {
66-
return errors.Annotatef(err, "failed to compile include pattern '%v' for formatter '%v'", pattern, f.name)
65+
return fmt.Errorf("%w: failed to compile include pattern '%v' for formatter '%v'", err, pattern, f.name)
6766
}
6867
f.includes = append(f.includes, g)
6968
}
@@ -73,7 +72,7 @@ func (f *Formatter) Init(name string) error {
7372
for _, pattern := range f.Excludes {
7473
g, err := glob.Compile("**/" + pattern)
7574
if err != nil {
76-
return errors.Annotatef(err, "failed to compile exclude pattern '%v' for formatter '%v'", pattern, f.name)
75+
return fmt.Errorf("%w: failed to compile exclude pattern '%v' for formatter '%v'", err, pattern, f.name)
7776
}
7877
f.excludes = append(f.excludes, g)
7978
}

0 commit comments

Comments
 (0)