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