commands: move operator autopilot set command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-17 08:42:52 +02:00 committed by Frank Schröder
parent f719d78441
commit 4947ba7f3d
3 changed files with 47 additions and 42 deletions

View File

@ -30,6 +30,7 @@ import (
"github.com/hashicorp/consul/command/oper" "github.com/hashicorp/consul/command/oper"
"github.com/hashicorp/consul/command/operauto" "github.com/hashicorp/consul/command/operauto"
"github.com/hashicorp/consul/command/operautoget" "github.com/hashicorp/consul/command/operautoget"
"github.com/hashicorp/consul/command/operautoset"
"github.com/hashicorp/consul/command/operraft" "github.com/hashicorp/consul/command/operraft"
"github.com/hashicorp/consul/command/operraftlist" "github.com/hashicorp/consul/command/operraftlist"
"github.com/hashicorp/consul/command/operraftremove" "github.com/hashicorp/consul/command/operraftremove"
@ -160,12 +161,7 @@ func init() {
}, },
"operator autopilot set-config": func() (cli.Command, error) { "operator autopilot set-config": func() (cli.Command, error) {
return &OperatorAutopilotSetCommand{ return operautoset.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
}, },
"operator raft": func() (cli.Command, error) { "operator raft": func() (cli.Command, error) {

View File

@ -1,4 +1,4 @@
package command package operautoset
import ( import (
"flag" "flag"
@ -7,10 +7,20 @@ import (
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags" "github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
) )
type OperatorAutopilotSetCommand struct { func New(ui cli.Ui) *cmd {
BaseCommand c := &cmd{UI: ui}
c.init()
return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
usage string
// flags // flags
cleanupDeadServers flags.BoolValue cleanupDeadServers flags.BoolValue
@ -22,51 +32,49 @@ type OperatorAutopilotSetCommand struct {
upgradeVersionTag flags.StringValue upgradeVersionTag flags.StringValue
} }
func (c *OperatorAutopilotSetCommand) initFlags() { func (c *cmd) init() {
c.InitFlagSet() c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.FlagSet.Var(&c.cleanupDeadServers, "cleanup-dead-servers", c.flags.Var(&c.cleanupDeadServers, "cleanup-dead-servers",
"Controls whether Consul will automatically remove dead servers "+ "Controls whether Consul will automatically remove dead servers "+
"when new ones are successfully added. Must be one of `true|false`.") "when new ones are successfully added. Must be one of `true|false`.")
c.FlagSet.Var(&c.maxTrailingLogs, "max-trailing-logs", c.flags.Var(&c.maxTrailingLogs, "max-trailing-logs",
"Controls the maximum number of log entries that a server can trail the "+ "Controls the maximum number of log entries that a server can trail the "+
"leader by before being considered unhealthy.") "leader by before being considered unhealthy.")
c.FlagSet.Var(&c.lastContactThreshold, "last-contact-threshold", c.flags.Var(&c.lastContactThreshold, "last-contact-threshold",
"Controls the maximum amount of time a server can go without contact "+ "Controls the maximum amount of time a server can go without contact "+
"from the leader before being considered unhealthy. Must be a duration value "+ "from the leader before being considered unhealthy. Must be a duration value "+
"such as `200ms`.") "such as `200ms`.")
c.FlagSet.Var(&c.serverStabilizationTime, "server-stabilization-time", c.flags.Var(&c.serverStabilizationTime, "server-stabilization-time",
"Controls the minimum amount of time a server must be stable in the "+ "Controls the minimum amount of time a server must be stable in the "+
"'healthy' state before being added to the cluster. Only takes effect if all "+ "'healthy' state before being added to the cluster. Only takes effect if all "+
"servers are running Raft protocol version 3 or higher. Must be a duration "+ "servers are running Raft protocol version 3 or higher. Must be a duration "+
"value such as `10s`.") "value such as `10s`.")
c.FlagSet.Var(&c.redundancyZoneTag, "redundancy-zone-tag", c.flags.Var(&c.redundancyZoneTag, "redundancy-zone-tag",
"(Enterprise-only) Controls the node_meta tag name used for separating servers into "+ "(Enterprise-only) Controls the node_meta tag name used for separating servers into "+
"different redundancy zones.") "different redundancy zones.")
c.FlagSet.Var(&c.disableUpgradeMigration, "disable-upgrade-migration", c.flags.Var(&c.disableUpgradeMigration, "disable-upgrade-migration",
"(Enterprise-only) Controls whether Consul will avoid promoting new servers until "+ "(Enterprise-only) Controls whether Consul will avoid promoting new servers until "+
"it can perform a migration. Must be one of `true|false`.") "it can perform a migration. Must be one of `true|false`.")
c.FlagSet.Var(&c.upgradeVersionTag, "upgrade-version-tag", c.flags.Var(&c.upgradeVersionTag, "upgrade-version-tag",
"(Enterprise-only) The node_meta tag to use for version info when performing upgrade "+ "(Enterprise-only) The node_meta tag to use for version info when performing upgrade "+
"migrations. If left blank, the Consul version will be used.") "migrations. If left blank, the Consul version will be used.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
} }
func (c *OperatorAutopilotSetCommand) Help() string { func (c *cmd) Synopsis() string {
c.initFlags()
return c.HelpCommand(`
Usage: consul operator autopilot set-config [options]
Modifies the current Autopilot configuration.
`)
}
func (c *OperatorAutopilotSetCommand) Synopsis() string {
return "Modify the current Autopilot configuration" return "Modify the current Autopilot configuration"
} }
func (c *OperatorAutopilotSetCommand) Run(args []string) int { func (c *cmd) Help() string {
c.initFlags() return c.usage
if err := c.FlagSet.Parse(args); err != nil { }
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
if err == flag.ErrHelp { if err == flag.ErrHelp {
return 0 return 0
} }
@ -75,7 +83,7 @@ func (c *OperatorAutopilotSetCommand) Run(args []string) int {
} }
// Set up a client. // Set up a client.
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error initializing client: %s", err)) c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1 return 1
@ -120,3 +128,7 @@ func (c *OperatorAutopilotSetCommand) Run(args []string) int {
c.UI.Output("Configuration could not be atomically updated, please try again") c.UI.Output("Configuration could not be atomically updated, please try again")
return 1 return 1
} }
const usage = `Usage: consul operator autopilot set-config [options]
Modifies the current Autopilot configuration.`

View File

@ -1,4 +1,4 @@
package command package operautoset
import ( import (
"strings" "strings"
@ -10,9 +10,11 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestOperator_Autopilot_Set_Implements(t *testing.T) { func TestOperatorAutopilotSetCommand_noTabs(t *testing.T) {
t.Parallel() t.Parallel()
var _ cli.Command = &OperatorAutopilotSetCommand{} if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
t.Fatal("usage has tabs")
}
} }
func TestOperator_Autopilot_Set(t *testing.T) { func TestOperator_Autopilot_Set(t *testing.T) {
@ -21,12 +23,7 @@ func TestOperator_Autopilot_Set(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
ui := cli.NewMockUi() ui := cli.NewMockUi()
c := OperatorAutopilotSetCommand{ c := New(ui)
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetHTTP,
},
}
args := []string{ args := []string{
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),
"-cleanup-dead-servers=false", "-cleanup-dead-servers=false",