1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/posener/complete"
8)
9
10type ACLTokenInfoCommand struct {
11	Meta
12}
13
14func (c *ACLTokenInfoCommand) Help() string {
15	helpText := `
16Usage: nomad acl token info <token_accessor_id>
17
18  Info is used to fetch information on an existing ACL tokens. Requires a management token.
19
20General Options:
21
22  ` + generalOptionsUsage()
23
24	return strings.TrimSpace(helpText)
25}
26
27func (c *ACLTokenInfoCommand) AutocompleteFlags() complete.Flags {
28	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
29		complete.Flags{})
30}
31
32func (c *ACLTokenInfoCommand) AutocompleteArgs() complete.Predictor {
33	return complete.PredictNothing
34}
35
36func (c *ACLTokenInfoCommand) Synopsis() string {
37	return "Fetch information on an existing ACL token"
38}
39
40func (c *ACLTokenInfoCommand) Name() string { return "acl token info" }
41
42func (c *ACLTokenInfoCommand) Run(args []string) int {
43	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
44	flags.Usage = func() { c.Ui.Output(c.Help()) }
45	if err := flags.Parse(args); err != nil {
46		return 1
47	}
48
49	// Check that we have exactly one argument
50	args = flags.Args()
51	if l := len(args); l != 1 {
52		c.Ui.Error("This command takes one argument: <token_accessor_id>")
53		c.Ui.Error(commandErrorText(c))
54		return 1
55	}
56
57	tokenAccessorID := args[0]
58
59	// Get the HTTP client
60	client, err := c.Meta.Client()
61	if err != nil {
62		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
63		return 1
64	}
65
66	// Get the specified token information
67	token, _, err := client.ACLTokens().Info(tokenAccessorID, nil)
68	if err != nil {
69		c.Ui.Error(fmt.Sprintf("Error fetching token: %s", err))
70		return 1
71	}
72
73	// Format the output
74	c.Ui.Output(formatKVACLToken(token))
75	return 0
76}
77