|
| 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 ACLTokenListCommand struct { |
| 12 | + Meta |
| 13 | +} |
| 14 | + |
| 15 | +func (c *ACLTokenListCommand) Help() string { |
| 16 | + helpText := ` |
| 17 | +Usage: nomad acl token list |
| 18 | +
|
| 19 | + List is used to list existing ACL tokens. |
| 20 | +
|
| 21 | +General Options: |
| 22 | +
|
| 23 | + ` + generalOptionsUsage() + ` |
| 24 | +
|
| 25 | +List Options: |
| 26 | +
|
| 27 | + -json |
| 28 | + Output the ACL tokens in a JSON format. |
| 29 | +
|
| 30 | + -t |
| 31 | + Format and display the ACL tokens using a Go template. |
| 32 | +` |
| 33 | + |
| 34 | + return strings.TrimSpace(helpText) |
| 35 | +} |
| 36 | + |
| 37 | +func (c *ACLTokenListCommand) 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 *ACLTokenListCommand) AutocompleteArgs() complete.Predictor { |
| 46 | + return complete.PredictNothing |
| 47 | +} |
| 48 | + |
| 49 | +func (c *ACLTokenListCommand) Synopsis() string { |
| 50 | + return "List ACL tokens" |
| 51 | +} |
| 52 | + |
| 53 | +func (c *ACLTokenListCommand) Name() string { return "acl token list" } |
| 54 | + |
| 55 | +func (c *ACLTokenListCommand) 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 | + tokens, _, err := client.ACLTokens().List(nil) |
| 85 | + if err != nil { |
| 86 | + c.Ui.Error(fmt.Sprintf("Error listing ACL tokens: %s", err)) |
| 87 | + return 1 |
| 88 | + } |
| 89 | + |
| 90 | + if json || len(tmpl) > 0 { |
| 91 | + out, err := Format(json, tmpl, tokens) |
| 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(formatTokens(tokens)) |
| 102 | + return 0 |
| 103 | +} |
| 104 | + |
| 105 | +func formatTokens(tokens []*api.ACLTokenListStub) string { |
| 106 | + if len(tokens) == 0 { |
| 107 | + return "No tokens found" |
| 108 | + } |
| 109 | + |
| 110 | + output := make([]string, 0, len(tokens)+1) |
| 111 | + output = append(output, fmt.Sprintf("Name|Type|Global|Accessor ID")) |
| 112 | + for _, p := range tokens { |
| 113 | + output = append(output, fmt.Sprintf("%s|%s|%t|%s", p.Name, p.Type, p.Global, p.AccessorID)) |
| 114 | + } |
| 115 | + |
| 116 | + return formatList(output) |
| 117 | +} |
0 commit comments