github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/command/acl/policy/delete/policy_delete.go (about) 1 package policydelete 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 } 27 28 func (c *cmd) init() { 29 c.flags = flag.NewFlagSet("", flag.ContinueOnError) 30 c.flags.StringVar(&c.policyID, "id", "", "The ID of the policy to delete. "+ 31 "It may be specified as a unique ID prefix but will error if the prefix "+ 32 "matches multiple policy IDs") 33 c.flags.StringVar(&c.policyName, "name", "", "The name of the policy to delete.") 34 c.http = &flags.HTTPFlags{} 35 flags.Merge(c.flags, c.http.ClientFlags()) 36 flags.Merge(c.flags, c.http.ServerFlags()) 37 c.help = flags.Usage(help, c.flags) 38 } 39 40 func (c *cmd) Run(args []string) int { 41 if err := c.flags.Parse(args); err != nil { 42 return 1 43 } 44 45 if c.policyID == "" && c.policyName == "" { 46 c.UI.Error(fmt.Sprintf("Must specify either the -id or -name parameters")) 47 return 1 48 } 49 50 client, err := c.http.APIClient() 51 if err != nil { 52 c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) 53 return 1 54 } 55 56 var policyID string 57 if c.policyID != "" { 58 policyID, err = acl.GetPolicyIDFromPartial(client, c.policyID) 59 } else { 60 policyID, err = acl.GetPolicyIDByName(client, c.policyName) 61 } 62 if err != nil { 63 c.UI.Error(fmt.Sprintf("Error determining policy ID: %v", err)) 64 return 1 65 } 66 67 if _, err := client.ACL().PolicyDelete(policyID, nil); err != nil { 68 c.UI.Error(fmt.Sprintf("Error deleting policy %q: %v", policyID, err)) 69 return 1 70 } 71 72 c.UI.Info(fmt.Sprintf("Policy %q deleted successfully", policyID)) 73 return 0 74 } 75 76 func (c *cmd) Synopsis() string { 77 return synopsis 78 } 79 80 func (c *cmd) Help() string { 81 return flags.Usage(c.help, nil) 82 } 83 84 const synopsis = "Delete an ACL Policy" 85 const help = ` 86 Usage: consul acl policy delete [options] -id POLICY 87 88 Deletes an ACL policy by providing either the ID or a unique ID prefix. 89 90 Delete by prefix: 91 92 $ consul acl policy delete -id b6b85 93 94 Delete by full ID: 95 96 $ consul acl policy delete -id b6b856da-5193-4e78-845a-7d61ca8371ba 97 98 `