github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/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 General Options: 21 22 ` + generalOptionsUsage() + ` 23 24 Inspect Options: 25 26 -t 27 Format and display the namespaces using a Go template. 28 ` 29 30 return strings.TrimSpace(helpText) 31 } 32 33 func (c *NamespaceInspectCommand) AutocompleteFlags() complete.Flags { 34 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 35 complete.Flags{ 36 "-t": complete.PredictAnything, 37 }) 38 } 39 40 func (c *NamespaceInspectCommand) AutocompleteArgs() complete.Predictor { 41 return NamespacePredictor(c.Meta.Client, nil) 42 } 43 44 func (c *NamespaceInspectCommand) Synopsis() string { 45 return "Inspect a namespace" 46 } 47 48 func (c *NamespaceInspectCommand) Name() string { return "namespace inspect" } 49 50 func (c *NamespaceInspectCommand) Run(args []string) int { 51 var tmpl string 52 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 53 flags.Usage = func() { c.Ui.Output(c.Help()) } 54 flags.StringVar(&tmpl, "t", "", "") 55 56 if err := flags.Parse(args); err != nil { 57 return 1 58 } 59 60 // Check that we got one arguments 61 args = flags.Args() 62 if l := len(args); l != 1 { 63 c.Ui.Error("This command takes one argument: <namespace>") 64 c.Ui.Error(commandErrorText(c)) 65 return 1 66 } 67 68 name := args[0] 69 70 // Get the HTTP client 71 client, err := c.Meta.Client() 72 if err != nil { 73 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 74 return 1 75 } 76 77 // Do a prefix lookup 78 ns, possible, err := getNamespace(client.Namespaces(), name) 79 if err != nil { 80 c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err)) 81 return 1 82 } 83 84 if len(possible) != 0 { 85 c.Ui.Error(fmt.Sprintf("Prefix matched multiple namespaces\n\n%s", formatNamespaces(possible))) 86 return 1 87 } 88 89 out, err := Format(len(tmpl) == 0, tmpl, ns) 90 if err != nil { 91 c.Ui.Error(err.Error()) 92 return 1 93 } 94 95 c.Ui.Output(out) 96 return 0 97 }