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

Commit 411612e

Browse files
committed
Fix scan message with quiet flag
Signed-off-by: Chris Crone <[email protected]>
1 parent 74773b9 commit 411612e

File tree

8 files changed

+77
-10
lines changed

8 files changed

+77
-10
lines changed

api/compose/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ type BuildOptions struct {
8686
Args types.Mapping
8787
// NoCache disables cache use
8888
NoCache bool
89+
// Quiet make the build process not output to the console
90+
Quiet bool
8991
}
9092

9193
// CreateOptions group options of the Create API

cli/cmd/compose/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error {
9797
Progress: opts.progress,
9898
Args: types.NewMapping(opts.args),
9999
NoCache: opts.noCache,
100+
Quiet: opts.quiet,
100101
})
101102
})
102103
return err

cli/metrics/metrics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ func isCommandFlag(word string) bool {
5757
return utils.StringContains(commandFlags, word)
5858
}
5959

60+
// HasQuietFlag returns true if one of the arguments is `--quiet` or `-q`
61+
func HasQuietFlag(args []string) bool {
62+
for _, a := range args {
63+
switch a {
64+
case "--quiet", "-q":
65+
return true
66+
}
67+
}
68+
return false
69+
}
70+
6071
// GetCommand get the invoked command
6172
func GetCommand(args []string) string {
6273
result := ""

cli/metrics/metrics_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ import (
2222
"gotest.tools/v3/assert"
2323
)
2424

25+
func TestHasQuietFlag(t *testing.T) {
26+
cases := []struct {
27+
name string
28+
args []string
29+
expected bool
30+
}{
31+
{
32+
name: "long flag",
33+
args: []string{"build", "-t", "tag", "--quiet", "."},
34+
expected: true,
35+
},
36+
{
37+
name: "short flag",
38+
args: []string{"build", "-t", "tag", "-q", "."},
39+
expected: true,
40+
},
41+
{
42+
name: "no flag",
43+
args: []string{"build", "-t", "tag", "."},
44+
expected: false,
45+
},
46+
}
47+
for _, c := range cases {
48+
t.Run(c.name, func(t *testing.T) {
49+
result := HasQuietFlag(c.args)
50+
assert.Equal(t, c.expected, result)
51+
})
52+
}
53+
}
54+
2555
func TestGetCommand(t *testing.T) {
2656
testCases := []struct {
2757
name string

cli/mobycli/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func Exec(root *cobra.Command) {
8080
os.Exit(1)
8181
}
8282
command := metrics.GetCommand(os.Args[1:])
83-
if command == "build" {
83+
if command == "build" && !metrics.HasQuietFlag(os.Args[1:]) {
8484
utils.DisplayScanSuggestMsg()
8585
}
8686
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.SuccessStatus)

local/compose/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
7575

7676
err := s.build(ctx, project, opts, Containers{}, options.Progress)
7777
if err == nil {
78-
if len(imagesToBuild) > 0 {
78+
if len(imagesToBuild) > 0 && !options.Quiet {
7979
utils.DisplayScanSuggestMsg()
8080
}
8181
}

local/e2e/compose/scan_message_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,56 @@ func TestDisplayScanMessageAfterBuild(t *testing.T) {
4242

4343
t.Run("display when docker build", func(t *testing.T) {
4444
res := c.RunDockerCmd("build", "-t", "test-image-scan-msg", "./fixtures/simple-build-test/nginx-build")
45-
res.Assert(t, icmd.Expected{Out: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
45+
defer c.RunDockerCmd("rmi", "-f", "test-image-scan-msg")
46+
res.Assert(t, icmd.Expected{Err: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
4647
})
4748

48-
t.Run("do not display if envvar DOCKER_SCAN_SUGGEST=false", func(t *testing.T) {
49+
t.Run("do not display with docker build and quiet flag", func(t *testing.T) {
50+
res := c.RunDockerCmd("build", "-t", "test-image-scan-msg-quiet", "--quiet", "./fixtures/simple-build-test/nginx-build")
51+
defer c.RunDockerCmd("rmi", "-f", "test-image-scan-msg-quiet")
52+
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"))
53+
54+
res = c.RunDockerCmd("build", "-t", "test-image-scan-msg-q", "-q", "./fixtures/simple-build-test/nginx-build")
55+
defer c.RunDockerCmd("rmi", "-f", "test-image-scan-msg-q")
56+
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"))
57+
})
58+
59+
t.Run("do not display if envvar DOCKER_SCAN_SUGGEST=false", func(t *testing.T) {
4960
cmd := c.NewDockerCmd("build", "-t", "test-image-scan-msg", "./fixtures/build-test/nginx-build")
61+
defer c.RunDockerCmd("rmi", "-f", "test-image-scan-msg")
5062
cmd.Env = append(cmd.Env, "DOCKER_SCAN_SUGGEST=false")
5163
res := icmd.StartCmd(cmd)
5264
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
5365
})
5466

5567
t.Run("display on compose build", func(t *testing.T) {
56-
res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "build")
57-
res.Assert(t, icmd.Expected{Out: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
68+
res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test-compose-build", "build")
69+
defer c.RunDockerCmd("rmi", "-f", "scan-msg-test-compose-build_nginx")
70+
res.Assert(t, icmd.Expected{Err: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
71+
})
72+
73+
t.Run("do not display on compose build with quiet flag", func(t *testing.T) {
74+
res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test-quiet", "build", "--quiet")
75+
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
76+
res = c.RunDockerCmd("rmi", "-f", "scan-msg-test-quiet_nginx")
77+
assert.Assert(t, !strings.Contains(res.Combined(), "No such image"))
78+
79+
res = c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test-q", "build", "-q")
80+
defer c.RunDockerCmd("rmi", "-f", "scan-msg-test-q_nginx")
81+
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
5882
})
5983

6084
_ = c.RunDockerOrExitError("rmi", "scan-msg-test_nginx")
6185

6286
t.Run("display on compose up if image is built", func(t *testing.T) {
6387
res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "up", "-d")
6488
defer c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "down")
65-
res.Assert(t, icmd.Expected{Out: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
89+
res.Assert(t, icmd.Expected{Err: "Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them"})
6690
})
6791

6892
t.Run("do not display on compose up if no image built", func(t *testing.T) { // re-run the same Compose aproject
6993
res := c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "up", "-d")
70-
defer c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "down")
94+
defer c.RunDockerCmd("compose", "-f", "./fixtures/simple-build-test/compose.yml", "-p", "scan-msg-test", "down", "--rmi", "all")
7195
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
7296
})
7397

utils/scan_suggest.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ func DisplayScanSuggestMsg() {
3939
if scanAlreadyInvoked() {
4040
return
4141
}
42-
fmt.Println()
43-
fmt.Println("Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them")
42+
fmt.Fprintf(os.Stderr, "\nUse 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them\n")
4443
}
4544

4645
func scanAlreadyInvoked() bool {

0 commit comments

Comments
 (0)