diff --git a/command/acl_policy_list.go b/command/acl_policy_list.go index cf9ed255b..65e2a42ae 100644 --- a/command/acl_policy_list.go +++ b/command/acl_policy_list.go @@ -16,17 +16,30 @@ func (c *ACLPolicyListCommand) Help() string { helpText := ` Usage: nomad acl policy list -List is used to list available ACL policies. + List is used to list available ACL policies. General Options: - ` + generalOptionsUsage() + ` + generalOptionsUsage() + ` + +List Options: + + -json + Output the ACL policies in a JSON format. + + -t + Format and display the ACL policies using a Go template. +` return strings.TrimSpace(helpText) } func (c *ACLPolicyListCommand) AutocompleteFlags() complete.Flags { - return c.Meta.AutocompleteFlags(FlagSetClient) + return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), + complete.Flags{ + "-json": complete.PredictNothing, + "-t": complete.PredictAnything, + }) } func (c *ACLPolicyListCommand) AutocompleteArgs() complete.Predictor { @@ -38,8 +51,14 @@ func (c *ACLPolicyListCommand) Synopsis() string { } func (c *ACLPolicyListCommand) Run(args []string) int { + var json bool + var tmpl string + flags := c.Meta.FlagSet("acl policy list", FlagSetClient) flags.Usage = func() { c.Ui.Output(c.Help()) } + flags.BoolVar(&json, "json", false, "") + flags.StringVar(&tmpl, "t", "", "") + if err := flags.Parse(args); err != nil { return 1 } @@ -65,6 +84,17 @@ func (c *ACLPolicyListCommand) Run(args []string) int { return 1 } + if json || len(tmpl) > 0 { + out, err := Format(json, tmpl, policies) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + c.Ui.Output(out) + return 0 + } + c.Ui.Output(formatPolicies(policies)) return 0 } diff --git a/command/acl_policy_list_test.go b/command/acl_policy_list_test.go index 36f2940b9..f1f2a4e09 100644 --- a/command/acl_policy_list_test.go +++ b/command/acl_policy_list_test.go @@ -52,4 +52,14 @@ func TestACLPolicyListCommand(t *testing.T) { if !strings.Contains(out, policy.Name) { t.Fatalf("bad: %v", out) } + + // List json + if code := cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-json"}); code != 0 { + t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String()) + } + out = ui.OutputWriter.String() + if !strings.Contains(out, "CreateIndex") { + t.Fatalf("expected json output, got: %s", out) + } + ui.OutputWriter.Reset() } diff --git a/command/namespace_list_test.go b/command/namespace_list_test.go index 46012fe3a..49b232a07 100644 --- a/command/namespace_list_test.go +++ b/command/namespace_list_test.go @@ -58,7 +58,6 @@ func TestNamespaceListCommand_List(t *testing.T) { ui.OutputWriter.Reset() // List json - t.Log(url) if code := cmd.Run([]string{"-address=" + url, "-json"}); code != 0 { t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String()) } diff --git a/website/source/docs/commands/acl/policy-list.html.md.erb b/website/source/docs/commands/acl/policy-list.html.md.erb index 475ea6655..90d0c2fd3 100644 --- a/website/source/docs/commands/acl/policy-list.html.md.erb +++ b/website/source/docs/commands/acl/policy-list.html.md.erb @@ -19,6 +19,12 @@ nomad acl policy list ## General Options <%= partial "docs/commands/_general_options" %> +# +## List Options + +* `-json` : Output the namespaces in their JSON format. + +* `-t` : Format and display the namespaces using a Go template. ## Examples