gapi: Add a new Names API

This gets the list more directly. We could also just lookup a key in the
big map to get the same effect, but this is more logical for now. This
is useful for removing the importing of three packages.
This commit is contained in:
James Shubin
2024-03-03 15:45:25 -05:00
parent 589a5f9aeb
commit a65c87b584
3 changed files with 14 additions and 2 deletions

View File

@@ -32,6 +32,7 @@ import (
emptyGAPI "github.com/purpleidea/mgmt/gapi/empty"
langGAPI "github.com/purpleidea/mgmt/lang/gapi"
"github.com/purpleidea/mgmt/lib"
"github.com/purpleidea/mgmt/util"
"github.com/purpleidea/mgmt/util/errwrap"
yamlGAPI "github.com/purpleidea/mgmt/yamlgraph"
@@ -80,9 +81,10 @@ func (obj *DeployArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error
}
// XXX: workaround https://github.com/alexflint/go-arg/issues/239
gapiNames := gapi.Names() // list of registered names
if l := len(obj.Seeds); name == "" && l > 1 {
elem := obj.Seeds[l-2] // second to last element
if elem == emptyGAPI.Name || elem == langGAPI.Name || elem == yamlGAPI.Name {
if util.StrInList(elem, gapiNames) {
return false, cliUtil.CliParseError(cliUtil.MissingEquals) // consistent errors
}
}

View File

@@ -129,10 +129,11 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
obj.AdvertiseClientURLs,
obj.AdvertiseServerURLs,
}
gapiNames := gapi.Names() // list of registered names
for _, list := range lists {
if l := len(list); name == "" && l > 1 {
elem := list[l-2] // second to last element
if elem == emptyGAPI.Name || elem == langGAPI.Name || elem == yamlGAPI.Name {
if util.StrInList(elem, gapiNames) {
return false, cliUtil.CliParseError(cliUtil.MissingEquals) // consistent errors
}
}

View File

@@ -41,6 +41,15 @@ func Register(name string, fn func() GAPI) {
RegisteredGAPIs[name] = fn
}
// Names returns a list of the names of all registered GAPIs.
func Names() []string {
names := []string{}
for name := range RegisteredGAPIs {
names = append(names, name)
}
return names
}
// Flags is some common data that comes from a higher-level command, and is used
// by a subcommand. By type circularity, the subcommands can't easily access the
// data in the parent command struct, so instead, the data that the parent wants