github.com/hernad/nomad@v1.6.112/command/plugin_status.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/hernad/nomad/api/contexts" 11 "github.com/posener/complete" 12 ) 13 14 type PluginStatusCommand struct { 15 Meta 16 length int 17 short bool 18 verbose bool 19 json bool 20 template string 21 } 22 23 func (c *PluginStatusCommand) Help() string { 24 helpText := ` 25 Usage nomad plugin status [options] <plugin> 26 27 Display status information about a plugin. If no plugin id is given, 28 a list of all plugins will be displayed. 29 30 If ACLs are enabled, this command requires a token with the 'plugin:read' 31 capability. 32 33 General Options: 34 35 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` 36 37 Status Options: 38 39 -type <type> 40 List only plugins of type <type>. 41 42 -short 43 Display short output. 44 45 -verbose 46 Display full information. 47 48 -json 49 Output the allocation in its JSON format. 50 51 -t 52 Format and display allocation using a Go template. 53 ` 54 return helpText 55 } 56 57 func (c *PluginStatusCommand) Synopsis() string { 58 return "Display status information about a plugin" 59 } 60 61 // predictVolumeType is also used in volume_status 62 var predictVolumeType = complete.PredictFunc(func(a complete.Args) []string { 63 types := []string{"csi"} 64 for _, t := range types { 65 if strings.Contains(t, a.Last) { 66 return []string{t} 67 } 68 } 69 return nil 70 }) 71 72 func (c *PluginStatusCommand) AutocompleteFlags() complete.Flags { 73 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 74 complete.Flags{ 75 "-type": predictVolumeType, 76 "-short": complete.PredictNothing, 77 "-verbose": complete.PredictNothing, 78 "-json": complete.PredictNothing, 79 "-t": complete.PredictAnything, 80 }) 81 } 82 83 func (c *PluginStatusCommand) AutocompleteArgs() complete.Predictor { 84 return complete.PredictFunc(func(a complete.Args) []string { 85 client, err := c.Meta.Client() 86 if err != nil { 87 return nil 88 } 89 90 resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Plugins, nil) 91 if err != nil { 92 return []string{} 93 } 94 return resp.Matches[contexts.Plugins] 95 }) 96 } 97 98 func (c *PluginStatusCommand) Name() string { return "plugin status" } 99 100 func (c *PluginStatusCommand) Run(args []string) int { 101 var typeArg string 102 103 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 104 flags.Usage = func() { c.Ui.Output(c.Help()) } 105 flags.StringVar(&typeArg, "type", "", "") 106 flags.BoolVar(&c.short, "short", false, "") 107 flags.BoolVar(&c.verbose, "verbose", false, "") 108 flags.BoolVar(&c.json, "json", false, "") 109 flags.StringVar(&c.template, "t", "", "") 110 111 if err := flags.Parse(args); err != nil { 112 c.Ui.Error(fmt.Sprintf("Error parsing arguments %s", err)) 113 return 1 114 } 115 116 // Check that we either got no arguments or exactly one. 117 args = flags.Args() 118 if len(args) > 1 { 119 c.Ui.Error("This command takes either no arguments or one: <plugin>") 120 c.Ui.Error(commandErrorText(c)) 121 return 1 122 } 123 124 typeArg = strings.ToLower(typeArg) 125 126 // Check that the plugin type flag is supported. Empty implies we are 127 // querying all plugins, otherwise we currently only support "csi". 128 switch typeArg { 129 case "", "csi": 130 default: 131 c.Ui.Error(fmt.Sprintf("Unsupported plugin type: %s", typeArg)) 132 return 1 133 } 134 135 // Truncate the id unless full length is requested 136 c.length = shortId 137 if c.verbose { 138 c.length = fullId 139 } 140 141 // Get the HTTP client 142 client, err := c.Meta.Client() 143 if err != nil { 144 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 145 return 1 146 } 147 148 id := "" 149 if len(args) == 1 { 150 id = args[0] 151 } 152 153 code := c.csiStatus(client, id) 154 if code != 0 { 155 return code 156 } 157 158 // Extend this section with other plugin implementations 159 160 return 0 161 }