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