github.com/djenriquez/nomad-1@v0.8.1/command/acl_policy_list.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api" 8 "github.com/posener/complete" 9 ) 10 11 type ACLPolicyListCommand struct { 12 Meta 13 } 14 15 func (c *ACLPolicyListCommand) Help() string { 16 helpText := ` 17 Usage: nomad acl policy list 18 19 List is used to list available ACL policies. 20 21 General Options: 22 23 ` + generalOptionsUsage() + ` 24 25 List Options: 26 27 -json 28 Output the ACL policies in a JSON format. 29 30 -t 31 Format and display the ACL policies using a Go template. 32 ` 33 34 return strings.TrimSpace(helpText) 35 } 36 37 func (c *ACLPolicyListCommand) AutocompleteFlags() complete.Flags { 38 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 39 complete.Flags{ 40 "-json": complete.PredictNothing, 41 "-t": complete.PredictAnything, 42 }) 43 } 44 45 func (c *ACLPolicyListCommand) AutocompleteArgs() complete.Predictor { 46 return complete.PredictNothing 47 } 48 49 func (c *ACLPolicyListCommand) Synopsis() string { 50 return "List ACL policies" 51 } 52 53 func (c *ACLPolicyListCommand) Run(args []string) int { 54 var json bool 55 var tmpl string 56 57 flags := c.Meta.FlagSet("acl policy list", FlagSetClient) 58 flags.Usage = func() { c.Ui.Output(c.Help()) } 59 flags.BoolVar(&json, "json", false, "") 60 flags.StringVar(&tmpl, "t", "", "") 61 62 if err := flags.Parse(args); err != nil { 63 return 1 64 } 65 66 // Check that we got no arguments 67 args = flags.Args() 68 if l := len(args); l != 0 { 69 c.Ui.Error(c.Help()) 70 return 1 71 } 72 73 // Get the HTTP client 74 client, err := c.Meta.Client() 75 if err != nil { 76 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 77 return 1 78 } 79 80 // Fetch info on the policy 81 policies, _, err := client.ACLPolicies().List(nil) 82 if err != nil { 83 c.Ui.Error(fmt.Sprintf("Error listing ACL policies: %s", err)) 84 return 1 85 } 86 87 if json || len(tmpl) > 0 { 88 out, err := Format(json, tmpl, policies) 89 if err != nil { 90 c.Ui.Error(err.Error()) 91 return 1 92 } 93 94 c.Ui.Output(out) 95 return 0 96 } 97 98 c.Ui.Output(formatPolicies(policies)) 99 return 0 100 } 101 102 func formatPolicies(policies []*api.ACLPolicyListStub) string { 103 if len(policies) == 0 { 104 return "No policies found" 105 } 106 107 output := make([]string, 0, len(policies)+1) 108 output = append(output, fmt.Sprintf("Name|Description")) 109 for _, p := range policies { 110 output = append(output, fmt.Sprintf("%s|%s", p.Name, p.Description)) 111 } 112 113 return formatList(output) 114 }