Fix CLI namespace commands (#8315)

* Fix namespace commands help

* Fix useless prediction for namespace commands

* Add namespace prediction capability
This commit is contained in:
Daniel Spangenberg 2020-02-09 19:38:39 +01:00 committed by GitHub
parent ef687a97a6
commit f7cfec47ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 10 deletions

View File

@ -128,6 +128,12 @@ func (b *BaseCommand) PredictVaultFolders() complete.Predictor {
return NewPredict().VaultFolders()
}
// PredictVaultNamespaces returns a predictor for "namespaces". See PredictVaultFiles
// for more information an restrictions.
func (b *BaseCommand) PredictVaultNamespaces() complete.Predictor {
return NewPredict().VaultNamespaces()
}
// PredictVaultMounts returns a predictor for "folders". See PredictVaultFiles
// for more information and restrictions.
func (b *BaseCommand) PredictVaultMounts() complete.Predictor {
@ -181,6 +187,13 @@ func (p *Predict) VaultFolders() complete.Predictor {
return p.vaultPaths(false)
}
// VaultNamespaces returns a predictor for Vault "namespaces". This is a public
// API for consumers, but you probably want BaseCommand.PredictVaultNamespaces
// instead.
func (p *Predict) VaultNamespaces() complete.Predictor {
return p.filterFunc(p.namespaces)
}
// VaultMounts returns a predictor for Vault "folders". This is a public
// API for consumers, but you probably want BaseCommand.PredictVaultMounts
// instead.
@ -428,6 +441,36 @@ func (p *Predict) mounts() []string {
return list
}
// namespaces returns a sorted list of the namespace paths for Vault server for
// which the client is configured to communicate with. This function returns
// an empty list in any error occurs.
func (p *Predict) namespaces() []string {
client := p.Client()
if client == nil {
return nil
}
secret, err := client.Logical().List("sys/namespaces")
if err != nil {
return nil
}
namespaces, ok := extractListData(secret)
if !ok {
return nil
}
list := make([]string, 0, len(namespaces))
for _, n := range namespaces {
s, ok := n.(string)
if !ok {
continue
}
list = append(list, s)
}
sort.Strings(list)
return list
}
// listPaths returns a list of paths (HTTP LIST) for the given path. This
// function returns an empty list of any errors occur.
func (p *Predict) listPaths(path string) []string {

View File

@ -45,7 +45,7 @@ func (c *NamespaceCreateCommand) Flags() *FlagSets {
}
func (c *NamespaceCreateCommand) AutocompleteArgs() complete.Predictor {
return c.PredictVaultFolders()
return complete.PredictNothing
}
func (c *NamespaceCreateCommand) AutocompleteFlags() complete.Flags {

View File

@ -33,7 +33,7 @@ Usage: vault namespace delete [options] PATH
Delete a namespace namespace from a parent namespace (e.g. ns1/ns2/):
$ vault namespace create -namespace=ns1 ns2
$ vault namespace delete -namespace=ns1 ns2
` + c.Flags().Help()
@ -45,7 +45,7 @@ func (c *NamespaceDeleteCommand) Flags() *FlagSets {
}
func (c *NamespaceDeleteCommand) AutocompleteArgs() complete.Predictor {
return c.PredictVaultFolders()
return c.PredictVaultNamespaces()
}
func (c *NamespaceDeleteCommand) AutocompleteFlags() complete.Flags {

View File

@ -39,7 +39,7 @@ func (c *NamespaceListCommand) Flags() *FlagSets {
}
func (c *NamespaceListCommand) AutocompleteArgs() complete.Predictor {
return c.PredictVaultFolders()
return complete.PredictNothing
}
func (c *NamespaceListCommand) AutocompleteFlags() complete.Flags {

View File

@ -21,11 +21,7 @@ func (c *NamespaceLookupCommand) Synopsis() string {
func (c *NamespaceLookupCommand) Help() string {
helpText := `
Usage: vault namespace create [options] PATH
Create a child namespace. The namespace created will be relative to the
namespace provided in either the VAULT_NAMESPACE environment variable or
-namespace CLI flag.
Usage: vault namespace lookup [options] PATH
Get information about the namespace of the locally authenticated token:
@ -45,7 +41,7 @@ func (c *NamespaceLookupCommand) Flags() *FlagSets {
}
func (c *NamespaceLookupCommand) AutocompleteArgs() complete.Predictor {
return c.PredictVaultFolders()
return c.PredictVaultNamespaces()
}
func (c *NamespaceLookupCommand) AutocompleteFlags() complete.Flags {