commands: move operator autopilot get command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-17 08:37:16 +02:00 committed by Frank Schröder
parent bd1b189990
commit f719d78441
3 changed files with 41 additions and 32 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/hashicorp/consul/command/monitor" "github.com/hashicorp/consul/command/monitor"
"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/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"
@ -155,12 +156,7 @@ func init() {
}, },
"operator autopilot get-config": func() (cli.Command, error) { "operator autopilot get-config": func() (cli.Command, error) {
return &OperatorAutopilotGetCommand{ return operautoget.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
}, },
"operator autopilot set-config": func() (cli.Command, error) { "operator autopilot set-config": func() (cli.Command, error) {

View File

@ -1,33 +1,45 @@
package command package operautoget
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
) )
type OperatorAutopilotGetCommand struct { func New(ui cli.Ui) *cmd {
BaseCommand c := &cmd{UI: ui}
c.init()
return c
} }
func (c *OperatorAutopilotGetCommand) Help() string { type cmd struct {
c.InitFlagSet() UI cli.Ui
return c.HelpCommand(` flags *flag.FlagSet
Usage: consul operator autopilot get-config [options] http *flags.HTTPFlags
usage string
Displays the current Autopilot configuration.
`)
} }
func (c *OperatorAutopilotGetCommand) Synopsis() string { func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
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 *cmd) Synopsis() string {
return "Display the current Autopilot configuration" return "Display the current Autopilot configuration"
} }
func (c *OperatorAutopilotGetCommand) Run(args []string) int { func (c *cmd) Help() string {
c.InitFlagSet() 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
} }
@ -36,7 +48,7 @@ func (c *OperatorAutopilotGetCommand) 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
@ -44,7 +56,7 @@ func (c *OperatorAutopilotGetCommand) Run(args []string) int {
// Fetch the current configuration. // Fetch the current configuration.
opts := &api.QueryOptions{ opts := &api.QueryOptions{
AllowStale: c.HTTPStale(), AllowStale: c.http.Stale(),
} }
config, err := client.Operator().AutopilotGetConfiguration(opts) config, err := client.Operator().AutopilotGetConfiguration(opts)
if err != nil { if err != nil {
@ -61,3 +73,7 @@ func (c *OperatorAutopilotGetCommand) Run(args []string) int {
return 0 return 0
} }
const usage = `Usage: consul operator autopilot get-config [options]
Displays the current Autopilot configuration.`

View File

@ -1,4 +1,4 @@
package command package operautoget
import ( import (
"strings" "strings"
@ -8,9 +8,11 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestOperator_Autopilot_Get_Implements(t *testing.T) { func TestOperatorAutopilotGetCommand_noTabs(t *testing.T) {
t.Parallel() t.Parallel()
var _ cli.Command = &OperatorAutopilotGetCommand{} if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
t.Fatal("usage has tabs")
}
} }
func TestOperator_Autopilot_Get(t *testing.T) { func TestOperator_Autopilot_Get(t *testing.T) {
@ -19,12 +21,7 @@ func TestOperator_Autopilot_Get(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
ui := cli.NewMockUi() ui := cli.NewMockUi()
c := OperatorAutopilotGetCommand{ c := New(ui)
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetHTTP,
},
}
args := []string{"-http-addr=" + a.HTTPAddr()} args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args) code := c.Run(args)