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

View File

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

View File

@ -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:]