List does json/template

This commit is contained in:
Alex Dadgar 2017-10-13 16:37:33 -07:00
parent 919fc3ce70
commit b6d2d1f6af
4 changed files with 49 additions and 4 deletions

View File

@ -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
}

View File

@ -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()
}

View File

@ -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())
}

View File

@ -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