Skip to content

Commit

Permalink
chore: remove dependency on juju/errors (#11)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
zimbatm authored and Brian McGee committed Jan 2, 2024
1 parent 60a1d78 commit 1019851
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 10 additions & 11 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"crypto/sha1"
"encoding/base32"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"

"github.com/adrg/xdg"
"github.com/juju/errors"
"github.com/vmihailenco/msgpack/v5"
bolt "go.etcd.io/bbolt"
)
Expand Down Expand Up @@ -41,24 +41,23 @@ 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 {
err := os.Remove(path)
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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
}
}

Expand Down
7 changes: 3 additions & 4 deletions internal/cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -38,15 +37,15 @@ 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
formatterSet := make(map[string]bool)
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
}
Expand Down Expand Up @@ -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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cli/format_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 1019851

Please sign in to comment.