diff --git a/metrics/metics_test.go b/metrics/metics_test.go index 174b2cab1..243ea168b 100644 --- a/metrics/metics_test.go +++ b/metrics/metics_test.go @@ -83,6 +83,56 @@ func TestFlag(t *testing.T) { args: []string{"image", "ls", "-q"}, expected: "image ls", }, + { + name: "azure login", + args: []string{"login", "azure"}, + expected: "login azure", + }, + { + name: "azure login with flags", + args: []string{"login", "-u", "test", "azure"}, + expected: "login azure", + }, + { + name: "azure login with azure user", + args: []string{"login", "-u", "azure"}, + expected: "login", + }, + { + name: "login to a registry", + args: []string{"login", "registry"}, + expected: "login", + }, + { + name: "context create aci", + args: []string{"context", "create", "aci"}, + expected: "context create aci", + }, + { + name: "create a context from another context", + args: []string{"context", "create", "test-context", "--from=default"}, + expected: "context create", + }, + { + name: "create a container", + args: []string{"create"}, + expected: "create", + }, + { + name: "create a container named aci", + args: []string{"create", "aci"}, + expected: "create", + }, + { + name: "create a container named test-container", + args: []string{"create", "test-container"}, + expected: "create", + }, + { + name: "create with flags", + args: []string{"create", "--rm", "test"}, + expected: "create", + }, } for _, testCase := range testCases { diff --git a/metrics/metrics.go b/metrics/metrics.go index d6348b97b..e201f92f7 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -28,13 +28,20 @@ var managementCommands = []string{ "builder", "buildx", "ecs", + "ecs compose", "cluster", "compose", "config", "container", "context", + // We add "context create" as a management command to be able to catch + // calls to "context create aci" + "context create", "help", "image", + // Adding "login" as a management command so that the system can catch + // commands like `docker login azure` + "login", "manifest", "network", "node", @@ -50,6 +57,19 @@ var managementCommands = []string{ "volume", } +// managementSubCommands holds a list of allowed subcommands of a management +// command. For example we want to send an event for "docker login azure" but +// we don't wat to send the name of the registry when the user does a +// "docker login my-registry", we only want to send "login" +var managementSubCommands = map[string][]string{ + "login": { + "azure", + }, + "context create": { + "aci", + }, +} + const ( scanCommand = "scan" ) @@ -91,9 +111,8 @@ func getCommand(args []string, flags *flag.FlagSet) string { } for { - currentCommand := strippedArgs[0] - if contains(managementCommands, currentCommand) { - if sub := getSubCommand(strippedArgs[1:]); sub != "" { + if contains(managementCommands, command) { + if sub := getSubCommand(command, strippedArgs[1:]); sub != "" { command += " " + sub strippedArgs = strippedArgs[1:] continue @@ -120,10 +139,22 @@ func getScanCommand(args []string) string { return command } -func getSubCommand(args []string) string { - if len(args) != 0 && isArg(args[0]) { +func getSubCommand(command string, args []string) string { + if len(args) == 0 { + return "" + } + + if val, ok := managementSubCommands[command]; ok { + if contains(val, args[0]) { + return args[0] + } + return "" + } + + if isArg(args[0]) { return args[0] } + return "" }