Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Go 1.20 wrapped errors instead of multierror #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 5 additions & 4 deletions internal/license/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package license

import (
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"regexp"
"strings"
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type ModuleReference struct {
}

// pathRgx covers
// - unix paths: ".", "..", prefixed "./", prefixed "../", prefixed "/"
// - windows paths: ".", "..", prefixed ".\", prefixed "..\", prefixed "<drive>:\"
// - unix paths: ".", "..", prefixed "./", prefixed "../", prefixed "/"
// - windows paths: ".", "..", prefixed ".\", prefixed "..\", prefixed "<drive>:\"
var pathRgx = regexp.MustCompile(`^(\.\.?($|/|\\)|/|[A-Za-z]:\\)`)

// IsLocal returns true if the module reference points to a local path
Expand Down
12 changes: 6 additions & 6 deletions internal/module/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package module

import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"

"github.com/hashicorp/go-multierror"
"github.com/uw-labs/lichen/internal/buildinfo"
"github.com/uw-labs/lichen/internal/model"
)
Expand All @@ -30,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
Expand All @@ -50,7 +50,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)
}
Expand Down
11 changes: 5 additions & 6 deletions internal/module/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"

"github.com/hashicorp/go-multierror"
"github.com/uw-labs/lichen/internal/model"
)

Expand All @@ -25,7 +23,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)
}
Expand Down Expand Up @@ -76,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...)
}
10 changes: 4 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"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"
Expand Down Expand Up @@ -89,19 +87,19 @@ 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) {
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)
}
Expand Down
Loading