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:
@@ -32,6 +32,7 @@ import (
|
|||||||
emptyGAPI "github.com/purpleidea/mgmt/gapi/empty"
|
emptyGAPI "github.com/purpleidea/mgmt/gapi/empty"
|
||||||
langGAPI "github.com/purpleidea/mgmt/lang/gapi"
|
langGAPI "github.com/purpleidea/mgmt/lang/gapi"
|
||||||
"github.com/purpleidea/mgmt/lib"
|
"github.com/purpleidea/mgmt/lib"
|
||||||
|
"github.com/purpleidea/mgmt/util"
|
||||||
"github.com/purpleidea/mgmt/util/errwrap"
|
"github.com/purpleidea/mgmt/util/errwrap"
|
||||||
yamlGAPI "github.com/purpleidea/mgmt/yamlgraph"
|
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
|
// 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 {
|
if l := len(obj.Seeds); name == "" && l > 1 {
|
||||||
elem := obj.Seeds[l-2] // second to last element
|
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
|
return false, cliUtil.CliParseError(cliUtil.MissingEquals) // consistent errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,10 +129,11 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
|
|||||||
obj.AdvertiseClientURLs,
|
obj.AdvertiseClientURLs,
|
||||||
obj.AdvertiseServerURLs,
|
obj.AdvertiseServerURLs,
|
||||||
}
|
}
|
||||||
|
gapiNames := gapi.Names() // list of registered names
|
||||||
for _, list := range lists {
|
for _, list := range lists {
|
||||||
if l := len(list); name == "" && l > 1 {
|
if l := len(list); name == "" && l > 1 {
|
||||||
elem := list[l-2] // second to last element
|
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
|
return false, cliUtil.CliParseError(cliUtil.MissingEquals) // consistent errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,15 @@ func Register(name string, fn func() GAPI) {
|
|||||||
RegisteredGAPIs[name] = fn
|
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
|
// 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
|
// 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
|
// data in the parent command struct, so instead, the data that the parent wants
|
||||||
|
|||||||
Reference in New Issue
Block a user