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