From 3e93a690d2a9e9a530767f6370b0ecf87349cc7f Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Thu, 8 Oct 2020 13:12:36 +0200 Subject: [PATCH] =?UTF-8?q?Adding=20command=20aliases,=20was=20missing=20o?= =?UTF-8?q?nly=20=E2=80=9Cf=E2=80=9D=20and=20=E2=80=9Cb=E2=80=9D=20(from?= =?UTF-8?q?=20`build=20bake`=20and=20`build=20build`)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Guillaume Tardif --- metrics/commands.go | 35 +++++++++++---------- metrics/generatecommands/main.go | 52 +++++++++++++++++++++----------- metrics/metrics_test.go | 15 +++++++++ 3 files changed, 68 insertions(+), 34 deletions(-) diff --git a/metrics/commands.go b/metrics/commands.go index 1350de16e..b9a13a08b 100644 --- a/metrics/commands.go +++ b/metrics/commands.go @@ -28,31 +28,32 @@ var managementCommands = []string{ "registry", "template", "cluster", + "scan", "app", "builder", - "buildx", "imagetools", + "buildx", "checkpoint", "config", "container", "context", + "create", "image", "manifest", "network", "node", "plugin", - "scan", "secret", "service", "stack", "swarm", "system", - "trust", "key", "signer", + "trust", "volume", "login", - "create", + "logout", "compose", } @@ -62,7 +63,9 @@ var commands = []string{ "init", "inspect", "install", + "deploy", "list", + "ls", "merge", "pull", "push", @@ -77,11 +80,13 @@ var commands = []string{ "prune", "create", "bake", + "f", + "b", "du", - "ls", "rm", "stop", "use", + "remove", "attach", "commit", "cp", @@ -90,6 +95,7 @@ var commands = []string{ "export", "kill", "logs", + "ps", "pause", "port", "rename", @@ -101,10 +107,14 @@ var commands = []string{ "unpause", "update", "wait", + "aci", + "ecs", "show", "history", "import", "load", + "images", + "rmi", "save", "tag", "annotate", @@ -112,13 +122,13 @@ var commands = []string{ "disconnect", "demote", "promote", - "ps", "disable", "enable", "set", "rollback", "scale", - "deploy", + "up", + "down", "services", "ca", "join", @@ -131,18 +141,11 @@ var commands = []string{ "info", "generate", "add", - "remove", "revoke", "sign", - "images", "login", - "logout", - "rmi", - "search", "azure", - "aci", - "ecs", + "logout", + "search", "convert", - "down", - "up", } diff --git a/metrics/generatecommands/main.go b/metrics/generatecommands/main.go index d4c57b5cf..494b5875f 100644 --- a/metrics/generatecommands/main.go +++ b/metrics/generatecommands/main.go @@ -24,14 +24,12 @@ import ( "github.com/docker/compose-cli/utils" ) -var managementCommands = []string{"ecs", "assemble", "registry", "template", "cluster"} +var managementCommands = []string{"ecs", "assemble", "registry", "template", "cluster", "scan"} var commands = []string{} func main() { getCommands() - getCommands("login") - getCommands("context", "create") getCommands("compose") fmt.Printf(` @@ -45,10 +43,13 @@ var commands = []string{ `, strings.Join(managementCommands, "\", \n\t\""), strings.Join(commands, "\", \n\t\"")) } +const ( + mgtCommandsSection = "Management Commands:" + commandsSection = "Commands:" + aliasesSection = "Aliases:" +) + func getCommands(execCommands ...string) { - if len(execCommands) > 0 { - managementCommands = append(managementCommands, execCommands[len(execCommands)-1]) - } withHelp := append(execCommands, "--help") cmd := exec.Command("docker", withHelp...) output, err := cmd.Output() @@ -57,33 +58,48 @@ func getCommands(execCommands ...string) { } text := string(output) lines := strings.Split(text, "\n") - mgtCommandsStarted := false - commandsStarted := false + section := "" for _, line := range lines { trimmedLine := strings.TrimSpace(line) - if strings.HasPrefix(trimmedLine, "Management Commands:") { - mgtCommandsStarted = true + if strings.HasPrefix(trimmedLine, mgtCommandsSection) { + section = mgtCommandsSection continue } - if strings.HasPrefix(trimmedLine, "Commands:") || strings.HasPrefix(trimmedLine, "Available Commands:") { - mgtCommandsStarted = false - commandsStarted = true + if strings.HasPrefix(trimmedLine, commandsSection) || strings.HasPrefix(trimmedLine, "Available Commands:") { + section = commandsSection + if len(execCommands) > 0 { + command := execCommands[len(execCommands)-1] + managementCommands = append(managementCommands, command) + } + continue + } + if strings.HasPrefix(trimmedLine, aliasesSection) { + section = aliasesSection continue } if trimmedLine == "" { - mgtCommandsStarted = false - commandsStarted = false + section = "" continue } + tokens := strings.Split(trimmedLine, " ") command := strings.Replace(tokens[0], "*", "", 1) - if mgtCommandsStarted { + switch section { + case mgtCommandsSection: getCommands(append(execCommands, command)...) - } - if commandsStarted { + case commandsSection: if !utils.StringContains(commands, command) { commands = append(commands, command) } + getCommands(append(execCommands, command)...) + case aliasesSection: + aliases := strings.Split(trimmedLine, ",") + for _, alias := range aliases { + trimmedAlias := strings.TrimSpace(alias) + if !utils.StringContains(commands, trimmedAlias) { + commands = append(commands, trimmedAlias) + } + } } } } diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 4e19df738..12846baf1 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -68,6 +68,11 @@ func TestGetCommand(t *testing.T) { args: []string{"login", "azure"}, expected: "login azure", }, + { + name: "azure logout", + args: []string{"logout", "azure"}, + expected: "logout azure", + }, { name: "azure login with flags", args: []string{"login", "-u", "test", "azure"}, @@ -78,11 +83,21 @@ func TestGetCommand(t *testing.T) { args: []string{"login", "myregistry"}, expected: "login", }, + { + name: "logout from a registry", + args: []string{"logout", "myregistry"}, + expected: "logout", + }, { name: "context create aci", args: []string{"context", "create", "aci"}, expected: "context create aci", }, + { + name: "context create ecs", + args: []string{"context", "create", "ecs"}, + expected: "context create ecs", + }, { name: "create a context from another context", args: []string{"context", "create", "test-context", "--from=default"},