github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/command/acl/token/list/token_list.go (about)

     1  package tokenlist
     2  
     3  import (
     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  
    12  func New(ui cli.Ui) *cmd {
    13  	c := &cmd{UI: ui}
    14  	c.init()
    15  	return c
    16  }
    17  
    18  type cmd struct {
    19  	UI    cli.Ui
    20  	flags *flag.FlagSet
    21  	http  *flags.HTTPFlags
    22  	help  string
    23  
    24  	showMeta bool
    25  }
    26  
    27  func (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  	c.help = flags.Usage(help, c.flags)
    35  }
    36  
    37  func (c *cmd) Run(args []string) int {
    38  	if err := c.flags.Parse(args); err != nil {
    39  		return 1
    40  	}
    41  
    42  	client, err := c.http.APIClient()
    43  	if err != nil {
    44  		c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
    45  		return 1
    46  	}
    47  
    48  	tokens, _, err := client.ACL().TokenList(nil)
    49  	if err != nil {
    50  		c.UI.Error(fmt.Sprintf("Failed to retrieve the token list: %v", err))
    51  		return 1
    52  	}
    53  
    54  	first := true
    55  	for _, token := range tokens {
    56  		if first {
    57  			first = false
    58  		} else {
    59  			c.UI.Info("")
    60  		}
    61  		acl.PrintTokenListEntry(token, c.UI, c.showMeta)
    62  	}
    63  
    64  	return 0
    65  }
    66  
    67  func (c *cmd) Synopsis() string {
    68  	return synopsis
    69  }
    70  
    71  func (c *cmd) Help() string {
    72  	return flags.Usage(c.help, nil)
    73  }
    74  
    75  const synopsis = "List ACL Tokens"
    76  const help = `
    77  Usage: consul acl token list [options]
    78  
    79    List all the ALC tokens
    80  
    81            $ consul acl token list
    82  `