1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/posener/complete"
8)
9
10type ACLPolicyInfoCommand struct {
11	Meta
12}
13
14func (c *ACLPolicyInfoCommand) Help() string {
15	helpText := `
16Usage: nomad acl policy info <name>
17
18  Info is used to fetch information on an existing ACL policy.
19
20General Options:
21
22  ` + generalOptionsUsage()
23
24	return strings.TrimSpace(helpText)
25}
26
27func (c *ACLPolicyInfoCommand) AutocompleteFlags() complete.Flags {
28	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
29		complete.Flags{})
30}
31
32func (c *ACLPolicyInfoCommand) AutocompleteArgs() complete.Predictor {
33	return complete.PredictNothing
34}
35
36func (c *ACLPolicyInfoCommand) Synopsis() string {
37	return "Fetch info on an existing ACL policy"
38}
39
40func (c *ACLPolicyInfoCommand) Name() string { return "acl policy info" }
41
42func (c *ACLPolicyInfoCommand) Run(args []string) int {
43	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
44	flags.Usage = func() { c.Ui.Output(c.Help()) }
45	if err := flags.Parse(args); err != nil {
46		return 1
47	}
48
49	// Check that we got exactly one argument
50	args = flags.Args()
51	if l := len(args); l != 1 {
52		c.Ui.Error("This command takes one argument: <name>")
53		c.Ui.Error(commandErrorText(c))
54		return 1
55	}
56
57	// Get the policy name
58	policyName := args[0]
59
60	// Get the HTTP client
61	client, err := c.Meta.Client()
62	if err != nil {
63		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
64		return 1
65	}
66
67	// Fetch info on the policy
68	policy, _, err := client.ACLPolicies().Info(policyName, nil)
69	if err != nil {
70		c.Ui.Error(fmt.Sprintf("Error fetching info on ACL policy: %s", err))
71		return 1
72	}
73
74	c.Ui.Output(formatKVPolicy(policy))
75	return 0
76}
77