Convert leave command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-09 16:41:17 -05:00
parent 41ca1ccebc
commit e385af8eeb
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
4 changed files with 31 additions and 29 deletions

View File

@ -1,53 +1,47 @@
package command
import (
"flag"
"fmt"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/base"
"strings"
)
// LeaveCommand is a Command implementation that instructs
// the Consul agent to gracefully leave the cluster
type LeaveCommand struct {
Ui cli.Ui
base.Command
}
func (c *LeaveCommand) Help() string {
helpText := `
Usage: consul leave
Usage: consul leave [options]
Causes the agent to gracefully leave the Consul cluster and shutdown.
Options:
` + c.Command.Help()
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent.
`
return strings.TrimSpace(helpText)
}
func (c *LeaveCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("leave", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
rpcAddr := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
f := c.Command.NewFlagSet(c)
if err := c.Command.Parse(args); err != nil {
return 1
}
nonFlagArgs := cmdFlags.Args()
nonFlagArgs := f.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 := RPCClient(*rpcAddr)
client, err := c.Command.HTTPClient()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
defer client.Close()
if err := client.Leave(); err != nil {
if err := client.Agent().Leave(); err != nil {
c.Ui.Error(fmt.Sprintf("Error leaving: %s", err))
return 1
}

View File

@ -1,11 +1,22 @@
package command
import (
"github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli"
"strings"
"testing"
)
func testLeaveCommand(t *testing.T) (*cli.MockUi, *LeaveCommand) {
ui := new(cli.MockUi)
return ui, &LeaveCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetClientHTTP,
},
}
}
func TestLeaveCommand_implements(t *testing.T) {
var _ cli.Command = &LeaveCommand{}
}
@ -14,9 +25,8 @@ func TestLeaveCommandRun(t *testing.T) {
a1 := testAgent(t)
defer a1.Shutdown()
ui := new(cli.MockUi)
c := &LeaveCommand{Ui: ui}
args := []string{"-rpc-addr=" + a1.addr}
ui, c := testLeaveCommand(t)
args := []string{"-http-addr=" + a1.httpAddr}
code := c.Run(args)
if code != 0 {
@ -32,9 +42,8 @@ func TestLeaveCommandFailOnNonFlagArgs(t *testing.T) {
a1 := testAgent(t)
defer a1.Shutdown()
ui := new(cli.MockUi)
c := &LeaveCommand{Ui: ui}
args := []string{"-rpc-addr=" + a1.addr, "appserver1"}
_, c := testLeaveCommand(t)
args := []string{"-http-addr=" + a1.httpAddr, "appserver1"}
code := c.Run(args)
if code == 0 {

View File

@ -117,7 +117,10 @@ func init() {
"leave": func() (cli.Command, error) {
return &command.LeaveCommand{
Ui: ui,
Command: base.Command{
Flags: base.FlagSetClientHTTP,
Ui: ui,
},
}, nil
},

View File

@ -21,12 +21,8 @@ non-graceful leave can affect cluster availability.
## Usage
Usage: `consul leave`
Usage: `consul leave [options]`
The command-line flags are all optional. The list of available flags are:
* `-rpc-addr` - Address to the RPC server of the agent you want to contact
to send this command. If this isn't specified, the command checks the
CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
address will be set to "127.0.0.1:8400".
#### API Options
<%= partial "docs/commands/http_api_options_client" %>