Pluralize 'host(s)' subcommands

This commit is contained in:
Brian Picciano 2024-07-22 15:52:51 +02:00
parent ca62a37692
commit d31be8455b
3 changed files with 22 additions and 12 deletions

View File

@ -11,7 +11,7 @@ import (
"sort" "sort"
) )
var subCmdHostsCreate = subCmd{ var subCmdHostCreate = subCmd{
name: "create", name: "create",
descr: "Creates a new host in the network, writing its new bootstrap.json to stdout", descr: "Creates a new host in the network, writing its new bootstrap.json to stdout",
do: func(subCmdCtx subCmdCtx) error { do: func(subCmdCtx subCmdCtx) error {
@ -64,7 +64,7 @@ var subCmdHostsCreate = subCmd{
}, },
} }
var subCmdHostsList = subCmd{ var subCmdHostList = subCmd{
name: "list", name: "list",
descr: "Lists all hosts in the network, and their IPs", descr: "Lists all hosts in the network, and their IPs",
do: func(subCmdCtx subCmdCtx) error { do: func(subCmdCtx subCmdCtx) error {
@ -100,7 +100,7 @@ var subCmdHostsList = subCmd{
}, },
} }
var subCmdHostsRemove = subCmd{ var subCmdHostRemove = subCmd{
name: "remove", name: "remove",
descr: "Removes a host from the network", descr: "Removes a host from the network",
do: func(subCmdCtx subCmdCtx) error { do: func(subCmdCtx subCmdCtx) error {
@ -136,14 +136,15 @@ var subCmdHostsRemove = subCmd{
}, },
} }
var subCmdHosts = subCmd{ var subCmdHost = subCmd{
name: "hosts", name: "host",
plural: "s",
descr: "Sub-commands having to do with configuration of hosts in the network", descr: "Sub-commands having to do with configuration of hosts in the network",
do: func(subCmdCtx subCmdCtx) error { do: func(subCmdCtx subCmdCtx) error {
return subCmdCtx.doSubCmd( return subCmdCtx.doSubCmd(
subCmdHostsCreate, subCmdHostCreate,
subCmdHostsRemove, subCmdHostRemove,
subCmdHostsList, subCmdHostList,
) )
}, },
} }

View File

@ -63,7 +63,7 @@ func main() {
}.doSubCmd( }.doSubCmd(
subCmdDaemon, subCmdDaemon,
subCmdGarage, subCmdGarage,
subCmdHosts, subCmdHost,
subCmdNebula, subCmdNebula,
subCmdNetwork, subCmdNetwork,
subCmdVersion, subCmdVersion,

View File

@ -27,6 +27,9 @@ type subCmd struct {
name string name string
descr string descr string
do func(subCmdCtx) error 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 { func (ctx subCmdCtx) usagePrefix() string {
@ -78,7 +81,11 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error {
fmt.Fprintf(os.Stderr, "\nSUB-COMMANDS:\n\n") fmt.Fprintf(os.Stderr, "\nSUB-COMMANDS:\n\n")
for _, subCmd := range subCmds { 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") fmt.Fprintf(os.Stderr, "\n")
@ -94,8 +101,10 @@ func (ctx subCmdCtx) doSubCmd(subCmds ...subCmd) error {
subCmdsMap := map[string]subCmd{} subCmdsMap := map[string]subCmd{}
for _, subCmd := range subCmds { for _, subCmd := range subCmds {
// TODO allow subCmd(s) in some cases
subCmdsMap[subCmd.name] = subCmd subCmdsMap[subCmd.name] = subCmd
if subCmd.plural != "" {
subCmdsMap[subCmd.name+subCmd.plural] = subCmd
}
} }
subCmdName, args := args[0], args[1:] subCmdName, args := args[0], args[1:]