github.com/rohankumardubey/nomad@v0.11.8/command/volume_status.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api/contexts" 8 "github.com/posener/complete" 9 ) 10 11 type VolumeStatusCommand struct { 12 Meta 13 length int 14 short bool 15 verbose bool 16 json bool 17 template string 18 } 19 20 func (c *VolumeStatusCommand) Help() string { 21 helpText := ` 22 Usage: nomad volume status [options] <id> 23 24 Display status information about a CSI volume. If no volume id is given, a 25 list of all volumes will be displayed. 26 27 General Options: 28 29 ` + generalOptionsUsage() + ` 30 31 Status Options: 32 33 -type <type> 34 List only volumes of type <type>. 35 36 -short 37 Display short output. Used only when a single volume is being 38 queried, and drops verbose information about allocations. 39 40 -verbose 41 Display full allocation information. 42 43 -json 44 Output the allocation in its JSON format. 45 46 -t 47 Format and display allocation using a Go template. 48 ` 49 return strings.TrimSpace(helpText) 50 } 51 52 func (c *VolumeStatusCommand) Synopsis() string { 53 return "Display status information about a volume" 54 } 55 56 func (c *VolumeStatusCommand) AutocompleteFlags() complete.Flags { 57 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 58 complete.Flags{ 59 "-type": predictVolumeType, 60 "-short": complete.PredictNothing, 61 "-verbose": complete.PredictNothing, 62 "-json": complete.PredictNothing, 63 "-t": complete.PredictAnything, 64 }) 65 } 66 67 func (c *VolumeStatusCommand) AutocompleteArgs() complete.Predictor { 68 return complete.PredictFunc(func(a complete.Args) []string { 69 client, err := c.Meta.Client() 70 if err != nil { 71 return nil 72 } 73 74 resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Volumes, nil) 75 if err != nil { 76 return []string{} 77 } 78 return resp.Matches[contexts.Volumes] 79 }) 80 } 81 82 func (c *VolumeStatusCommand) Name() string { return "volume status" } 83 84 func (c *VolumeStatusCommand) Run(args []string) int { 85 var typeArg string 86 87 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 88 flags.Usage = func() { c.Ui.Output(c.Help()) } 89 flags.StringVar(&typeArg, "type", "", "") 90 flags.BoolVar(&c.short, "short", false, "") 91 flags.BoolVar(&c.verbose, "verbose", false, "") 92 flags.BoolVar(&c.json, "json", false, "") 93 flags.StringVar(&c.template, "t", "", "") 94 95 if err := flags.Parse(args); err != nil { 96 c.Ui.Error(fmt.Sprintf("Error parsing arguments %s", err)) 97 return 1 98 } 99 100 // Check that we either got no arguments or exactly one 101 args = flags.Args() 102 if len(args) > 1 { 103 c.Ui.Error("This command takes either no arguments or one: <id>") 104 c.Ui.Error(commandErrorText(c)) 105 return 1 106 } 107 108 // Truncate the id unless full length is requested 109 c.length = shortId 110 if c.verbose { 111 c.length = fullId 112 } 113 114 // Get the HTTP client 115 client, err := c.Meta.Client() 116 if err != nil { 117 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 118 return 1 119 } 120 121 id := "" 122 if len(args) == 1 { 123 id = args[0] 124 } 125 126 code := c.csiStatus(client, id) 127 if code != 0 { 128 return code 129 } 130 131 // Extend this section with other volume implementations 132 133 return 0 134 }