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

Commit 635db3c

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 635db3c

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-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

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mobycli
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"strconv"
23+
24+
"github.com/fatih/color"
25+
)
26+
27+
const (
28+
scoutHintEnvVarName = "DOCKER_SCOUT_HINTS"
29+
)
30+
31+
func isDockerScoutHintsEnabled() bool {
32+
enabled, err := strconv.ParseBool(os.Getenv(scoutHintEnvVarName))
33+
return err != nil || enabled
34+
}
35+
36+
func displayScoutQuickViewSuggestMsg(image string) {
37+
if !isDockerScoutHintsEnabled() {
38+
return
39+
}
40+
if len(image) > 0 {
41+
image = " " + image
42+
}
43+
out := os.Stderr
44+
b := color.New(color.Bold)
45+
_, _ = fmt.Fprintln(out)
46+
_, _ = b.Fprintln(out, "What's Next?")
47+
_, _ = fmt.Fprintf(out, " View summary of image vulnerabilities and recommendations → %s", color.CyanString("docker scout quickview%s", image))
48+
_, _ = fmt.Fprintln(out)
49+
}

cli/mobycli/scout_suggest_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mobycli
18+
19+
import (
20+
"testing"
21+
22+
"gotest.tools/v3/assert"
23+
)
24+
25+
func TestHintsEnabled(t *testing.T) {
26+
testCases := []struct {
27+
name string
28+
setup func()
29+
expected bool
30+
}{
31+
{
32+
"enabled by default",
33+
func() {},
34+
true,
35+
},
36+
{
37+
"handle true value",
38+
func() {
39+
t.Setenv(scoutHintEnvVarName, "t")
40+
},
41+
true,
42+
},
43+
{
44+
"handle false value",
45+
func() {
46+
t.Setenv(scoutHintEnvVarName, "FALSE")
47+
},
48+
false,
49+
},
50+
{
51+
"handle error",
52+
func() {
53+
t.Setenv(scoutHintEnvVarName, "123")
54+
},
55+
true,
56+
},
57+
}
58+
59+
for _, testCase := range testCases {
60+
tc := testCase
61+
t.Run(tc.name, func(t *testing.T) {
62+
tc.setup()
63+
assert.Equal(t, isDockerScoutHintsEnabled(), tc.expected)
64+
})
65+
}
66+
}

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)