github.com/superfly/nomad@v0.10.5-fly/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) Name() string { return "acl policy list" } 54 55 func (c *ACLPolicyListCommand) Run(args []string) int { 56 var json bool 57 var tmpl string 58 59 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 60 flags.Usage = func() { c.Ui.Output(c.Help()) } 61 flags.BoolVar(&json, "json", false, "") 62 flags.StringVar(&tmpl, "t", "", "") 63 64 if err := flags.Parse(args); err != nil { 65 return 1 66 } 67 68 // Check that we got no arguments 69 args = flags.Args() 70 if l := len(args); l != 0 { 71 c.Ui.Error("This command takes no arguments") 72 c.Ui.Error(commandErrorText(c)) 73 return 1 74 } 75 76 // Get the HTTP client 77 client, err := c.Meta.Client() 78 if err != nil { 79 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 80 return 1 81 } 82 83 // Fetch info on the policy 84 policies, _, err := client.ACLPolicies().List(nil) 85 if err != nil { 86 c.Ui.Error(fmt.Sprintf("Error listing ACL policies: %s", err)) 87 return 1 88 } 89 90 if json || len(tmpl) > 0 { 91 out, err := Format(json, tmpl, policies) 92 if err != nil { 93 c.Ui.Error(err.Error()) 94 return 1 95 } 96 97 c.Ui.Output(out) 98 return 0 99 } 100 101 c.Ui.Output(formatPolicies(policies)) 102 return 0 103 } 104 105 func formatPolicies(policies []*api.ACLPolicyListStub) string { 106 if len(policies) == 0 { 107 return "No policies found" 108 } 109 110 output := make([]string, 0, len(policies)+1) 111 output = append(output, fmt.Sprintf("Name|Description")) 112 for _, p := range policies { 113 output = append(output, fmt.Sprintf("%s|%s", p.Name, p.Description)) 114 } 115 116 return formatList(output) 117 }