Skip to content

Commit

Permalink
remove use of github.com/pkg/errors
Browse files Browse the repository at this point in the history
This commit removes all uses of `github.com/pkg/errors.Wrap` and
replaces them by appropriate calls to `fmt.Errorf()`. Other calls are
also migrated to the standard library.
  • Loading branch information
tkw1536 committed Dec 3, 2024
1 parent dd3748a commit 7b4ea1c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 74 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,12 @@ ggman comes with the following builtin aliases:
### 1.23.0 (Upcoming)

- update dependencies to latest
- use standard library packages where available
- update to `goprogram` 0.7.0
- minor linting updates
- minor performance improvements
- explicitly handle output errors
- update to `goprogram` 0.7.0


### 1.22.0 (Released [Sep 22 2024](https://github.com/tkw1536/ggman/releases/tag/v1.22.0))

Expand Down
35 changes: 3 additions & 32 deletions constants/legal/notices.go

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions env/canfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package env
//spellchecker:words bufio strings github errors
import (
"bufio"
"errors"
"fmt"
"io"
"strings"

"github.com/pkg/errors"
)

//spellchecker:words canfile
Expand All @@ -18,7 +18,10 @@ type CanLine struct {
}

// ErrEmpty is an error representing an empty CanLine
var ErrEmpty = errors.New("CanLine is empty")
var ErrEmpty = errors.New("CanLine.Unmarshal: CanLine is empty")

// ErrEmptyFields is an error indicating that unmarshaling a CanLine failed
var ErrEmptyFields = errors.New("CanLine.Unmarshal: strings.Fields() unexpectedly returned 0-length slice")

// Unmarshal reads a CanLine from a string
func (cl *CanLine) Unmarshal(s string) error {
Expand All @@ -35,7 +38,7 @@ func (cl *CanLine) Unmarshal(s string) error {
// switch based on the length
switch len(fields) {
case 0:
return errors.Errorf("strings.Fields() unexpectedly returned 0-length slice")
return ErrEmptyFields
case 1:
fields = []string{"", fields[0]}
}
Expand Down Expand Up @@ -66,12 +69,15 @@ func (cf *CanFile) ReadFrom(reader io.Reader) (int64, error) {

var line CanLine
if err := line.Unmarshal(text); err != nil {
return bytes, errors.Wrap(err, "Unable to parse CANFILE line")
return bytes, fmt.Errorf("unable to parse CANFILE line: %w", err)
}
*cf = append(*cf, line)
}

return bytes, errors.Wrap(scanner.Err(), "Unable to read CANFILE")
if err := scanner.Err(); err != nil {
return bytes, fmt.Errorf("unable to read CANFILE: %w", err)
}
return bytes, nil
}

var defaultCanFile = []string{
Expand Down
7 changes: 5 additions & 2 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package env

//spellchecker:words path filepath strings github errors ggman internal walker goprogram exit pkglib
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/tkw1536/ggman/git"
"github.com/tkw1536/ggman/internal/path"
"github.com/tkw1536/ggman/internal/walker"
Expand Down Expand Up @@ -183,7 +184,7 @@ func (env *Env) LoadDefaultCANFILE() (CanFile, error) {
case errors.Is(err, fs.ErrNotExist):
continue
default:
return nil, errors.Wrapf(err, "Unable to open CANFILE %q", file)
return nil, fmt.Errorf("unable to open CANFILE %q: %w", file, err)
}
defer f.Close()
if _, err := cf.ReadFrom(f); err != nil {
Expand Down Expand Up @@ -424,3 +425,5 @@ func (env Env) ScanRepos(folder string, resolved bool) ([]string, error) {
results, _, err := env.ScanReposScores(folder, resolved)
return results, err
}

// spellchecker:words nosec
12 changes: 7 additions & 5 deletions env/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package env
//spellchecker:words bufio strings github errors goprogram exit pkglib
import (
"bufio"
"fmt"
"os"
"strings"

"github.com/pkg/errors"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/pkglib/fsx"
)
Expand Down Expand Up @@ -115,13 +115,13 @@ func (env Env) NewFromFileFilter(p string, fuzzy bool) (filters []Filter, err er
// resolve the path
path, err := env.Abs(p)
if err != nil {
return nil, errors.Wrapf(err, "Unable to resolve path %q", p)
return nil, fmt.Errorf("unable to resolve path %q: %w", p, err)
}

// open the file
file, err := os.Open(path) /* #nosec G304 -- explicitly passed as a parameter */
if err != nil {
return nil, errors.Wrapf(err, "Unable to open path %q", p)
return nil, fmt.Errorf("unable to open path %q: %w", p, err)
}
defer file.Close()

Expand All @@ -138,7 +138,7 @@ func (env Env) NewFromFileFilter(p string, fuzzy bool) (filters []Filter, err er
}

if err = scanner.Err(); err != nil {
return nil, errors.Wrapf(err, "Unable to read file %q", p)
return nil, fmt.Errorf("unable to read file %q: %w", p, err)
}

return filters, nil
Expand All @@ -159,7 +159,7 @@ func (env Env) ResolvePathFilter(p string) (path string, err error) {
// sub-repositories contained in a path
path, err = env.Abs(p)
if err != nil {
return "", errors.Wrapf(err, "Unable to resolve path %q", p)
return "", fmt.Errorf("unable to resolve path %q: %w", p, err)
}

// must be a directory!
Expand All @@ -169,3 +169,5 @@ func (env Env) ResolvePathFilter(p string) (path string, err error) {

return
}

// spellchecker:words nosec
Loading

0 comments on commit 7b4ea1c

Please sign in to comment.