Fix scan message with quiet flag

Signed-off-by: Chris Crone <christopher.crone@docker.com>
This commit is contained in:
Chris Crone 2021-04-13 13:53:07 +02:00
parent 74773b9062
commit 411612ed8d
8 changed files with 77 additions and 10 deletions

View file

@ -97,6 +97,7 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error {
Progress: opts.progress,
Args: types.NewMapping(opts.args),
NoCache: opts.noCache,
Quiet: opts.quiet,
})
})
return err

View file

@ -57,6 +57,17 @@ func isCommandFlag(word string) bool {
return utils.StringContains(commandFlags, word)
}
// HasQuietFlag returns true if one of the arguments is `--quiet` or `-q`
func HasQuietFlag(args []string) bool {
for _, a := range args {
switch a {
case "--quiet", "-q":
return true
}
}
return false
}
// GetCommand get the invoked command
func GetCommand(args []string) string {
result := ""

View file

@ -22,6 +22,36 @@ import (
"gotest.tools/v3/assert"
)
func TestHasQuietFlag(t *testing.T) {
cases := []struct {
name string
args []string
expected bool
}{
{
name: "long flag",
args: []string{"build", "-t", "tag", "--quiet", "."},
expected: true,
},
{
name: "short flag",
args: []string{"build", "-t", "tag", "-q", "."},
expected: true,
},
{
name: "no flag",
args: []string{"build", "-t", "tag", "."},
expected: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := HasQuietFlag(c.args)
assert.Equal(t, c.expected, result)
})
}
}
func TestGetCommand(t *testing.T) {
testCases := []struct {
name string

View file

@ -80,7 +80,7 @@ func Exec(root *cobra.Command) {
os.Exit(1)
}
command := metrics.GetCommand(os.Args[1:])
if command == "build" {
if command == "build" && !metrics.HasQuietFlag(os.Args[1:]) {
utils.DisplayScanSuggestMsg()
}
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.SuccessStatus)