Skip to content

Commit

Permalink
refactor: use slices package instead of custom function
Browse files Browse the repository at this point in the history
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
  • Loading branch information
nikpivkin committed Dec 24, 2024
1 parent 774e04d commit 91ecc30
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/language/python/pipenv/pipenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"os"
"path/filepath"
"slices"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency/parser/python/pipenv"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/language"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

func init() {
Expand All @@ -34,7 +34,7 @@ func (a pipenvLibraryAnalyzer) Analyze(_ context.Context, input analyzer.Analysi

func (a pipenvLibraryAnalyzer) Required(filePath string, _ os.FileInfo) bool {
fileName := filepath.Base(filePath)
return utils.StringInSlice(fileName, requiredFiles)
return slices.Contains(requiredFiles, fileName)
}

func (a pipenvLibraryAnalyzer) Type() analyzer.Type {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/amazonlinux/amazonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"context"
"io"
"os"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

func init() {
Expand Down Expand Up @@ -63,7 +63,7 @@ func (a amazonlinuxOSAnalyzer) parseRelease(r io.Reader) (types.OS, error) {
}

func (a amazonlinuxOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, requiredFiles)
return slices.Contains(requiredFiles, filePath)
}

func (a amazonlinuxOSAnalyzer) Type() analyzer.Type {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/debian/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bufio"
"context"
"os"
"slices"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

func init() {
Expand Down Expand Up @@ -38,7 +38,7 @@ func (a debianOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInpu
}

func (a debianOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, requiredFiles)
return slices.Contains(requiredFiles, filePath)
}

func (a debianOSAnalyzer) Type() analyzer.Type {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/redhatbase/alma.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bufio"
"context"
"os"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

const almaAnalyzerVersion = 1
Expand Down Expand Up @@ -46,7 +46,7 @@ func (a almaOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput)
}

func (a almaOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, a.requiredFiles())
return slices.Contains(a.requiredFiles(), filePath)
}

func (a almaOSAnalyzer) requiredFiles() []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/redhatbase/centos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bufio"
"context"
"os"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

const centosAnalyzerVersion = 1
Expand Down Expand Up @@ -46,7 +46,7 @@ func (a centOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput)
}

func (a centOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, a.requiredFiles())
return slices.Contains(a.requiredFiles(), filePath)
}

func (a centOSAnalyzer) requiredFiles() []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/redhatbase/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bufio"
"context"
"os"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

const fedoraAnalyzerVersion = 1
Expand Down Expand Up @@ -45,7 +45,7 @@ func (a fedoraOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInpu
}

func (a fedoraOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, a.requiredFiles())
return slices.Contains(a.requiredFiles(), filePath)
}

func (a fedoraOSAnalyzer) requiredFiles() []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/redhatbase/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bufio"
"context"
"os"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

const oracleAnalyzerVersion = 1
Expand Down Expand Up @@ -42,7 +42,7 @@ func (a oracleOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInpu
}

func (a oracleOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, a.requiredFiles())
return slices.Contains(a.requiredFiles(), filePath)
}

func (a oracleOSAnalyzer) requiredFiles() []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/redhatbase/redhatbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"io"
"os"
"regexp"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

const redhatAnalyzerVersion = 1
Expand Down Expand Up @@ -83,7 +83,7 @@ func (a redhatOSAnalyzer) parseRelease(r io.Reader) (types.OS, error) {
}

func (a redhatOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, a.requiredFiles())
return slices.Contains(a.requiredFiles(), filePath)
}

func (a redhatOSAnalyzer) requiredFiles() []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/os/redhatbase/rocky.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bufio"
"context"
"os"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
fos "github.com/aquasecurity/trivy/pkg/fanal/analyzer/os"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
)

const rockyAnalyzerVersion = 1
Expand Down Expand Up @@ -46,7 +46,7 @@ func (a rockyOSAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput
}

func (a rockyOSAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, a.requiredFiles())
return slices.Contains(a.requiredFiles(), filePath)
}

func (a rockyOSAnalyzer) requiredFiles() []string {
Expand Down
3 changes: 1 addition & 2 deletions pkg/fanal/analyzer/pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/aquasecurity/trivy/pkg/digest"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/utils"
"github.com/aquasecurity/trivy/pkg/log"
)

Expand Down Expand Up @@ -198,7 +197,7 @@ func (a rpmPkgAnalyzer) listPkgs(ctx context.Context, db RPMDB) (types.Packages,
}

func (a rpmPkgAnalyzer) Required(filePath string, _ os.FileInfo) bool {
return utils.StringInSlice(filePath, requiredFiles)
return slices.Contains(requiredFiles, filePath)
}

func (a rpmPkgAnalyzer) Type() analyzer.Type {
Expand Down
9 changes: 0 additions & 9 deletions pkg/fanal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ func CacheDir() string {
return cacheDir
}

func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

func IsCommandAvailable(name string) bool {
if _, err := exec.LookPath(name); err != nil {
return false
Expand Down

0 comments on commit 91ecc30

Please sign in to comment.