github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/namespace_list.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/hashicorp/nomad/api" 9 "github.com/posener/complete" 10 ) 11 12 type NamespaceListCommand struct { 13 Meta 14 } 15 16 func (c *NamespaceListCommand) Help() string { 17 helpText := ` 18 Usage: nomad namespace list [options] 19 20 List is used to list available namespaces. 21 22 If ACLs are enabled, this command requires a management ACL token to view 23 all namespaces. A non-management token can be used to list namespaces for 24 which it has an associated capability. 25 26 General Options: 27 28 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` 29 30 List Options: 31 32 -json 33 Output the namespaces in a JSON format. 34 35 -t 36 Format and display the namespaces using a Go template. 37 ` 38 return strings.TrimSpace(helpText) 39 } 40 41 func (c *NamespaceListCommand) AutocompleteFlags() complete.Flags { 42 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 43 complete.Flags{ 44 "-json": complete.PredictNothing, 45 "-t": complete.PredictAnything, 46 }) 47 } 48 49 func (c *NamespaceListCommand) AutocompleteArgs() complete.Predictor { 50 return complete.PredictNothing 51 } 52 53 func (c *NamespaceListCommand) Synopsis() string { 54 return "List namespaces" 55 } 56 57 func (c *NamespaceListCommand) Name() string { return "namespace list" } 58 59 func (c *NamespaceListCommand) Run(args []string) int { 60 var json bool 61 var tmpl string 62 63 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 64 flags.Usage = func() { c.Ui.Output(c.Help()) } 65 flags.BoolVar(&json, "json", false, "") 66 flags.StringVar(&tmpl, "t", "", "") 67 68 if err := flags.Parse(args); err != nil { 69 return 1 70 } 71 72 // Check that we got no arguments 73 args = flags.Args() 74 if l := len(args); l != 0 { 75 c.Ui.Error("This command takes no arguments") 76 c.Ui.Error(commandErrorText(c)) 77 return 1 78 } 79 80 // Get the HTTP client 81 client, err := c.Meta.Client() 82 if err != nil { 83 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 84 return 1 85 } 86 87 namespaces, _, err := client.Namespaces().List(nil) 88 if err != nil { 89 c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err)) 90 return 1 91 } 92 93 if json || len(tmpl) > 0 { 94 out, err := Format(json, tmpl, namespaces) 95 if err != nil { 96 c.Ui.Error(err.Error()) 97 return 1 98 } 99 100 c.Ui.Output(out) 101 return 0 102 } 103 104 c.Ui.Output(formatNamespaces(namespaces)) 105 return 0 106 } 107 108 func formatNamespaces(namespaces []*api.Namespace) string { 109 if len(namespaces) == 0 { 110 return "No namespaces found" 111 } 112 113 // Sort the output by namespace name 114 sort.Slice(namespaces, func(i, j int) bool { return namespaces[i].Name < namespaces[j].Name }) 115 116 rows := make([]string, len(namespaces)+1) 117 rows[0] = "Name|Description" 118 for i, ns := range namespaces { 119 rows[i+1] = fmt.Sprintf("%s|%s", 120 ns.Name, 121 ns.Description) 122 } 123 return formatList(rows) 124 }