From 66cf5ccc26d51d714cc9a1a2ca10bdddd20348eb Mon Sep 17 00:00:00 2001 From: Yves Brissaud Date: Mon, 9 Jan 2023 11:55:57 +0100 Subject: [PATCH] chore: fix gosimple issues Signed-off-by: Yves Brissaud --- format/cve.go | 2 +- format/format.go | 12 ++++++------ query/base.go | 2 +- registry/save.go | 2 +- sbom/detect/node.go | 2 +- sbom/diff.go | 8 ++------ sbom/monitor.go | 2 +- sbom/upload.go | 4 +--- types/purl.go | 10 +++------- types/query_types.go | 9 ++------- 10 files changed, 19 insertions(+), 34 deletions(-) diff --git a/format/cve.go b/format/cve.go index 7f13d8d..01817d3 100644 --- a/format/cve.go +++ b/format/cve.go @@ -102,6 +102,6 @@ func Cves(cve string, cves *[]types.Cve, sb *types.Sbom, remediate bool, dockerC Remediation(remediation) } } else { - fmt.Println(fmt.Sprintf("%s not detected", cve)) + fmt.Printf("%s not detected\n", cve) } } diff --git a/format/format.go b/format/format.go index 0635f2e..20a1f94 100644 --- a/format/format.go +++ b/format/format.go @@ -174,19 +174,19 @@ func Cve(sb *types.Sbom, c *types.Cve) { sourceId = c.Cve.SourceId } fmt.Println("") - fmt.Println(defaultColors.underline.Sprintf(fmt.Sprintf("Detected %s %s", sourceId, ColorizeSeverity(ToSeverity(*c))))) - fmt.Println(fmt.Sprintf("https://dso.docker.com/cve/%s", sourceId)) + defaultColors.underline.Printf("Detected %s %s\n", sourceId, ColorizeSeverity(ToSeverity(*c))) + fmt.Printf("https://dso.docker.com/cve/%s\n", sourceId) fmt.Println("") purl := c.Purl for _, p := range sb.Artifacts { if p.Purl == purl { - fmt.Println(defaultColors.cyan.Sprintf(p.Purl)) + defaultColors.cyan.Println(p.Purl) loc := p.Locations[0] for i, l := range sb.Source.Image.Config.RootFS.DiffIDs { if l.String() == loc.DiffId { h := sb.Source.Image.Config.History[i] fmt.Println(formatCreatedBy(h.CreatedBy)) - fmt.Println(fmt.Sprintf("%d: %s", i, loc.Digest)) + fmt.Printf("%d: %s\n", i, loc.Digest) } } } @@ -196,9 +196,9 @@ func Cve(sb *types.Sbom, c *types.Cve) { func Remediation(remediation []string) { if len(remediation) > 0 { fmt.Println("") - fmt.Println(defaultColors.underline.Sprintf("Suggested remediation")) + defaultColors.underline.Println("Suggested remediation") for i, r := range remediation { - fmt.Println(fmt.Sprintf("\n%d. %s", i+1, r)) + fmt.Printf("\n%d. %s\n", i+1, r) } } } diff --git a/query/base.go b/query/base.go index 913a5a5..9f47fdf 100644 --- a/query/base.go +++ b/query/base.go @@ -295,7 +295,7 @@ func ForImageInGraphQL(sb *types.Sbom) (*types.ImageByDigestQuery, error) { } func normalizeRepository(image *types.BaseImage) *types.BaseImage { - if image.Repository.Host == "hub.docker.com" && strings.Index(image.Repository.Repo, "/") < 0 { + if image.Repository.Host == "hub.docker.com" && strings.Contains(image.Repository.Repo, "/") { image.Repository.Badge = "OFFICIAL" } if image.Repository.Badge != "" { diff --git a/registry/save.go b/registry/save.go index b5677c0..594c078 100644 --- a/registry/save.go +++ b/registry/save.go @@ -111,7 +111,7 @@ func (c *ImageCache) StoreImage() error { spinner.Stop() } else if format == "tar" { if c.remote { - u := make(chan v1.Update, 0) + u := make(chan v1.Update) errchan := make(chan error) go func() { if err := tarball.WriteToFile(c.ImagePath, *c.Ref, *c.Image, tarball.WithProgress(u)); err != nil { diff --git a/sbom/detect/node.go b/sbom/detect/node.go index f11d4ed..1f77fbb 100644 --- a/sbom/detect/node.go +++ b/sbom/detect/node.go @@ -23,7 +23,7 @@ import ( ) func nodePackageDetector() PackageDetector { - expr := regexp.MustCompile("node\\.js/v(.*)") + expr := regexp.MustCompile(`node\.js/v(.*)`) pkg := types.Package{ Type: "github", Namespace: "nodejs", diff --git a/sbom/diff.go b/sbom/diff.go index 17172f7..c406fa5 100644 --- a/sbom/diff.go +++ b/sbom/diff.go @@ -100,12 +100,8 @@ func toPackageName(pkg packageurl.PackageURL) string { func toImageName(result ImageIndexResult) string { imageName := result.Sbom.Source.Image.Name - if strings.HasPrefix(imageName, "index.docker.io/") { - imageName = imageName[len("index.docker.io/"):] - } - if strings.HasPrefix(imageName, "library/") { - imageName = imageName[len("library/"):] - } + imageName = strings.TrimPrefix(imageName, "index.docker.io/") + imageName = strings.TrimPrefix(imageName, "library/") return imageName } diff --git a/sbom/monitor.go b/sbom/monitor.go index 0e0d8ff..42236f9 100644 --- a/sbom/monitor.go +++ b/sbom/monitor.go @@ -19,7 +19,7 @@ func init() { } func WatchImages(cli command.Cli) error { - indexJobs := make(chan types.ImageSummary, 0) + indexJobs := make(chan types.ImageSummary) for w := 1; w <= maxIndexWorkers; w++ { go indexImageWorker(cli, indexJobs) } diff --git a/sbom/upload.go b/sbom/upload.go index 112c6c1..f830d40 100644 --- a/sbom/upload.go +++ b/sbom/upload.go @@ -326,9 +326,7 @@ func parseReference(sb *types.Sbom) (string, string, error) { host = "hub.docker.com" } name := ref.Context().RepositoryStr() - if strings.HasPrefix(name, "library/") { - name = name[len("library/"):] - } + name = strings.TrimPrefix(name, "library/") return host, name, nil } diff --git a/types/purl.go b/types/purl.go index 8d74eed..ebefb65 100644 --- a/types/purl.go +++ b/types/purl.go @@ -41,10 +41,8 @@ func NormalizePackages(pkgs []Package) ([]Package, error) { } purl.Namespace = toNamespace(purl) - // some versions strings (e.g. such of Go) have a v prefix that we drop - if strings.HasPrefix(purl.Version, "v") { - purl.Version = purl.Version[1:] - } + // some version strings (e.g. such of Go) have a v prefix that we drop + purl.Version = strings.TrimPrefix(purl.Version, "v") if purl.Version == "" { purl.Version = "0.0.0" } @@ -94,9 +92,7 @@ func NormalizePackages(pkgs []Package) ([]Package, error) { } func ToPackageUrl(url string) (packageurl.PackageURL, error) { - if strings.HasSuffix(url, "/") { - url = url[0 : len(url)-1] - } + url = strings.TrimSuffix(url, "/") purl, err := packageurl.FromString(url) return purl, err } diff --git a/types/query_types.go b/types/query_types.go index a50df36..fa1e495 100644 --- a/types/query_types.go +++ b/types/query_types.go @@ -80,13 +80,8 @@ type Image struct { } func ImageTags(image *Image) []string { - tags := make([]string, 0) - for _, tag := range image.Tags { - tags = append(tags, tag) - } - sort.Slice(tags, func(i, j int) bool { - return len(tags[i]) < len(tags[j]) - }) + tags := append([]string{}, image.Tags...) + sort.Strings(tags) return tags }