1package tokenlist
2
3import (
4	"flag"
5	"fmt"
6
7	"github.com/hashicorp/consul/command/acl"
8	"github.com/hashicorp/consul/command/flags"
9	"github.com/mitchellh/cli"
10)
11
12func New(ui cli.Ui) *cmd {
13	c := &cmd{UI: ui}
14	c.init()
15	return c
16}
17
18type cmd struct {
19	UI    cli.Ui
20	flags *flag.FlagSet
21	http  *flags.HTTPFlags
22	help  string
23
24	showMeta bool
25}
26
27func (c *cmd) init() {
28	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
29	c.flags.BoolVar(&c.showMeta, "meta", false, "Indicates that token metadata such "+
30		"as the content hash and Raft indices should be shown for each entry")
31	c.http = &flags.HTTPFlags{}
32	flags.Merge(c.flags, c.http.ClientFlags())
33	flags.Merge(c.flags, c.http.ServerFlags())
34	flags.Merge(c.flags, c.http.NamespaceFlags())
35	c.help = flags.Usage(help, c.flags)
36}
37
38func (c *cmd) Run(args []string) int {
39	if err := c.flags.Parse(args); err != nil {
40		return 1
41	}
42
43	client, err := c.http.APIClient()
44	if err != nil {
45		c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
46		return 1
47	}
48
49	tokens, _, err := client.ACL().TokenList(nil)
50	if err != nil {
51		c.UI.Error(fmt.Sprintf("Failed to retrieve the token list: %v", err))
52		return 1
53	}
54
55	first := true
56	for _, token := range tokens {
57		if first {
58			first = false
59		} else {
60			c.UI.Info("")
61		}
62		acl.PrintTokenListEntry(token, c.UI, c.showMeta)
63	}
64
65	return 0
66}
67
68func (c *cmd) Synopsis() string {
69	return synopsis
70}
71
72func (c *cmd) Help() string {
73	return flags.Usage(c.help, nil)
74}
75
76const synopsis = "List ACL tokens"
77const help = `
78Usage: consul acl token list [options]
79
80  List all the ACL tokens
81
82          $ consul acl token list
83`
84