Merge pull request #342 from docker/feat-aci-metrics

Metrics for azure commands
This commit is contained in:
Guillaume Tardif 2020-07-06 13:07:01 +02:00 committed by GitHub
commit 2c981f14b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 5 deletions

View File

@ -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 {

View File

@ -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,13 +139,25 @@ 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 ""
}
func contains(array []string, needle string) bool {
for _, val := range array {
if val == needle {