commands: move operator raft list-peers command to separate pkg

pull/3584/head
Frank Schroeder 7 years ago committed by Frank Schröder
parent f10f07b448
commit 2a824f0575

@ -29,6 +29,7 @@ import (
"github.com/hashicorp/consul/command/monitor"
"github.com/hashicorp/consul/command/oper"
"github.com/hashicorp/consul/command/operraft"
"github.com/hashicorp/consul/command/operraftlist"
"github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version"
"github.com/mitchellh/cli"
@ -179,12 +180,7 @@ func init() {
},
"operator raft list-peers": func() (cli.Command, error) {
return &OperatorRaftListCommand{
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
return operraftlist.New(ui), nil
},
"operator raft remove-peer": func() (cli.Command, error) {

@ -1,45 +0,0 @@
package command
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/consul/agent"
"github.com/mitchellh/cli"
)
func TestOperator_Raft_ListPeers_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &OperatorRaftListCommand{}
}
func TestOperator_Raft_ListPeers(t *testing.T) {
t.Parallel()
a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown()
expected := fmt.Sprintf("%s %s 127.0.0.1:%d leader true 3",
a.Config.NodeName, a.Config.NodeID, a.Config.ServerPort)
// Test the list-peers subcommand directly
{
ui := cli.NewMockUi()
c := OperatorRaftListCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetHTTP,
},
}
args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
output := strings.TrimSpace(ui.OutputWriter.String())
if !strings.Contains(output, expected) {
t.Fatalf("bad: %q, %q", output, expected)
}
}
}

@ -1,34 +1,46 @@
package command
package operraftlist
import (
"flag"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
"github.com/ryanuber/columnize"
)
type OperatorRaftListCommand struct {
BaseCommand
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.init()
return c
}
func (c *OperatorRaftListCommand) Help() string {
c.InitFlagSet()
return c.HelpCommand(`
Usage: consul operator raft list-peers [options]
Displays the current Raft peer configuration.
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
usage 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 *OperatorRaftListCommand) Synopsis() string {
func (c *cmd) Synopsis() string {
return "Display the current Raft peer configuration"
}
func (c *OperatorRaftListCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
func (c *cmd) Help() string {
return c.usage
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
if err == flag.ErrHelp {
return 0
}
@ -37,14 +49,14 @@ func (c *OperatorRaftListCommand) Run(args []string) int {
}
// Set up a client.
client, err := c.HTTPClient()
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Fetch the current configuration.
result, err := raftListPeers(client, c.HTTPStale())
result, err := raftListPeers(client, c.http.Stale())
if err != nil {
c.UI.Error(fmt.Sprintf("Error getting peers: %v", err))
return 1
@ -55,7 +67,6 @@ func (c *OperatorRaftListCommand) Run(args []string) int {
}
func raftListPeers(client *api.Client, stale bool) (string, error) {
q := &api.QueryOptions{
AllowStale: stale,
}
@ -82,3 +93,7 @@ func raftListPeers(client *api.Client, stale bool) (string, error) {
return columnize.SimpleFormat(result), nil
}
const usage = `Usage: consul operator raft list-peers [options]
Displays the current Raft peer configuration.`

@ -0,0 +1,40 @@
package operraftlist
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/consul/agent"
"github.com/mitchellh/cli"
)
func TestOperatorRaftListPeersCommand_noTabs(t *testing.T) {
t.Parallel()
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
t.Fatal("usage has tabs")
}
}
func TestOperatorRaftListPeersCommandRun(t *testing.T) {
t.Parallel()
a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown()
expected := fmt.Sprintf("%s %s 127.0.0.1:%d leader true 3",
a.Config.NodeName, a.Config.NodeID, a.Config.ServerPort)
// Test the list-peers subcommand directly
ui := cli.NewMockUi()
c := New(ui)
args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
output := strings.TrimSpace(ui.OutputWriter.String())
if !strings.Contains(output, expected) {
t.Fatalf("bad: %q, %q", output, expected)
}
}
Loading…
Cancel
Save