github.hscsec.cn/hashicorp/consul@v1.4.5/command/acl/policy/read/policy_read.go (about) 1 package policyread 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 policyID string 25 policyName string 26 showMeta bool 27 } 28 29 func (c *cmd) init() { 30 c.flags = flag.NewFlagSet("", flag.ContinueOnError) 31 c.flags.BoolVar(&c.showMeta, "meta", false, "Indicates that policy metadata such "+ 32 "as the content hash and raft indices should be shown for each entry") 33 c.flags.StringVar(&c.policyID, "id", "", "The ID of the policy to read. "+ 34 "It may be specified as a unique ID prefix but will error if the prefix "+ 35 "matches multiple policy IDs") 36 c.flags.StringVar(&c.policyName, "name", "", "The name of the policy to read.") 37 c.http = &flags.HTTPFlags{} 38 flags.Merge(c.flags, c.http.ClientFlags()) 39 flags.Merge(c.flags, c.http.ServerFlags()) 40 c.help = flags.Usage(help, c.flags) 41 } 42 43 func (c *cmd) Run(args []string) int { 44 if err := c.flags.Parse(args); err != nil { 45 return 1 46 } 47 48 if c.policyID == "" && c.policyName == "" { 49 c.UI.Error(fmt.Sprintf("Must specify either the -id or -name parameters")) 50 return 1 51 } 52 53 client, err := c.http.APIClient() 54 if err != nil { 55 c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) 56 return 1 57 } 58 59 var policyID string 60 if c.policyID != "" { 61 policyID, err = acl.GetPolicyIDFromPartial(client, c.policyID) 62 } else { 63 policyID, err = acl.GetPolicyIDByName(client, c.policyName) 64 } 65 if err != nil { 66 c.UI.Error(fmt.Sprintf("Error determining policy ID: %v", err)) 67 return 1 68 } 69 70 policy, _, err := client.ACL().PolicyRead(policyID, nil) 71 if err != nil { 72 c.UI.Error(fmt.Sprintf("Error reading policy %q: %v", policyID, err)) 73 return 1 74 } 75 acl.PrintPolicy(policy, c.UI, c.showMeta) 76 return 0 77 } 78 79 func (c *cmd) Synopsis() string { 80 return synopsis 81 } 82 83 func (c *cmd) Help() string { 84 return flags.Usage(c.help, nil) 85 } 86 87 const synopsis = "Read an ACL Policy" 88 const help = ` 89 Usage: consul acl policy read [options] POLICY 90 91 This command will retrieve and print out the details 92 of a single policy. 93 94 Read: 95 96 $ consul acl policy read -id fdabbcb5-9de5-4b1a-961f-77214ae88cba 97 98 Read by name: 99 100 $ consul acl policy read -name my-policy 101 102 `