2017-09-07 23:56:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-19 14:47:10 +00:00
|
|
|
"sort"
|
2017-09-07 23:56:15 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NamespaceListCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceListCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad namespace list [options]
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
List is used to list available namespaces.
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this command requires a management ACL token to view
|
|
|
|
all namespaces. A non-management token can be used to list namespaces for
|
|
|
|
which it has an associated capability.
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2017-09-07 23:56:15 +00:00
|
|
|
|
|
|
|
List Options:
|
|
|
|
|
|
|
|
-json
|
|
|
|
Output the namespaces in a JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display the namespaces using a Go template.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceListCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceListCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceListCommand) Synopsis() string {
|
|
|
|
return "List namespaces"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *NamespaceListCommand) Name() string { return "namespace list" }
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
func (c *NamespaceListCommand) Run(args []string) int {
|
|
|
|
var json bool
|
|
|
|
var tmpl string
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2017-09-07 23:56:15 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got no arguments
|
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l != 0 {
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error("This command takes no arguments")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
2017-09-07 23:56:15 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaces, _, err := client.Namespaces().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, namespaces)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(formatNamespaces(namespaces))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatNamespaces(namespaces []*api.Namespace) string {
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
return "No namespaces found"
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// Sort the output by namespace name
|
|
|
|
sort.Slice(namespaces, func(i, j int) bool { return namespaces[i].Name < namespaces[j].Name })
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
rows := make([]string, len(namespaces)+1)
|
|
|
|
rows[0] = "Name|Description"
|
|
|
|
for i, ns := range namespaces {
|
|
|
|
rows[i+1] = fmt.Sprintf("%s|%s",
|
|
|
|
ns.Name,
|
|
|
|
ns.Description)
|
|
|
|
}
|
|
|
|
return formatList(rows)
|
|
|
|
}
|