diff --git a/command/base_predict.go b/command/base_predict.go index c7b306118a..5775cdfee5 100644 --- a/command/base_predict.go +++ b/command/base_predict.go @@ -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 { diff --git a/command/namespace_create.go b/command/namespace_create.go index 7fdff88d5a..9937ee0489 100644 --- a/command/namespace_create.go +++ b/command/namespace_create.go @@ -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 { diff --git a/command/namespace_delete.go b/command/namespace_delete.go index f0614adb81..7061b64602 100644 --- a/command/namespace_delete.go +++ b/command/namespace_delete.go @@ -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 { diff --git a/command/namespace_list.go b/command/namespace_list.go index 192e3bacb6..e5352f5077 100644 --- a/command/namespace_list.go +++ b/command/namespace_list.go @@ -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 { diff --git a/command/namespace_lookup.go b/command/namespace_lookup.go index 73f3ddc8df..718d0452da 100644 --- a/command/namespace_lookup.go +++ b/command/namespace_lookup.go @@ -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 {