commands: move leave command to separate pkg
This commit is contained in:
parent
ffb747a744
commit
5d75449419
|
@ -21,6 +21,7 @@ import (
|
|||
"github.com/hashicorp/consul/command/kvget"
|
||||
"github.com/hashicorp/consul/command/kvimp"
|
||||
"github.com/hashicorp/consul/command/kvput"
|
||||
"github.com/hashicorp/consul/command/leave"
|
||||
"github.com/hashicorp/consul/command/validate"
|
||||
"github.com/hashicorp/consul/version"
|
||||
"github.com/mitchellh/cli"
|
||||
|
@ -136,12 +137,7 @@ func init() {
|
|||
},
|
||||
|
||||
"leave": func() (cli.Command, error) {
|
||||
return &LeaveCommand{
|
||||
BaseCommand: BaseCommand{
|
||||
Flags: FlagSetClientHTTP,
|
||||
UI: ui,
|
||||
},
|
||||
}, nil
|
||||
return leave.New(ui), nil
|
||||
},
|
||||
|
||||
"lock": func() (cli.Command, error) {
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// LeaveCommand is a Command implementation that instructs
|
||||
// the Consul agent to gracefully leave the cluster
|
||||
type LeaveCommand struct {
|
||||
BaseCommand
|
||||
}
|
||||
|
||||
func (c *LeaveCommand) Help() string {
|
||||
c.InitFlagSet()
|
||||
return c.HelpCommand(`
|
||||
Usage: consul leave [options]
|
||||
|
||||
Causes the agent to gracefully leave the Consul cluster and shutdown.
|
||||
|
||||
`)
|
||||
}
|
||||
|
||||
func (c *LeaveCommand) Run(args []string) int {
|
||||
c.InitFlagSet()
|
||||
if err := c.FlagSet.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
nonFlagArgs := c.FlagSet.Args()
|
||||
if len(nonFlagArgs) > 0 {
|
||||
c.UI.Error(fmt.Sprintf("Error found unexpected args: %v", nonFlagArgs))
|
||||
c.UI.Output(c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
client, err := c.HTTPClient()
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
if err := client.Agent().Leave(); err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error leaving: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
c.UI.Output("Graceful leave complete")
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *LeaveCommand) Synopsis() string {
|
||||
return "Gracefully leaves the Consul cluster and shuts down"
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package leave
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/consul/command/flags"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func New(ui cli.Ui) *cmd {
|
||||
c := &cmd{UI: ui}
|
||||
c.initFlags()
|
||||
return c
|
||||
}
|
||||
|
||||
type cmd struct {
|
||||
UI cli.Ui
|
||||
flags *flag.FlagSet
|
||||
http *flags.HTTPFlags
|
||||
}
|
||||
|
||||
func (c *cmd) initFlags() {
|
||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
c.http = &flags.HTTPFlags{}
|
||||
flags.Merge(c.flags, c.http.ClientFlags())
|
||||
flags.Merge(c.flags, c.http.ServerFlags())
|
||||
}
|
||||
|
||||
func (c *cmd) Run(args []string) int {
|
||||
if err := c.flags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
nonFlagArgs := c.flags.Args()
|
||||
if len(nonFlagArgs) > 0 {
|
||||
c.UI.Error(fmt.Sprintf("Error found unexpected args: %v", nonFlagArgs))
|
||||
c.UI.Output(c.Help())
|
||||
return 1
|
||||
}
|
||||
|
||||
client, err := c.http.APIClient()
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
if err := client.Agent().Leave(); err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error leaving: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
c.UI.Output("Graceful leave complete")
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *cmd) Synopsis() string {
|
||||
return "Gracefully leaves the Consul cluster and shuts down"
|
||||
}
|
||||
|
||||
func (c *cmd) Help() string {
|
||||
s := `Usage: consul leave [options]
|
||||
|
||||
Causes the agent to gracefully leave the Consul cluster and shutdown.`
|
||||
|
||||
return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package command
|
||||
package leave
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
@ -8,19 +8,11 @@ import (
|
|||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func testLeaveCommand(t *testing.T) (*cli.MockUi, *LeaveCommand) {
|
||||
ui := cli.NewMockUi()
|
||||
return ui, &LeaveCommand{
|
||||
BaseCommand: BaseCommand{
|
||||
UI: ui,
|
||||
Flags: FlagSetClientHTTP,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeaveCommand_implements(t *testing.T) {
|
||||
func TestLeaveCommand_noTabs(t *testing.T) {
|
||||
t.Parallel()
|
||||
var _ cli.Command = &LeaveCommand{}
|
||||
if strings.ContainsRune(New(nil).Help(), '\t') {
|
||||
t.Fatal("usage has tabs")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeaveCommandRun(t *testing.T) {
|
||||
|
@ -28,7 +20,8 @@ func TestLeaveCommandRun(t *testing.T) {
|
|||
a := agent.NewTestAgent(t.Name(), ``)
|
||||
defer a.Shutdown()
|
||||
|
||||
ui, c := testLeaveCommand(t)
|
||||
ui := cli.NewMockUi()
|
||||
c := New(ui)
|
||||
args := []string{"-http-addr=" + a.HTTPAddr()}
|
||||
|
||||
code := c.Run(args)
|
||||
|
@ -46,7 +39,8 @@ func TestLeaveCommandFailOnNonFlagArgs(t *testing.T) {
|
|||
a := agent.NewTestAgent(t.Name(), ``)
|
||||
defer a.Shutdown()
|
||||
|
||||
_, c := testLeaveCommand(t)
|
||||
ui := cli.NewMockUi()
|
||||
c := New(ui)
|
||||
args := []string{"-http-addr=" + a.HTTPAddr(), "appserver1"}
|
||||
|
||||
code := c.Run(args)
|
Loading…
Reference in New Issue