From 5bcd7c0365c5dd67433bbdc995753809df57a301 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 19 Aug 2023 17:30:32 +1200 Subject: [PATCH] refactor: use `slices.Contains` --- go.mod | 6 +++--- go.sum | 22 ++++++---------------- main.go | 13 ++----------- pkg/database/osv.go | 25 +++---------------------- 4 files changed, 14 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index e2a55569..5c09d361 100644 --- a/go.mod +++ b/go.mod @@ -6,13 +6,13 @@ require ( github.com/BurntSushi/toml v1.0.0 github.com/fatih/color v1.13.0 github.com/google/go-cmp v0.5.9 - golang.org/x/mod v0.5.1 + golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 + golang.org/x/mod v0.12.0 gopkg.in/yaml.v2 v2.4.0 ) require ( github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-isatty v0.0.14 // indirect - golang.org/x/sys v0.1.0 // indirect - golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect + golang.org/x/sys v0.11.0 // indirect ) diff --git a/go.sum b/go.sum index c6a9a35d..1757b2d7 100644 --- a/go.sum +++ b/go.sum @@ -9,25 +9,15 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= -golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/main.go b/main.go index a08b555e..8ef664b2 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/g-rath/osv-detector/internal/reporter" "github.com/g-rath/osv-detector/pkg/database" "github.com/g-rath/osv-detector/pkg/lockfile" + "golang.org/x/exp/slices" "io" "os" "path/filepath" @@ -41,16 +42,6 @@ func makeEcosystemDBConfig(ecosystem internal.Ecosystem) database.Config { type OSVDatabases []database.DB -func contains(items []string, value string) bool { - for _, item := range items { - if value == item { - return true - } - } - - return false -} - func (dbs OSVDatabases) transposePkgResults( pkg internal.PackageDetails, ignores []string, @@ -69,7 +60,7 @@ func (dbs OSVDatabases) transposePkgResults( continue } - if contains(ignores, vulnerability.ID) { + if slices.Contains(ignores, vulnerability.ID) { ignored = append(ignored, vulnerability) } else { vulnerabilities = append(vulnerabilities, vulnerability) diff --git a/pkg/database/osv.go b/pkg/database/osv.go index 3cdb9c24..0afc0ba3 100644 --- a/pkg/database/osv.go +++ b/pkg/database/osv.go @@ -7,6 +7,7 @@ import ( "github.com/g-rath/osv-detector/internal/cachedregexp" "github.com/g-rath/osv-detector/pkg/lockfile" "github.com/g-rath/osv-detector/pkg/semantic" + "golang.org/x/exp/slices" "os" "sort" "strings" @@ -157,16 +158,6 @@ func (vs Versions) MarshalJSON() ([]byte, error) { return out, nil } -func (vs Versions) includes(v string) bool { - for _, v2 := range vs { - if v == v2 { - return true - } - } - - return false -} - type Affected struct { Package Package `json:"package"` Versions Versions `json:"versions"` @@ -185,19 +176,9 @@ type OSV struct { Affected []Affected `json:"affected"` } -func (osv *OSV) isAliasOfID(id string) bool { - for _, alias := range osv.Aliases { - if alias == id { - return true - } - } - - return false -} - func (osv *OSV) isAliasOf(vulnerability OSV) bool { for _, alias := range vulnerability.Aliases { - if osv.ID == alias || osv.isAliasOfID(alias) { + if osv.ID == alias || slices.Contains(osv.Aliases, alias) { return true } } @@ -287,7 +268,7 @@ func (osv *OSV) IsAffected(pkg internal.PackageDetails) bool { continue } - if affected.Versions.includes(pkg.Version) { + if slices.Contains(affected.Versions, pkg.Version) { return true }