Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 676857e

Browse files
committed
feat: display docker scout hints on build and pull
On `docker build` and `docker pull` commands, display a hint to `docker scout quickview`. Hints are disabled if quiet flag is used or if `DOCKER_SCOUT_HINTS` environment variable is set to a false (according to Go) value: `0`, `f`, `F`, `false`, `FALSE`, `False` In case of a `docker build` the `docker scout quickview` command doesn't need an argument as it will take the most recently built image by default. In case of a `docker pull` the pulled image is extracted from the command arguments. Signed-off-by: Yves Brissaud <[email protected]>
1 parent a0eabb3 commit 676857e

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

cli/mobycli/exec.go

+12
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ func Exec(_ *cobra.Command) {
111111
}
112112
commandArgs := os.Args[1:]
113113
command := metrics.GetCommand(commandArgs)
114+
if (command == "build" || command == "pull") && !metrics.HasQuietFlag(commandArgs) {
115+
var image string
116+
if command == "pull" {
117+
for _, a := range commandArgs[1:] {
118+
if !strings.HasPrefix(a, "--") {
119+
image = a
120+
break
121+
}
122+
}
123+
}
124+
displayScoutQuickViewSuggestMsg(image)
125+
}
114126
if command == "login" && !metrics.HasQuietFlag(commandArgs) {
115127
displayPATSuggestMsg(commandArgs)
116128
}

cli/mobycli/scout_suggest.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mobycli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
8+
"github.com/fatih/color"
9+
)
10+
11+
const (
12+
scoutHintEnvVarName = "DOCKER_SCOUT_HINTS"
13+
)
14+
15+
func isDockerScoutHintsEnabled() bool {
16+
enabled, err := strconv.ParseBool(os.Getenv(scoutHintEnvVarName))
17+
return err != nil || enabled
18+
}
19+
20+
func displayScoutQuickViewSuggestMsg(image string) {
21+
if !isDockerScoutHintsEnabled() {
22+
return
23+
}
24+
if len(image) > 0 {
25+
image = " " + image
26+
}
27+
out := os.Stderr
28+
b := color.New(color.Bold)
29+
_, _ = fmt.Fprintln(out)
30+
_, _ = b.Fprintln(out, "What's Next?")
31+
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s", color.CyanString("docker scout quickview%s", image))
32+
_, _ = fmt.Fprintln(out)
33+
}

cli/mobycli/scout_suggest_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package mobycli
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestHintsEnabled(t *testing.T) {
10+
testCases := []struct {
11+
name string
12+
setup func()
13+
expected bool
14+
}{
15+
{
16+
"enabled by default",
17+
func() {},
18+
true,
19+
},
20+
{
21+
"handle true value",
22+
func() {
23+
t.Setenv(scoutHintEnvVarName, "t")
24+
},
25+
true,
26+
},
27+
{
28+
"handle false value",
29+
func() {
30+
t.Setenv(scoutHintEnvVarName, "FALSE")
31+
},
32+
false,
33+
},
34+
{
35+
"handle error",
36+
func() {
37+
t.Setenv(scoutHintEnvVarName, "123")
38+
},
39+
true,
40+
},
41+
}
42+
43+
for _, testCase := range testCases {
44+
tc := testCase
45+
t.Run(tc.name, func(t *testing.T) {
46+
tc.setup()
47+
assert.Equal(t, isDockerScoutHintsEnabled(), tc.expected)
48+
})
49+
}
50+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/docker/docker v20.10.7+incompatible
2727
github.com/docker/go-connections v0.4.0
2828
github.com/docker/go-units v0.5.0
29+
github.com/fatih/color v1.7.0
2930
github.com/gobwas/ws v1.1.0
3031
github.com/golang/mock v1.6.0
3132
github.com/golang/protobuf v1.5.2
@@ -100,7 +101,6 @@ require (
100101
github.com/docker/go-metrics v0.0.1 // indirect
101102
github.com/evanphx/json-patch v4.9.0+incompatible // indirect
102103
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
103-
github.com/fatih/color v1.7.0 // indirect
104104
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
105105
github.com/fvbommel/sortorder v1.0.1 // indirect
106106
github.com/go-errors/errors v1.0.1 // indirect

0 commit comments

Comments
 (0)