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