Convert leave command to use base.Command
This commit is contained in:
parent
41ca1ccebc
commit
e385af8eeb
|
@ -1,53 +1,47 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/hashicorp/consul/command/base"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LeaveCommand is a Command implementation that instructs
|
// LeaveCommand is a Command implementation that instructs
|
||||||
// the Consul agent to gracefully leave the cluster
|
// the Consul agent to gracefully leave the cluster
|
||||||
type LeaveCommand struct {
|
type LeaveCommand struct {
|
||||||
Ui cli.Ui
|
base.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LeaveCommand) Help() string {
|
func (c *LeaveCommand) Help() string {
|
||||||
helpText := `
|
helpText := `
|
||||||
Usage: consul leave
|
Usage: consul leave [options]
|
||||||
|
|
||||||
Causes the agent to gracefully leave the Consul cluster and shutdown.
|
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)
|
return strings.TrimSpace(helpText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LeaveCommand) Run(args []string) int {
|
func (c *LeaveCommand) Run(args []string) int {
|
||||||
cmdFlags := flag.NewFlagSet("leave", flag.ContinueOnError)
|
f := c.Command.NewFlagSet(c)
|
||||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
if err := c.Command.Parse(args); err != nil {
|
||||||
rpcAddr := RPCAddrFlag(cmdFlags)
|
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
nonFlagArgs := cmdFlags.Args()
|
nonFlagArgs := f.Args()
|
||||||
if len(nonFlagArgs) > 0 {
|
if len(nonFlagArgs) > 0 {
|
||||||
c.Ui.Error(fmt.Sprintf("Error found unexpected args: %v", nonFlagArgs))
|
c.Ui.Error(fmt.Sprintf("Error found unexpected args: %v", nonFlagArgs))
|
||||||
c.Ui.Output(c.Help())
|
c.Ui.Output(c.Help())
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := RPCClient(*rpcAddr)
|
client, err := c.Command.HTTPClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||||
return 1
|
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))
|
c.Ui.Error(fmt.Sprintf("Error leaving: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,22 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/hashicorp/consul/command/base"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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) {
|
func TestLeaveCommand_implements(t *testing.T) {
|
||||||
var _ cli.Command = &LeaveCommand{}
|
var _ cli.Command = &LeaveCommand{}
|
||||||
}
|
}
|
||||||
|
@ -14,9 +25,8 @@ func TestLeaveCommandRun(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testLeaveCommand(t)
|
||||||
c := &LeaveCommand{Ui: ui}
|
args := []string{"-http-addr=" + a1.httpAddr}
|
||||||
args := []string{"-rpc-addr=" + a1.addr}
|
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
|
@ -32,9 +42,8 @@ func TestLeaveCommandFailOnNonFlagArgs(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
_, c := testLeaveCommand(t)
|
||||||
c := &LeaveCommand{Ui: ui}
|
args := []string{"-http-addr=" + a1.httpAddr, "appserver1"}
|
||||||
args := []string{"-rpc-addr=" + a1.addr, "appserver1"}
|
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
if code == 0 {
|
if code == 0 {
|
||||||
|
|
|
@ -117,7 +117,10 @@ func init() {
|
||||||
|
|
||||||
"leave": func() (cli.Command, error) {
|
"leave": func() (cli.Command, error) {
|
||||||
return &command.LeaveCommand{
|
return &command.LeaveCommand{
|
||||||
Ui: ui,
|
Command: base.Command{
|
||||||
|
Flags: base.FlagSetClientHTTP,
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -21,12 +21,8 @@ non-graceful leave can affect cluster availability.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Usage: `consul leave`
|
Usage: `consul leave [options]`
|
||||||
|
|
||||||
The command-line flags are all optional. The list of available flags are:
|
#### API Options
|
||||||
|
|
||||||
* `-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".
|
|
||||||
|
|
||||||
|
<%= partial "docs/commands/http_api_options_client" %>
|
Loading…
Reference in New Issue