github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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 Sentinel commands are only available when ACLs are enabled. This command 21 requires a management token. 22 23 General Options: 24 25 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` 26 27 ` 28 return strings.TrimSpace(helpText) 29 } 30 31 func (c *SentinelListCommand) AutocompleteFlags() complete.Flags { 32 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 33 complete.Flags{}) 34 } 35 36 func (c *SentinelListCommand) AutocompleteArgs() complete.Predictor { 37 return complete.PredictNothing 38 } 39 40 func (c *SentinelListCommand) Synopsis() string { 41 return "Display all Sentinel policies" 42 } 43 44 func (c *SentinelListCommand) Name() string { return "sentinel list" } 45 46 func (c *SentinelListCommand) Run(args []string) int { 47 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 48 flags.Usage = func() { c.Ui.Output(c.Help()) } 49 if err := flags.Parse(args); err != nil { 50 return 1 51 } 52 53 if args = flags.Args(); len(args) > 0 { 54 c.Ui.Error("This command takes no arguments") 55 c.Ui.Error(commandErrorText(c)) 56 } 57 // Get the HTTP client 58 client, err := c.Meta.Client() 59 if err != nil { 60 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 61 return 1 62 } 63 64 // Get the list of policies 65 policies, _, err := client.SentinelPolicies().List(nil) 66 if err != nil { 67 c.Ui.Error(fmt.Sprintf("Error listing Sentinel policies: %s", err)) 68 return 1 69 } 70 71 if len(policies) == 0 { 72 c.Ui.Output("No policies found") 73 return 0 74 } 75 76 out := []string{} 77 out = append(out, "Name|Scope|Enforcement Level|Description") 78 for _, p := range policies { 79 line := fmt.Sprintf("%s|%s|%s|%s", p.Name, p.Scope, p.EnforcementLevel, p.Description) 80 out = append(out, line) 81 } 82 c.Ui.Output(formatList(out)) 83 return 0 84 }