Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit b13049d

Browse files
committed
Polish output
Signed-off-by: Christian Dupuis <[email protected]>
1 parent 3e7abe2 commit b13049d

File tree

7 files changed

+46
-12
lines changed

7 files changed

+46
-12
lines changed

commands/cmd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import (
2828
"github.com/docker/cli/cli"
2929
"github.com/docker/cli/cli-plugins/plugin"
3030
"github.com/docker/cli/cli/command"
31+
"github.com/docker/index-cli-plugin/internal"
3132
"github.com/docker/index-cli-plugin/query"
3233
"github.com/docker/index-cli-plugin/sbom"
3334
"github.com/docker/index-cli-plugin/types"
34-
"github.com/docker/index-cli-plugin/util"
3535
v1 "github.com/google/go-containerregistry/pkg/v1"
3636
"github.com/moby/term"
3737
"github.com/pkg/errors"
@@ -251,7 +251,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
251251
}
252252

253253
// see if the package comes in via the base image
254-
s := util.StartInfoSpinner("Detecting base image", dockerCli.Out().IsTerminal())
254+
s := internal.StartInfoSpinner("Detecting base image", dockerCli.Out().IsTerminal())
255255
defer s.Stop()
256256
baseImages, index, _ := query.Detect(img, true, workspace, apiKey)
257257
s.Stop()
@@ -266,7 +266,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
266266
}
267267

268268
if baseImage != nil {
269-
s := util.StartInfoSpinner("Finding alternative base images", dockerCli.Out().IsTerminal())
269+
s := internal.StartInfoSpinner("Finding alternative base images", dockerCli.Out().IsTerminal())
270270
defer s.Stop()
271271
aBaseImage, _ := query.ForBaseImageWithoutCve(c.SourceId, baseImage.Repository.Name, img, workspace, apiKey)
272272
s.Stop()

util/spinner.go renamed to internal/spinner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package util
17+
package internal
1818

1919
import (
2020
"fmt"

internal/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,19 @@ func Contains[K interface{}](slice []K, value K) bool {
5454
}
5555
return false
5656
}
57+
58+
func UniqueBy[K interface{}](slice []K, by func(K) string) []K {
59+
values := make(map[string]K)
60+
for _, s := range slice {
61+
k := by(s)
62+
if _, ok := values[k]; !ok {
63+
values[k] = s
64+
}
65+
}
66+
67+
v := make([]K, 0, len(values))
68+
for _, value := range values {
69+
v = append(v, value)
70+
}
71+
return v
72+
}

query/query.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ func QueryCves(sb *types.Sbom, cve string, workspace string, apiKey string) (*[]
8686
} else {
8787
skill.Log.Infof("Detected %d vulnerabilities", len(result.Query.Data[0].Cves))
8888
}
89-
return &result.Query.Data[0].Cves, nil
89+
fcves := internal.UniqueBy(result.Query.Data[0].Cves, func(cve types.Cve) string {
90+
if cve.Cve != nil {
91+
return fmt.Sprintf("%s %s", cve.Purl, cve.Cve.SourceId)
92+
} else {
93+
return fmt.Sprintf("%s %s", cve.Purl, cve.Advisory.SourceId)
94+
}
95+
})
96+
return &fcves, nil
97+
9098
} else {
9199
return nil, nil
92100
}

registry/save.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
"github.com/atomist-skills/go-skill"
2727
"github.com/docker/cli/cli/command"
28-
"github.com/docker/index-cli-plugin/util"
28+
"github.com/docker/index-cli-plugin/internal"
2929
"github.com/dustin/go-humanize"
3030
"github.com/google/go-containerregistry/pkg/authn"
3131
"github.com/google/go-containerregistry/pkg/name"
@@ -88,14 +88,14 @@ func (c *ImageCache) StoreImage() error {
8888
var update v1.Update
8989
var err error
9090
var pp int64
91-
spinner := util.StartSpinner("info", "Copying image", c.cli.Out().IsTerminal())
91+
spinner := internal.StartSpinner("info", "Copying image", c.cli.Out().IsTerminal())
9292
defer spinner.Stop()
9393
for {
9494
select {
9595
case update = <-u:
9696
p := 100 * update.Complete / update.Total
9797
if pp != p {
98-
spinner.WithFields(util.Fields{
98+
spinner.WithFields(internal.Fields{
9999
"event": "progress",
100100
"total": update.Total,
101101
"complete": update.Complete,

sbom/index.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/docker/index-cli-plugin/query"
3131
"github.com/docker/index-cli-plugin/registry"
3232
"github.com/docker/index-cli-plugin/types"
33-
"github.com/docker/index-cli-plugin/util"
3433
"github.com/google/go-containerregistry/pkg/name"
3534
v1 "github.com/google/go-containerregistry/pkg/v1"
3635
"github.com/pkg/errors"
@@ -102,7 +101,7 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, *v1.I
102101
lm := createLayerMapping(*cache.Image)
103102
skill.Log.Debugf("Created layer mapping")
104103

105-
s := util.StartSpinner("info", "Indexing", cli.Out().IsTerminal())
104+
s := internal.StartSpinner("info", "Indexing", cli.Out().IsTerminal())
106105
defer s.Stop()
107106
trivyResultChan := make(chan types.IndexResult)
108107
syftResultChan := make(chan types.IndexResult)

types/format.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ func FormatCve(sb *Sbom, c *Cve) {
220220
for i, l := range sb.Source.Image.Config.RootFS.DiffIDs {
221221
if l.String() == loc.DiffId {
222222
h := sb.Source.Image.Config.History[i]
223-
fmt.Println(fmt.Sprintf("Instruction: %s", h.CreatedBy))
224-
fmt.Println(fmt.Sprintf("Layer %d: %s", i, loc.Digest))
223+
fmt.Println(formatCreatedBy(h.CreatedBy))
224+
fmt.Println(fmt.Sprintf("%d: %s", i, loc.Digest))
225225
}
226226
}
227227
}
@@ -343,3 +343,14 @@ func ToSeverityInt(cve Cve) int {
343343
return 0
344344
}
345345
}
346+
347+
func formatCreatedBy(createdBy string) string {
348+
trim := func(prefix string) {
349+
if strings.HasPrefix(createdBy, prefix) {
350+
createdBy = strings.TrimSpace(createdBy[len(prefix):])
351+
}
352+
}
353+
trim("/bin/sh -c")
354+
trim("#(nop)")
355+
return createdBy
356+
}

0 commit comments

Comments
 (0)