diff --git a/metrics/metics_test.go b/metrics/metics_test.go index d12e420ca..174b2cab1 100644 --- a/metrics/metics_test.go +++ b/metrics/metics_test.go @@ -95,8 +95,6 @@ func TestFlag(t *testing.T) { func TestEcs(t *testing.T) { root := &cobra.Command{} - root.PersistentFlags().BoolP("debug", "d", false, "debug") - root.PersistentFlags().String("str", "str", "str") testCases := []struct { name string @@ -167,3 +165,51 @@ func TestEcs(t *testing.T) { }) } } + +func TestScan(t *testing.T) { + root := &cobra.Command{} + + testCases := []struct { + name string + args []string + expected string + }{ + { + name: "scan", + args: []string{"scan"}, + expected: "scan", + }, + { + name: "scan image with long flags", + args: []string{"scan", "--file", "file", "image"}, + expected: "scan", + }, + { + name: "scan image with short flags", + args: []string{"scan", "-f", "file", "image"}, + expected: "scan", + }, + { + name: "scan with long flag", + args: []string{"scan", "--dependency-tree", "image"}, + expected: "scan", + }, + { + name: "auth", + args: []string{"scan", "--auth"}, + expected: "scan auth", + }, + { + name: "version", + args: []string{"scan", "--version"}, + expected: "scan version", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + result := getCommand(testCase.args, root.PersistentFlags()) + assert.Equal(t, testCase.expected, result) + }) + } +} diff --git a/metrics/metrics.go b/metrics/metrics.go index ba2bd1caa..25bbcac4c 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -50,6 +50,10 @@ var managementCommands = []string{ "volume", } +const ( + scanCommand = "scan" +) + // Track sends the tracking analytics to Docker Desktop func Track(context string, args []string, flags *flag.FlagSet) { // Fire and forget, we don't want to slow down the user waiting for DD @@ -71,16 +75,21 @@ func Track(context string, args []string, flags *flag.FlagSet) { func getCommand(args []string, flags *flag.FlagSet) string { command := "" - args = stripFlags(args, flags) + strippedArgs := stripFlags(args, flags) + + if len(strippedArgs) != 0 { + command = strippedArgs[0] + + if command == scanCommand { + return getScanCommand(args) + } - if len(args) != 0 { - command = args[0] for { - currentCommand := args[0] + currentCommand := strippedArgs[0] if contains(managementCommands, currentCommand) { - if sub := getSubCommand(args[1:]); sub != "" { + if sub := getSubCommand(strippedArgs[1:]); sub != "" { command += " " + sub - args = args[1:] + strippedArgs = strippedArgs[1:] continue } } @@ -91,6 +100,20 @@ func getCommand(args []string, flags *flag.FlagSet) string { return command } +func getScanCommand(args []string) string { + command := args[0] + + if contains(args, "--auth") { + return command + " auth" + } + + if contains(args, "--version") { + return command + " version" + } + + return command +} + func getSubCommand(args []string) string { if len(args) != 0 && isArg(args[0]) { return args[0]