Adding command aliases, was missing only “f” and “b” (from `build bake` and `build build`)

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-10-08 13:12:36 +02:00
parent 0d3f7186c5
commit 3e93a690d2
3 changed files with 68 additions and 34 deletions

View File

@ -28,31 +28,32 @@ var managementCommands = []string{
"registry", "registry",
"template", "template",
"cluster", "cluster",
"scan",
"app", "app",
"builder", "builder",
"buildx",
"imagetools", "imagetools",
"buildx",
"checkpoint", "checkpoint",
"config", "config",
"container", "container",
"context", "context",
"create",
"image", "image",
"manifest", "manifest",
"network", "network",
"node", "node",
"plugin", "plugin",
"scan",
"secret", "secret",
"service", "service",
"stack", "stack",
"swarm", "swarm",
"system", "system",
"trust",
"key", "key",
"signer", "signer",
"trust",
"volume", "volume",
"login", "login",
"create", "logout",
"compose", "compose",
} }
@ -62,7 +63,9 @@ var commands = []string{
"init", "init",
"inspect", "inspect",
"install", "install",
"deploy",
"list", "list",
"ls",
"merge", "merge",
"pull", "pull",
"push", "push",
@ -77,11 +80,13 @@ var commands = []string{
"prune", "prune",
"create", "create",
"bake", "bake",
"f",
"b",
"du", "du",
"ls",
"rm", "rm",
"stop", "stop",
"use", "use",
"remove",
"attach", "attach",
"commit", "commit",
"cp", "cp",
@ -90,6 +95,7 @@ var commands = []string{
"export", "export",
"kill", "kill",
"logs", "logs",
"ps",
"pause", "pause",
"port", "port",
"rename", "rename",
@ -101,10 +107,14 @@ var commands = []string{
"unpause", "unpause",
"update", "update",
"wait", "wait",
"aci",
"ecs",
"show", "show",
"history", "history",
"import", "import",
"load", "load",
"images",
"rmi",
"save", "save",
"tag", "tag",
"annotate", "annotate",
@ -112,13 +122,13 @@ var commands = []string{
"disconnect", "disconnect",
"demote", "demote",
"promote", "promote",
"ps",
"disable", "disable",
"enable", "enable",
"set", "set",
"rollback", "rollback",
"scale", "scale",
"deploy", "up",
"down",
"services", "services",
"ca", "ca",
"join", "join",
@ -131,18 +141,11 @@ var commands = []string{
"info", "info",
"generate", "generate",
"add", "add",
"remove",
"revoke", "revoke",
"sign", "sign",
"images",
"login", "login",
"logout",
"rmi",
"search",
"azure", "azure",
"aci", "logout",
"ecs", "search",
"convert", "convert",
"down",
"up",
} }

View File

@ -24,14 +24,12 @@ import (
"github.com/docker/compose-cli/utils" "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{} var commands = []string{}
func main() { func main() {
getCommands() getCommands()
getCommands("login")
getCommands("context", "create")
getCommands("compose") getCommands("compose")
fmt.Printf(` fmt.Printf(`
@ -45,10 +43,13 @@ var commands = []string{
`, strings.Join(managementCommands, "\", \n\t\""), strings.Join(commands, "\", \n\t\"")) `, strings.Join(managementCommands, "\", \n\t\""), strings.Join(commands, "\", \n\t\""))
} }
const (
mgtCommandsSection = "Management Commands:"
commandsSection = "Commands:"
aliasesSection = "Aliases:"
)
func getCommands(execCommands ...string) { func getCommands(execCommands ...string) {
if len(execCommands) > 0 {
managementCommands = append(managementCommands, execCommands[len(execCommands)-1])
}
withHelp := append(execCommands, "--help") withHelp := append(execCommands, "--help")
cmd := exec.Command("docker", withHelp...) cmd := exec.Command("docker", withHelp...)
output, err := cmd.Output() output, err := cmd.Output()
@ -57,33 +58,48 @@ func getCommands(execCommands ...string) {
} }
text := string(output) text := string(output)
lines := strings.Split(text, "\n") lines := strings.Split(text, "\n")
mgtCommandsStarted := false section := ""
commandsStarted := false
for _, line := range lines { for _, line := range lines {
trimmedLine := strings.TrimSpace(line) trimmedLine := strings.TrimSpace(line)
if strings.HasPrefix(trimmedLine, "Management Commands:") { if strings.HasPrefix(trimmedLine, mgtCommandsSection) {
mgtCommandsStarted = true section = mgtCommandsSection
continue continue
} }
if strings.HasPrefix(trimmedLine, "Commands:") || strings.HasPrefix(trimmedLine, "Available Commands:") { if strings.HasPrefix(trimmedLine, commandsSection) || strings.HasPrefix(trimmedLine, "Available Commands:") {
mgtCommandsStarted = false section = commandsSection
commandsStarted = true if len(execCommands) > 0 {
command := execCommands[len(execCommands)-1]
managementCommands = append(managementCommands, command)
}
continue
}
if strings.HasPrefix(trimmedLine, aliasesSection) {
section = aliasesSection
continue continue
} }
if trimmedLine == "" { if trimmedLine == "" {
mgtCommandsStarted = false section = ""
commandsStarted = false
continue continue
} }
tokens := strings.Split(trimmedLine, " ") tokens := strings.Split(trimmedLine, " ")
command := strings.Replace(tokens[0], "*", "", 1) command := strings.Replace(tokens[0], "*", "", 1)
if mgtCommandsStarted { switch section {
case mgtCommandsSection:
getCommands(append(execCommands, command)...) getCommands(append(execCommands, command)...)
} case commandsSection:
if commandsStarted {
if !utils.StringContains(commands, command) { if !utils.StringContains(commands, command) {
commands = append(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)
}
}
} }
} }
} }

View File

@ -68,6 +68,11 @@ func TestGetCommand(t *testing.T) {
args: []string{"login", "azure"}, args: []string{"login", "azure"},
expected: "login azure", expected: "login azure",
}, },
{
name: "azure logout",
args: []string{"logout", "azure"},
expected: "logout azure",
},
{ {
name: "azure login with flags", name: "azure login with flags",
args: []string{"login", "-u", "test", "azure"}, args: []string{"login", "-u", "test", "azure"},
@ -78,11 +83,21 @@ func TestGetCommand(t *testing.T) {
args: []string{"login", "myregistry"}, args: []string{"login", "myregistry"},
expected: "login", expected: "login",
}, },
{
name: "logout from a registry",
args: []string{"logout", "myregistry"},
expected: "logout",
},
{ {
name: "context create aci", name: "context create aci",
args: []string{"context", "create", "aci"}, args: []string{"context", "create", "aci"},
expected: "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", name: "create a context from another context",
args: []string{"context", "create", "test-context", "--from=default"}, args: []string{"context", "create", "test-context", "--from=default"},