github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/namespace_inspect.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/posener/complete" 8 ) 9 10 type NamespaceInspectCommand struct { 11 Meta 12 } 13 14 func (c *NamespaceInspectCommand) Help() string { 15 helpText := ` 16 Usage: nomad namespace inspect [options] <namespace> 17 18 Inspect is used to view raw information about a particular namespace. 19 20 If ACLs are enabled, this command requires a management ACL token or a token 21 that has a capability associated with the namespace. 22 23 General Options: 24 25 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` 26 27 Inspect Options: 28 29 -t 30 Format and display the namespaces using a Go template. 31 ` 32 33 return strings.TrimSpace(helpText) 34 } 35 36 func (c *NamespaceInspectCommand) AutocompleteFlags() complete.Flags { 37 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 38 complete.Flags{ 39 "-t": complete.PredictAnything, 40 }) 41 } 42 43 func (c *NamespaceInspectCommand) AutocompleteArgs() complete.Predictor { 44 return NamespacePredictor(c.Meta.Client, nil) 45 } 46 47 func (c *NamespaceInspectCommand) Synopsis() string { 48 return "Inspect a namespace" 49 } 50 51 func (c *NamespaceInspectCommand) Name() string { return "namespace inspect" } 52 53 func (c *NamespaceInspectCommand) Run(args []string) int { 54 var tmpl string 55 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 56 flags.Usage = func() { c.Ui.Output(c.Help()) } 57 flags.StringVar(&tmpl, "t", "", "") 58 59 if err := flags.Parse(args); err != nil { 60 return 1 61 } 62 63 // Check that we got one arguments 64 args = flags.Args() 65 if l := len(args); l != 1 { 66 c.Ui.Error("This command takes one argument: <namespace>") 67 c.Ui.Error(commandErrorText(c)) 68 return 1 69 } 70 71 name := args[0] 72 73 // Get the HTTP client 74 client, err := c.Meta.Client() 75 if err != nil { 76 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 77 return 1 78 } 79 80 // Do a prefix lookup 81 ns, possible, err := getNamespace(client.Namespaces(), name) 82 if err != nil { 83 c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err)) 84 return 1 85 } 86 87 if len(possible) != 0 { 88 c.Ui.Error(fmt.Sprintf("Prefix matched multiple namespaces\n\n%s", formatNamespaces(possible))) 89 return 1 90 } 91 92 out, err := Format(len(tmpl) == 0, tmpl, ns) 93 if err != nil { 94 c.Ui.Error(err.Error()) 95 return 1 96 } 97 98 c.Ui.Output(out) 99 return 0 100 }