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",
"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",
}

View File

@ -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)
}
}
}
}
}

View File

@ -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"},