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