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: #11
Co-authored-by: zimbatm <zimbatm@zimbatm.com>
Co-committed-by: zimbatm <zimbatm@zimbatm.com>
This commit is contained in:
Jonas Chevalier 2024-01-02 10:33:50 +00:00 committed by Brian McGee
parent 60a1d7848e
commit 1019851207
6 changed files with 22 additions and 28 deletions

1
go.mod
View File

@ -12,7 +12,6 @@ require (
github.com/otiai10/copy v1.14.0
github.com/stretchr/testify v1.8.4
github.com/vmihailenco/msgpack/v5 v5.4.1
github.com/ztrue/shutdown v0.1.1
go.etcd.io/bbolt v1.3.8
golang.org/x/sync v0.5.0
)

2
go.sum
View File

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

View File

@ -4,6 +4,7 @@ import (
"context"
"crypto/sha1"
"encoding/base32"
"errors"
"fmt"
"io/fs"
"os"
@ -11,7 +12,6 @@ import (
"time"
"github.com/adrg/xdg"
"github.com/juju/errors"
"github.com/vmihailenco/msgpack/v5"
bolt "go.etcd.io/bbolt"
)
@ -41,6 +41,9 @@ func Open(treeRoot string, clean bool) (err error) {
name := base32.StdEncoding.EncodeToString(digest)
path, err := xdg.CacheFile(fmt.Sprintf("treefmt/eval-cache/%v.db", name))
if err != nil {
return fmt.Errorf("%w: could not resolve local path for the cache", err)
}
// force a clean of the cache if specified
if clean {
@ -48,17 +51,13 @@ func Open(treeRoot string, clean bool) (err error) {
if errors.Is(err, os.ErrNotExist) {
err = nil
} else if err != nil {
return errors.Annotate(err, "failed to clear cache")
return fmt.Errorf("%w: failed to clear cache", err)
}
}
if err != nil {
return errors.Annotate(err, "could not resolve local path for the cache")
}
db, err = bolt.Open(path, 0o600, nil)
if err != nil {
return errors.Annotate(err, "failed to open cache")
return fmt.Errorf("%w: failed to open cache", err)
}
err = db.Update(func(tx *bolt.Tx) error {
@ -86,7 +85,7 @@ func getEntry(bucket *bolt.Bucket, path string) (*Entry, error) {
if b != nil {
var cached Entry
if err := msgpack.Unmarshal(b, &cached); err != nil {
return nil, errors.Annotatef(err, "failed to unmarshal cache info for path '%v'", path)
return nil, fmt.Errorf("%w: failed to unmarshal cache info for path '%v'", err, path)
}
return &cached, nil
} else {
@ -102,7 +101,7 @@ func ChangeSet(ctx context.Context, root string, pathsCh chan<- string) error {
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return errors.Annotate(err, "failed to walk path")
return fmt.Errorf("%w: failed to walk path", err)
} else if ctx.Err() != nil {
return ctx.Err()
} else if info.IsDir() {
@ -174,11 +173,11 @@ func Update(paths []string) (int, error) {
bytes, err := msgpack.Marshal(cacheInfo)
if err != nil {
return errors.Annotate(err, "failed to marshal mod time")
return fmt.Errorf("%w: failed to marshal mod time", err)
}
if err = bucket.Put([]byte(path), bytes); err != nil {
return errors.Annotate(err, "failed to put mode time")
return fmt.Errorf("%w: failed to put mode time", err)
}
}

View File

@ -12,7 +12,6 @@ import (
"git.numtide.com/numtide/treefmt/internal/format"
"github.com/charmbracelet/log"
"github.com/juju/errors"
"golang.org/x/sync/errgroup"
)
@ -38,7 +37,7 @@ func (f *Format) Run() error {
// read config
cfg, err := format.ReadConfigFile(Cli.ConfigFile)
if err != nil {
return errors.Annotate(err, "failed to read config file")
return fmt.Errorf("%w: failed to read config file", err)
}
// create optional formatter filter set
@ -46,7 +45,7 @@ func (f *Format) Run() error {
for _, name := range Cli.Formatters {
_, ok := cfg.Formatters[name]
if !ok {
return errors.Errorf("formatter not found in config: %v", name)
return fmt.Errorf("%w: formatter not found in config: %v", err, name)
}
formatterSet[name] = true
}
@ -75,7 +74,7 @@ func (f *Format) Run() error {
// remove this formatter
delete(cfg.Formatters, name)
} else if err != nil {
return errors.Annotatef(err, "failed to initialise formatter: %v", name)
return fmt.Errorf("%w: failed to initialise formatter: %v", err, name)
}
}

View File

@ -1,6 +1,7 @@
package cli
import (
"fmt"
"io"
"os"
"path/filepath"
@ -9,7 +10,6 @@ import (
"git.numtide.com/numtide/treefmt/internal/format"
"github.com/BurntSushi/toml"
"github.com/alecthomas/kong"
"github.com/juju/errors"
cp "github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -78,12 +78,12 @@ func cmd(t *testing.T, args ...string) ([]byte, error) {
// reset and read the temporary output
if _, err = tempOut.Seek(0, 0); err != nil {
return nil, errors.Annotate(err, "failed to reset temp output for reading")
return nil, fmt.Errorf("%w: failed to reset temp output for reading", err)
}
out, err := io.ReadAll(tempOut)
if err != nil {
return nil, errors.Annotate(err, "failed to read temp output")
return nil, fmt.Errorf("%w: failed to read temp output", err)
}
// swap outputs back

View File

@ -2,18 +2,17 @@ package format
import (
"context"
"errors"
"fmt"
"os/exec"
"time"
"github.com/charmbracelet/log"
"github.com/gobwas/glob"
"github.com/juju/errors"
)
const (
// ErrFormatterNotFound is returned when the Command for a Formatter is not available.
ErrFormatterNotFound = errors.ConstError("formatter not found")
)
// ErrFormatterNotFound is returned when the Command for a Formatter is not available.
var ErrFormatterNotFound = errors.New("formatter not found")
// Formatter represents a command which should be applied to a filesystem.
type Formatter struct {
@ -63,7 +62,7 @@ func (f *Formatter) Init(name string) error {
for _, pattern := range f.Includes {
g, err := glob.Compile("**/" + pattern)
if err != nil {
return errors.Annotatef(err, "failed to compile include pattern '%v' for formatter '%v'", pattern, f.name)
return fmt.Errorf("%w: failed to compile include pattern '%v' for formatter '%v'", err, pattern, f.name)
}
f.includes = append(f.includes, g)
}
@ -73,7 +72,7 @@ func (f *Formatter) Init(name string) error {
for _, pattern := range f.Excludes {
g, err := glob.Compile("**/" + pattern)
if err != nil {
return errors.Annotatef(err, "failed to compile exclude pattern '%v' for formatter '%v'", pattern, f.name)
return fmt.Errorf("%w: failed to compile exclude pattern '%v' for formatter '%v'", err, pattern, f.name)
}
f.excludes = append(f.excludes, g)
}