github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/sentinel_list.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/posener/complete"
     8  )
     9  
    10  type SentinelListCommand struct {
    11  	Meta
    12  }
    13  
    14  func (c *SentinelListCommand) Help() string {
    15  	helpText := `
    16  Usage: nomad sentinel list [options]
    17  
    18    List is used to display all the installed Sentinel policies.
    19  
    20  General Options:
    21  
    22    ` + generalOptionsUsage() + `
    23  
    24  `
    25  	return strings.TrimSpace(helpText)
    26  }
    27  
    28  func (c *SentinelListCommand) AutocompleteFlags() complete.Flags {
    29  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    30  		complete.Flags{})
    31  }
    32  
    33  func (c *SentinelListCommand) AutocompleteArgs() complete.Predictor {
    34  	return complete.PredictNothing
    35  }
    36  
    37  func (c *SentinelListCommand) Synopsis() string {
    38  	return "Display all Sentinel policies"
    39  }
    40  
    41  func (c *SentinelListCommand) Run(args []string) int {
    42  	flags := c.Meta.FlagSet("sentinel list", FlagSetClient)
    43  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    44  	if err := flags.Parse(args); err != nil {
    45  		return 1
    46  	}
    47  
    48  	// Get the HTTP client
    49  	client, err := c.Meta.Client()
    50  	if err != nil {
    51  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    52  		return 1
    53  	}
    54  
    55  	// Get the list of policies
    56  	policies, _, err := client.SentinelPolicies().List(nil)
    57  	if err != nil {
    58  		c.Ui.Error(fmt.Sprintf("Error listing Sentinel policies: %s", err))
    59  		return 1
    60  	}
    61  
    62  	if len(policies) == 0 {
    63  		c.Ui.Output("No policies found")
    64  		return 0
    65  	}
    66  
    67  	out := []string{}
    68  	out = append(out, "Name|Scope|Enforcement Level|Description")
    69  	for _, p := range policies {
    70  		line := fmt.Sprintf("%s|%s|%s|%s", p.Name, p.Scope, p.EnforcementLevel, p.Description)
    71  		out = append(out, line)
    72  	}
    73  	c.Ui.Output(formatList(out))
    74  	return 0
    75  }