Skip to content

Commit

Permalink
chore: golangci-lint run cleanup!
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Sep 2, 2024
1 parent d3ce852 commit 7f7101d
Show file tree
Hide file tree
Showing 26 changed files with 516 additions and 181 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/ernesto-jimenez/httplogger v0.0.0-20220128121225-117514c3f345 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -46,6 +47,7 @@ require (
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQ
github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 h1:UhxFibDNY/bfvqU5CAUmr9zpesgbU6SWc8/B4mflAE4=
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -180,6 +183,8 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
Expand Down Expand Up @@ -285,6 +290,9 @@ github.com/tj/go-buffer v1.1.0/go.mod h1:iyiJpfFcR2B9sXu7KvjbT9fpM4mOelRSDTbntVj
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4=
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
Expand Down
78 changes: 30 additions & 48 deletions pkg/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@ var (
ascType = filetype.AddType("asc", "text/plain")
pemType = filetype.AddType("pem", "application/x-pem-file")
sigType = filetype.AddType("sig", "text/plain")
sbomJSONType = filetype.AddType(".sbom.json", "application/json")
jsonType = filetype.AddType(".json", "application/json")
sbomType = filetype.AddType(".sbom", "application/octet-stream")
pubType = filetype.AddType(".pub", "text/plain")
sbomJSONType = filetype.AddType("sbom.json", "application/json")
jsonType = filetype.AddType("json", "application/json")
sbomType = filetype.AddType("sbom", "application/octet-stream")
pubType = filetype.AddType("pub", "text/plain")

ignoreFileExtensions = []string{
".txt",
".sbom",
}

executableMimetypes = []string{
"application/x-mach-binary",
"application/x-executable",
"application/vnd.microsoft.portable-executable",
}
)

// Type is the type of asset
type Type int

const (
Expand All @@ -49,29 +61,16 @@ const (
SBOM
)

type IAsset interface {
GetName() string
GetDisplayName() string
GetScore() int
GetType() Type
GetAsset() *Asset
GetFiles() []*File
GetTempPath() string
GetFilePath() string
Score(options *ScoreOptions) int
Download(context.Context) error
Extract() error
Install(string, string) error
Cleanup() error
ID() string
}
// processorFunc is a function that processes a reader
type processorFunc func(io.Reader) (io.Reader, error)

func New(name, displayName, os, arch, version string) *Asset {
// New creates a new asset
func New(name, displayName, osName, osArch, version string) *Asset {
a := &Asset{
Name: name,
DisplayName: displayName,
OS: os,
Arch: arch,
OS: osName,
Arch: osArch,
Version: version,
Files: make([]*File, 0),
score: 0,
Expand Down Expand Up @@ -152,8 +151,8 @@ type ScoreOptions struct {
}

// Classify determines the type of asset based on the file extension
func (a *Asset) Classify() {
if ext := strings.TrimPrefix(filepath.Ext(a.Name), "."); len(ext) > 0 {
func (a *Asset) Classify() { //nolint:gocyclo
if ext := strings.TrimPrefix(filepath.Ext(a.Name), "."); ext != "" {
switch filetype.GetType(ext) {
case matchers.TypeDeb, matchers.TypeRpm, msiType:
a.Type = Installer
Expand All @@ -168,10 +167,11 @@ func (a *Asset) Classify() {
case sbomJSONType, sbomType:
a.Type = SBOM
case jsonType:
if strings.Contains(a.Name, ".sbom") {
if strings.Contains(a.Name, "sbom") {
a.Type = SBOM
} else {
a.Type = Unknown
}
fallthrough
default:
a.Type = Unknown
}
Expand All @@ -196,7 +196,6 @@ func (a *Asset) Classify() {

// Score returns the score of the asset based on the options provided
func (a *Asset) Score(opts *ScoreOptions) int {
var scoringKeys []string
var scoringValues = make(map[string]int)

// Note: if it has the word "update" in it, we want to deprioritize it as it's likely an update binary from
Expand All @@ -212,9 +211,6 @@ func (a *Asset) Score(opts *ScoreOptions) int {
for _, ext := range opts.Extensions {
scoringValues[strings.ToLower(ext)] = 15
}
for key := range scoringValues {
scoringKeys = append(scoringKeys, key)
}

if !a.IsSupportedExtension() {
a.score = -1
Expand All @@ -231,7 +227,7 @@ func (a *Asset) Score(opts *ScoreOptions) int {
}

func (a *Asset) IsSupportedExtension() bool {
if ext := strings.TrimPrefix(filepath.Ext(a.Name), "."); len(ext) > 0 {
if ext := strings.TrimPrefix(filepath.Ext(a.Name), "."); ext != "" {
switch filetype.GetType(ext) {
case matchers.TypeGz, types.Unknown, matchers.TypeZip, matchers.TypeXz, matchers.TypeTar, matchers.TypeBz2, matchers.TypeExe:
break
Expand Down Expand Up @@ -270,18 +266,7 @@ func (a *Asset) copyFile(srcFile, dstFile string) error {
return nil
}

var ignoreFileExtensions = []string{
".txt",
".sbom",
}

var executableMimetypes = []string{
"application/x-mach-binary",
"application/x-executable",
"application/vnd.microsoft.portable-executable",
}

func (a *Asset) Install(id string, binDir string) error {
func (a *Asset) Install(id, binDir string) error {
found := false

logrus.Tracef("files to process: %d", len(a.Files))
Expand Down Expand Up @@ -381,8 +366,6 @@ func (a *Asset) Extract() error {
return a.doExtract(fileHandler)
}

type processorFunc func(io.Reader) (io.Reader, error)

func (a *Asset) doExtract(in io.Reader) error {
var buf bytes.Buffer
tee := io.TeeReader(in, &buf)
Expand Down Expand Up @@ -491,10 +474,9 @@ func (a *Asset) processZip(in io.Reader) (io.Reader, error) {

a.Files = append(a.Files, &File{Name: header.Name})
logrus.Tracef("zip > create file %s", target)

}

if len(a.Files) == 0 {
//return nil, fmt.Errorf("no files found in zip archive. PackagePath [%s]", f.opts.PackagePath)
return nil, fmt.Errorf("no files found in zip archive")
}

Expand Down
Loading

0 comments on commit 7f7101d

Please sign in to comment.