Browse Source

Metrics for azure commands

* docker context create aci
* docker login azure
Djordje Lukic 5 years ago
parent
commit
188dc1b06c
2 changed files with 86 additions and 5 deletions
  1. 50 0
      metrics/metics_test.go
  2. 36 5
      metrics/metrics.go

+ 50 - 0
metrics/metics_test.go

@@ -83,6 +83,56 @@ func TestFlag(t *testing.T) {
 			args:     []string{"image", "ls", "-q"},
 			args:     []string{"image", "ls", "-q"},
 			expected: "image ls",
 			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 {
 	for _, testCase := range testCases {

+ 36 - 5
metrics/metrics.go

@@ -28,13 +28,20 @@ var managementCommands = []string{
 	"builder",
 	"builder",
 	"buildx",
 	"buildx",
 	"ecs",
 	"ecs",
+	"ecs compose",
 	"cluster",
 	"cluster",
 	"compose",
 	"compose",
 	"config",
 	"config",
 	"container",
 	"container",
 	"context",
 	"context",
+	// We add "context create" as a management command to be able to catch
+	// calls to "context create aci"
+	"context create",
 	"help",
 	"help",
 	"image",
 	"image",
+	// Adding "login" as a management command so that the system can catch
+	// commands like `docker login azure`
+	"login",
 	"manifest",
 	"manifest",
 	"network",
 	"network",
 	"node",
 	"node",
@@ -50,6 +57,19 @@ var managementCommands = []string{
 	"volume",
 	"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 (
 const (
 	scanCommand = "scan"
 	scanCommand = "scan"
 )
 )
@@ -91,9 +111,8 @@ func getCommand(args []string, flags *flag.FlagSet) string {
 		}
 		}
 
 
 		for {
 		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
 					command += " " + sub
 					strippedArgs = strippedArgs[1:]
 					strippedArgs = strippedArgs[1:]
 					continue
 					continue
@@ -120,10 +139,22 @@ func getScanCommand(args []string) string {
 	return command
 	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 args[0]
 	}
 	}
+
 	return ""
 	return ""
 }
 }