Convert maint command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-09 16:50:45 -05:00
parent ccb193f77f
commit 6c4d4f9a32
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
4 changed files with 36 additions and 49 deletions

View File

@ -1,18 +1,16 @@
package command
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/consul/api"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/base"
)
// MaintCommand is a Command implementation that enables or disables
// node or service maintenance mode.
type MaintCommand struct {
Ui cli.Ui
base.Command
}
func (c *MaintCommand) Help() string {
@ -40,15 +38,8 @@ Usage: consul maint [options]
If no arguments are given, the agent's maintenance status will be shown.
This will return blank if nothing is currently under maintenance.
Options:
` + c.Command.Help()
-enable Enable maintenance mode.
-disable Disable maintenance mode.
-reason=<string> Text string describing the maintenance reason
-service=<serviceID> Control maintenance mode for a specific service ID
-token="" ACL token to use. Defaults to that of agent.
-http-addr=127.0.0.1:8500 HTTP address of the Consul agent.
`
return strings.TrimSpace(helpText)
}
@ -57,19 +48,15 @@ func (c *MaintCommand) Run(args []string) int {
var disable bool
var reason string
var serviceID string
var token string
cmdFlags := flag.NewFlagSet("maint", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
f := c.Command.NewFlagSet(c)
cmdFlags.BoolVar(&enable, "enable", false, "enable maintenance mode")
cmdFlags.BoolVar(&disable, "disable", false, "disable maintenance mode")
cmdFlags.StringVar(&reason, "reason", "", "maintenance reason")
cmdFlags.StringVar(&serviceID, "service", "", "service maintenance")
cmdFlags.StringVar(&token, "token", "", "")
httpAddr := HTTPAddrFlag(cmdFlags)
f.BoolVar(&enable, "enable", false, "Enable maintenance mode.")
f.BoolVar(&disable, "disable", false, "Disable maintenance mode.")
f.StringVar(&reason, "reason", "", "Text describing the maintenance reason.")
f.StringVar(&serviceID, "service", "", "Control maintenance mode for a specific service ID.")
if err := cmdFlags.Parse(args); err != nil {
if err := c.Command.Parse(args); err != nil {
return 1
}
@ -88,10 +75,7 @@ func (c *MaintCommand) Run(args []string) int {
}
// Create and test the HTTP client
conf := api.DefaultConfig()
conf.Address = *httpAddr
conf.Token = token
client, err := api.NewClient(conf)
client, err := c.Command.HTTPClient()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1

View File

@ -4,17 +4,27 @@ import (
"strings"
"testing"
"github.com/hashicorp/consul/command/base"
"github.com/hashicorp/consul/consul/structs"
"github.com/mitchellh/cli"
)
func testMaintCommand(t *testing.T) (*cli.MockUi, *MaintCommand) {
ui := new(cli.MockUi)
return ui, &MaintCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetClientHTTP,
},
}
}
func TestMaintCommand_implements(t *testing.T) {
var _ cli.Command = &MaintCommand{}
}
func TestMaintCommandRun_ConflictingArgs(t *testing.T) {
ui := new(cli.MockUi)
c := &MaintCommand{Ui: ui}
_, c := testMaintCommand(t)
if code := c.Run([]string{"-enable", "-disable"}); code != 1 {
t.Fatalf("expected return code 1, got %d", code)
@ -53,8 +63,7 @@ func TestMaintCommandRun_NoArgs(t *testing.T) {
a1.agent.EnableNodeMaintenance("broken 2", "")
// Run consul maint with no args (list mode)
ui := new(cli.MockUi)
c := &MaintCommand{Ui: ui}
ui, c := testMaintCommand(t)
args := []string{"-http-addr=" + a1.httpAddr}
code := c.Run(args)
@ -84,8 +93,7 @@ func TestMaintCommandRun_EnableNodeMaintenance(t *testing.T) {
a1 := testAgent(t)
defer a1.Shutdown()
ui := new(cli.MockUi)
c := &MaintCommand{Ui: ui}
ui, c := testMaintCommand(t)
args := []string{
"-http-addr=" + a1.httpAddr,
@ -106,8 +114,7 @@ func TestMaintCommandRun_DisableNodeMaintenance(t *testing.T) {
a1 := testAgent(t)
defer a1.Shutdown()
ui := new(cli.MockUi)
c := &MaintCommand{Ui: ui}
ui, c := testMaintCommand(t)
args := []string{
"-http-addr=" + a1.httpAddr,
@ -136,8 +143,7 @@ func TestMaintCommandRun_EnableServiceMaintenance(t *testing.T) {
t.Fatalf("err: %v", err)
}
ui := new(cli.MockUi)
c := &MaintCommand{Ui: ui}
ui, c := testMaintCommand(t)
args := []string{
"-http-addr=" + a1.httpAddr,
@ -168,8 +174,7 @@ func TestMaintCommandRun_DisableServiceMaintenance(t *testing.T) {
t.Fatalf("err: %v", err)
}
ui := new(cli.MockUi)
c := &MaintCommand{Ui: ui}
ui, c := testMaintCommand(t)
args := []string{
"-http-addr=" + a1.httpAddr,
@ -190,8 +195,7 @@ func TestMaintCommandRun_ServiceMaintenance_NoService(t *testing.T) {
a1 := testAgent(t)
defer a1.Shutdown()
ui := new(cli.MockUi)
c := &MaintCommand{Ui: ui}
ui, c := testMaintCommand(t)
args := []string{
"-http-addr=" + a1.httpAddr,

View File

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

View File

@ -24,9 +24,11 @@ health check.
Usage: `consul maint [options]`
All of the command line arguments are optional.
#### API Options
The list of available flags are:
<%= partial "docs/commands/http_api_options_client" %>
#### Command Options
* `-enable` - Enable maintenance mode on a given service or node. If
combined with the `-service` flag, we operate on a specific service ID.
@ -44,12 +46,6 @@ The list of available flags are:
providing this flag, the `-enable` and `-disable` flags functionality is
modified to operate on the given service ID.
* `-token` - ACL token to use. Defaults to that of agent.
* `-http-addr` - Address to the HTTP server of the agent you want to contact
to send this command. If this isn't specified, the command will contact
"127.0.0.1:8500" which is the default HTTP address of a Consul agent.
## List mode
If neither `-enable` nor `-disable` are passed, the `maint` command will