hub.fastgit.org/hashicorp/consul.git@v1.4.5/command/leave/leave.go (about) 1 package leave 2 3 import ( 4 "flag" 5 "fmt" 6 7 "github.com/hashicorp/consul/command/flags" 8 "github.com/mitchellh/cli" 9 ) 10 11 func New(ui cli.Ui) *cmd { 12 c := &cmd{UI: ui} 13 c.init() 14 return c 15 } 16 17 type cmd struct { 18 UI cli.Ui 19 flags *flag.FlagSet 20 http *flags.HTTPFlags 21 help string 22 } 23 24 func (c *cmd) init() { 25 c.flags = flag.NewFlagSet("", flag.ContinueOnError) 26 c.http = &flags.HTTPFlags{} 27 flags.Merge(c.flags, c.http.ClientFlags()) 28 c.help = flags.Usage(help, c.flags) 29 } 30 31 func (c *cmd) Run(args []string) int { 32 if err := c.flags.Parse(args); err != nil { 33 return 1 34 } 35 nonFlagArgs := c.flags.Args() 36 if len(nonFlagArgs) > 0 { 37 c.UI.Error(fmt.Sprintf("Error found unexpected args: %v", nonFlagArgs)) 38 c.UI.Output(c.Help()) 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 if err := client.Agent().Leave(); err != nil { 49 c.UI.Error(fmt.Sprintf("Error leaving: %s", err)) 50 return 1 51 } 52 53 c.UI.Output("Graceful leave complete") 54 return 0 55 } 56 57 func (c *cmd) Synopsis() string { 58 return synopsis 59 } 60 61 func (c *cmd) Help() string { 62 return c.help 63 } 64 65 const synopsis = "Gracefully leaves the Consul cluster and shuts down" 66 const help = ` 67 Usage: consul leave [options] 68 69 Causes the agent to gracefully leave the Consul cluster and shutdown. 70 `