From d31be8455b725e067313d36a7799fb6d4a7fd2c1 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Mon, 22 Jul 2024 15:52:51 +0200 Subject: [PATCH] Pluralize 'host(s)' subcommands --- go/cmd/entrypoint/{hosts.go => host.go} | 19 ++++++++++--------- go/cmd/entrypoint/main.go | 2 +- go/cmd/entrypoint/sub_cmd.go | 13 +++++++++++-- 3 files changed, 22 insertions(+), 12 deletions(-) rename go/cmd/entrypoint/{hosts.go => host.go} (90%) diff --git a/go/cmd/entrypoint/hosts.go b/go/cmd/entrypoint/host.go similarity index 90% rename from go/cmd/entrypoint/hosts.go rename to go/cmd/entrypoint/host.go index 340b1ba..fe52c6e 100644 --- a/go/cmd/entrypoint/hosts.go +++ b/go/cmd/entrypoint/host.go @@ -11,7 +11,7 @@ import ( "sort" ) -var subCmdHostsCreate = subCmd{ +var subCmdHostCreate = subCmd{ name: "create", descr: "Creates a new host in the network, writing its new bootstrap.json to stdout", do: func(subCmdCtx subCmdCtx) error { @@ -64,7 +64,7 @@ var subCmdHostsCreate = subCmd{ }, } -var subCmdHostsList = subCmd{ +var subCmdHostList = subCmd{ name: "list", descr: "Lists all hosts in the network, and their IPs", do: func(subCmdCtx subCmdCtx) error { @@ -100,7 +100,7 @@ var subCmdHostsList = subCmd{ }, } -var subCmdHostsRemove = subCmd{ +var subCmdHostRemove = subCmd{ name: "remove", descr: "Removes a host from the network", do: func(subCmdCtx subCmdCtx) error { @@ -136,14 +136,15 @@ var subCmdHostsRemove = subCmd{ }, } -var subCmdHosts = subCmd{ - name: "hosts", - descr: "Sub-commands having to do with configuration of hosts in the network", +var subCmdHost = subCmd{ + name: "host", + plural: "s", + descr: "Sub-commands having to do with configuration of hosts in the network", do: func(subCmdCtx subCmdCtx) error { return subCmdCtx.doSubCmd( - subCmdHostsCreate, - subCmdHostsRemove, - subCmdHostsList, + subCmdHostCreate, + subCmdHostRemove, + subCmdHostList, ) }, } diff --git a/go/cmd/entrypoint/main.go b/go/cmd/entrypoint/main.go index 22e82d4..09d5ee6 100644 --- a/go/cmd/entrypoint/main.go +++ b/go/cmd/entrypoint/main.go @@ -63,7 +63,7 @@ func main() { }.doSubCmd( subCmdDaemon, subCmdGarage, - subCmdHosts, + subCmdHost, subCmdNebula, subCmdNetwork, subCmdVersion, diff --git a/go/cmd/entrypoint/sub_cmd.go b/go/cmd/entrypoint/sub_cmd.go index 693bda3..ce27c47 100644 --- a/go/cmd/entrypoint/sub_cmd.go +++ b/go/cmd/entrypoint/sub_cmd.go @@ -27,6 +27,9 @@ type subCmd struct { name string descr string do func(subCmdCtx) error + + // If set then the name will be allowed to be suffixed with this string. + plural string } func (ctx subCmdCtx) usagePrefix() string { @@ -78,7 +81,11 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error { fmt.Fprintf(os.Stderr, "\nSUB-COMMANDS:\n\n") for _, subCmd := range subCmds { - fmt.Fprintf(os.Stderr, " %s\t%s\n", subCmd.name, subCmd.descr) + name := subCmd.name + if subCmd.plural != "" { + name += "(" + subCmd.plural + ")" + } + fmt.Fprintf(os.Stderr, " %s\t%s\n", name, subCmd.descr) } fmt.Fprintf(os.Stderr, "\n") @@ -94,8 +101,10 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error { subCmdsMap := map[string]subCmd{} for _, subCmd := range subCmds { - // TODO allow subCmd(s) in some cases subCmdsMap[subCmd.name] = subCmd + if subCmd.plural != "" { + subCmdsMap[subCmd.name+subCmd.plural] = subCmd + } } subCmdName, args := args[0], args[1:]