github.phpd.cn/hashicorp/consul@v1.4.5/command/acl/policy/list/policy_list.go (about)

     1  package policylist
     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 policy metadata such "+
    30  		"as the content hash and raft indices should be shown for each entry")
    31  
    32  	c.http = &flags.HTTPFlags{}
    33  	flags.Merge(c.flags, c.http.ClientFlags())
    34  	flags.Merge(c.flags, c.http.ServerFlags())
    35  	c.help = flags.Usage(help, c.flags)
    36  }
    37  
    38  func (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  	policies, _, err := client.ACL().PolicyList(nil)
    50  	if err != nil {
    51  		c.UI.Error(fmt.Sprintf("Failed to retrieve the policy list: %v", err))
    52  		return 1
    53  	}
    54  
    55  	for _, policy := range policies {
    56  		acl.PrintPolicyListEntry(policy, c.UI, c.showMeta)
    57  	}
    58  
    59  	return 0
    60  }
    61  
    62  func (c *cmd) Synopsis() string {
    63  	return synopsis
    64  }
    65  
    66  func (c *cmd) Help() string {
    67  	return flags.Usage(c.help, nil)
    68  }
    69  
    70  const synopsis = "Lists ACL Policies"
    71  const help = `
    72  Usage: consul acl policy list [options]
    73  
    74      Lists all the ACL policies
    75  
    76      Example:
    77  
    78          $ consul acl policy list
    79  `