From 8c476032979ecef4cd2914452e35449afb86574f Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Fri, 5 Jan 2024 14:40:24 +0100 Subject: [PATCH 1/4] Format internal/model/model.go This is failing linting (gofmt -s). Signed-off-by: Stephen Kitt --- internal/model/model.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/model/model.go b/internal/model/model.go index ba58c61..a2d0008 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -27,8 +27,8 @@ type ModuleReference struct { } // pathRgx covers -// - unix paths: ".", "..", prefixed "./", prefixed "../", prefixed "/" -// - windows paths: ".", "..", prefixed ".\", prefixed "..\", prefixed ":\" +// - unix paths: ".", "..", prefixed "./", prefixed "../", prefixed "/" +// - windows paths: ".", "..", prefixed ".\", prefixed "..\", prefixed ":\" var pathRgx = regexp.MustCompile(`^(\.\.?($|/|\\)|/|[A-Za-z]:\\)`) // IsLocal returns true if the module reference points to a local path From 4c210f13f95eea871cf6313a7952f8739ddb13b9 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Fri, 5 Jan 2024 14:40:44 +0100 Subject: [PATCH 2/4] Disable depguard depguard has switch from a deny list to an allow list, so it rejects everything by default. Since depguard wasn't previously configured to deny anything, this disables it entirely (no linting is lost). Signed-off-by: Stephen Kitt --- .golangci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index b774272..b98b1f1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,7 +3,6 @@ linters: enable: - bodyclose - deadcode - - depguard - dogsled - dupl - errcheck From 14d354bcd2eb60862d0bded1e28988b074dd4027 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Fri, 5 Jan 2024 15:16:04 +0100 Subject: [PATCH 3/4] Replace deprecated ioutil functions Signed-off-by: Stephen Kitt --- internal/license/resolve.go | 9 +++++---- internal/module/extract.go | 3 +-- internal/module/fetch.go | 3 +-- main.go | 3 +-- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/internal/license/resolve.go b/internal/license/resolve.go index 5e86921..a9210a9 100644 --- a/internal/license/resolve.go +++ b/internal/license/resolve.go @@ -2,7 +2,8 @@ package license import ( "fmt" - "io/ioutil" + "io" + "os" "path/filepath" "regexp" "strings" @@ -21,7 +22,7 @@ func Resolve(modules []model.Module, threshold float64) ([]model.Module, error) return nil, fmt.Errorf("failed to open license databse: %w", err) } defer f.Close() - return ioutil.ReadAll(f) + return io.ReadAll(f) }) lc, err := licenseclassifier.New(threshold, archiveFn) @@ -55,7 +56,7 @@ var fileRgx = regexp.MustCompile(`(?i)^(li[cs]en[cs]e|copying)`) // locateLicenses searches for license files func locateLicenses(path string) (lp []string, err error) { - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { return nil, err } @@ -71,7 +72,7 @@ func locateLicenses(path string) (lp []string, err error) { func classify(lc *licenseclassifier.License, paths []string) ([]model.License, error) { licenses := make([]model.License, 0) for _, p := range paths { - content, err := ioutil.ReadFile(p) + content, err := os.ReadFile(p) if err != nil { return nil, err } diff --git a/internal/module/extract.go b/internal/module/extract.go index 7c332bc..05398ce 100644 --- a/internal/module/extract.go +++ b/internal/module/extract.go @@ -3,7 +3,6 @@ package module import ( "context" "fmt" - "io/ioutil" "os" "os/exec" @@ -50,7 +49,7 @@ func goVersion(ctx context.Context, paths []string) (string, error) { return "", err } - tempDir, err := ioutil.TempDir("", "lichen") + tempDir, err := os.MkdirTemp("", "lichen") if err != nil { return "", fmt.Errorf("failed to create temp directory: %w", err) } diff --git a/internal/module/fetch.go b/internal/module/fetch.go index 2e9edbd..4886d51 100644 --- a/internal/module/fetch.go +++ b/internal/module/fetch.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" @@ -25,7 +24,7 @@ func Fetch(ctx context.Context, refs []model.ModuleReference) ([]model.Module, e return nil, err } - tempDir, err := ioutil.TempDir("", "lichen") + tempDir, err := os.MkdirTemp("", "lichen") if err != nil { return nil, fmt.Errorf("failed to create temp directory: %w", err) } diff --git a/main.go b/main.go index 71f7bd8..1a30602 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -101,7 +100,7 @@ func run(c *cli.Context) error { func parseConfig(path string) (scan.Config, error) { var conf scan.Config if path != "" { - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return scan.Config{}, fmt.Errorf("failed to read file %q: %w", path, err) } From e555d8b30026cadaa0e14710975ae475abc26fc8 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Fri, 5 Jan 2024 15:16:37 +0100 Subject: [PATCH 4/4] Use Go 1.20 wrapped errors instead of multierror Signed-off-by: Stephen Kitt --- .github/workflows/ci.yaml | 4 ++-- go.mod | 4 +--- go.sum | 4 ---- internal/module/extract.go | 9 +++++---- internal/module/fetch.go | 8 ++++---- main.go | 7 +++---- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 83f0008..e341f89 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.17.x, 1.18.x] + go-version: [1.20.x] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: @@ -24,7 +24,7 @@ jobs: - name: Install Go uses: actions/setup-go@v2 with: - go-version: 1.18.x + go-version: 1.20.x - name: Checkout uses: actions/checkout@v3 - name: Lint diff --git a/go.mod b/go.mod index ef0e90d..b266308 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,9 @@ module github.com/uw-labs/lichen -go 1.18 +go 1.20 require ( github.com/google/licenseclassifier v0.0.0-20201113175434-78a70215ca36 - github.com/hashicorp/go-multierror v1.1.1 github.com/muesli/termenv v0.11.0 github.com/stretchr/testify v1.7.1 github.com/urfave/cli/v2 v2.4.0 @@ -14,7 +13,6 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/davecgh/go-spew v1.1.0 // indirect - github.com/hashicorp/errwrap v1.0.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect diff --git a/go.sum b/go.sum index c221bbe..d4defe3 100644 --- a/go.sum +++ b/go.sum @@ -6,10 +6,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/licenseclassifier v0.0.0-20201113175434-78a70215ca36 h1:YGB3wNLUTvq+lbIwdNRsaMJvoX4mCKkwzHlmlT1V+ow= github.com/google/licenseclassifier v0.0.0-20201113175434-78a70215ca36/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M= -github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= -github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= -github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= diff --git a/internal/module/extract.go b/internal/module/extract.go index 05398ce..5a3222f 100644 --- a/internal/module/extract.go +++ b/internal/module/extract.go @@ -2,11 +2,11 @@ package module import ( "context" + "errors" "fmt" "os" "os/exec" - "github.com/hashicorp/go-multierror" "github.com/uw-labs/lichen/internal/buildinfo" "github.com/uw-labs/lichen/internal/model" ) @@ -29,17 +29,18 @@ func Extract(ctx context.Context, paths ...string) ([]model.BuildInfo, error) { } // verifyExtracted ensures all paths requests are covered by the parsed output -func verifyExtracted(extracted []model.BuildInfo, requested []string) (err error) { +func verifyExtracted(extracted []model.BuildInfo, requested []string) error { buildInfos := make(map[string]struct{}, len(extracted)) + errs := []error{} for _, binary := range extracted { buildInfos[binary.Path] = struct{}{} } for _, path := range requested { if _, found := buildInfos[path]; !found { - err = multierror.Append(err, fmt.Errorf("modules could not be obtained from %[1]s (hint: run `go version -m %[1]q`)", path)) + errs = append(errs, fmt.Errorf("modules could not be obtained from %[1]s (hint: run `go version -m %[1]q`)", path)) } } - return + return errors.Join(errs...) } // goVersion runs `go version -m [paths ...]` and returns the output diff --git a/internal/module/fetch.go b/internal/module/fetch.go index 4886d51..2b0889b 100644 --- a/internal/module/fetch.go +++ b/internal/module/fetch.go @@ -10,7 +10,6 @@ import ( "os" "os/exec" - "github.com/hashicorp/go-multierror" "github.com/uw-labs/lichen/internal/model" ) @@ -75,15 +74,16 @@ func Fetch(ctx context.Context, refs []model.ModuleReference) ([]model.Module, e return modules, nil } -func verifyFetched(fetched []model.Module, requested []model.ModuleReference) (err error) { +func verifyFetched(fetched []model.Module, requested []model.ModuleReference) error { fetchedRefs := make(map[model.ModuleReference]struct{}, len(fetched)) + errs := []error{} for _, module := range fetched { fetchedRefs[module.ModuleReference] = struct{}{} } for _, ref := range requested { if _, found := fetchedRefs[ref]; !found { - err = multierror.Append(err, fmt.Errorf("module %s could not be resolved", ref)) + errs = append(errs, fmt.Errorf("module %s could not be resolved", ref)) } } - return + return errors.Join(errs...) } diff --git a/main.go b/main.go index 1a30602..3b27712 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "path/filepath" "text/template" - "github.com/hashicorp/go-multierror" "github.com/muesli/termenv" "github.com/urfave/cli/v2" "github.com/uw-labs/lichen/internal/scan" @@ -88,13 +87,13 @@ func run(c *cli.Context) error { return fmt.Errorf("failed to write results: %w", err) } - var rErr error + errs := []error{} for _, m := range summary.Modules { if !m.Allowed() { - rErr = multierror.Append(rErr, fmt.Errorf("%s: %s", m.Module.ModuleReference, m.ExplainDecision())) + errs = append(errs, fmt.Errorf("%s: %s", m.Module.ModuleReference, m.ExplainDecision())) } } - return rErr + return errors.Join(errs...) } func parseConfig(path string) (scan.Config, error) {