mirror of
https://github.com/status-im/consul.git
synced 2025-02-05 18:33:48 +00:00
Move members command to its own package
This commit is contained in:
parent
ba546b0c1d
commit
5b6f0504ae
@ -25,6 +25,7 @@ import (
|
|||||||
"github.com/hashicorp/consul/command/leave"
|
"github.com/hashicorp/consul/command/leave"
|
||||||
"github.com/hashicorp/consul/command/lock"
|
"github.com/hashicorp/consul/command/lock"
|
||||||
"github.com/hashicorp/consul/command/maint"
|
"github.com/hashicorp/consul/command/maint"
|
||||||
|
"github.com/hashicorp/consul/command/members"
|
||||||
"github.com/hashicorp/consul/command/validate"
|
"github.com/hashicorp/consul/command/validate"
|
||||||
"github.com/hashicorp/consul/version"
|
"github.com/hashicorp/consul/version"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
@ -132,12 +133,7 @@ func init() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
"members": func() (cli.Command, error) {
|
"members": func() (cli.Command, error) {
|
||||||
return &MembersCommand{
|
return members.New(ui), nil
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetClientHTTP,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"monitor": func() (cli.Command, error) {
|
"monitor": func() (cli.Command, error) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package command
|
package members
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -7,16 +7,22 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"flag"
|
||||||
|
|
||||||
consulapi "github.com/hashicorp/consul/api"
|
consulapi "github.com/hashicorp/consul/api"
|
||||||
|
"github.com/hashicorp/consul/command/flags"
|
||||||
"github.com/hashicorp/serf/serf"
|
"github.com/hashicorp/serf/serf"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
"github.com/ryanuber/columnize"
|
"github.com/ryanuber/columnize"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MembersCommand is a Command implementation that queries a running
|
// cmd is a Command implementation that queries a running
|
||||||
// Consul agent what members are part of the cluster currently.
|
// Consul agent what members are part of the cluster currently.
|
||||||
type MembersCommand struct {
|
type cmd struct {
|
||||||
BaseCommand
|
UI cli.Ui
|
||||||
|
usage string
|
||||||
|
flags *flag.FlagSet
|
||||||
|
http *flags.HTTPFlags
|
||||||
// flags
|
// flags
|
||||||
detailed bool
|
detailed bool
|
||||||
wan bool
|
wan bool
|
||||||
@ -24,34 +30,37 @@ type MembersCommand struct {
|
|||||||
segment string
|
segment string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MembersCommand) initFlags() {
|
func New(ui cli.Ui) *cmd {
|
||||||
c.InitFlagSet()
|
c := &cmd{UI: ui}
|
||||||
c.FlagSet.BoolVar(&c.detailed, "detailed", false,
|
c.init()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) init() {
|
||||||
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
c.http = &flags.HTTPFlags{}
|
||||||
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||||||
|
|
||||||
|
c.flags.BoolVar(&c.detailed, "detailed", false,
|
||||||
"Provides detailed information about nodes.")
|
"Provides detailed information about nodes.")
|
||||||
c.FlagSet.BoolVar(&c.wan, "wan", false,
|
c.flags.BoolVar(&c.wan, "wan", false,
|
||||||
"If the agent is in server mode, this can be used to return the other "+
|
"If the agent is in server mode, this can be used to return the other "+
|
||||||
"peers in the WAN pool.")
|
"peers in the WAN pool.")
|
||||||
c.FlagSet.StringVar(&c.statusFilter, "status", ".*",
|
c.flags.StringVar(&c.statusFilter, "status", ".*",
|
||||||
"If provided, output is filtered to only nodes matching the regular "+
|
"If provided, output is filtered to only nodes matching the regular "+
|
||||||
"expression for status.")
|
"expression for status.")
|
||||||
c.FlagSet.StringVar(&c.segment, "segment", consulapi.AllSegments,
|
c.flags.StringVar(&c.segment, "segment", consulapi.AllSegments,
|
||||||
"(Enterprise-only) If provided, output is filtered to only nodes in"+
|
"(Enterprise-only) If provided, output is filtered to only nodes in"+
|
||||||
"the given segment.")
|
"the given segment.")
|
||||||
|
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MembersCommand) Help() string {
|
func (c *cmd) Help() string {
|
||||||
c.initFlags()
|
return c.usage
|
||||||
return c.HelpCommand(`
|
|
||||||
Usage: consul members [options]
|
|
||||||
|
|
||||||
Outputs the members of a running Consul agent.
|
|
||||||
|
|
||||||
`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MembersCommand) Run(args []string) int {
|
func (c *cmd) Run(args []string) int {
|
||||||
c.initFlags()
|
if err := c.flags.Parse(args); err != nil {
|
||||||
if err := c.FlagSet.Parse(args); err != nil {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +71,7 @@ func (c *MembersCommand) Run(args []string) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := c.HTTPClient()
|
client, err := c.http.APIClient()
|
||||||
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
|
||||||
@ -139,7 +148,7 @@ func (m ByMemberNameAndSegment) Less(i, j int) bool {
|
|||||||
|
|
||||||
// standardOutput is used to dump the most useful information about nodes
|
// standardOutput is used to dump the most useful information about nodes
|
||||||
// in a more human-friendly format
|
// in a more human-friendly format
|
||||||
func (c *MembersCommand) standardOutput(members []*consulapi.AgentMember) []string {
|
func (c *cmd) standardOutput(members []*consulapi.AgentMember) []string {
|
||||||
result := make([]string, 0, len(members))
|
result := make([]string, 0, len(members))
|
||||||
header := "Node|Address|Status|Type|Build|Protocol|DC|Segment"
|
header := "Node|Address|Status|Type|Build|Protocol|DC|Segment"
|
||||||
result = append(result, header)
|
result = append(result, header)
|
||||||
@ -176,7 +185,7 @@ func (c *MembersCommand) standardOutput(members []*consulapi.AgentMember) []stri
|
|||||||
|
|
||||||
// detailedOutput is used to dump all known information about nodes in
|
// detailedOutput is used to dump all known information about nodes in
|
||||||
// their raw format
|
// their raw format
|
||||||
func (c *MembersCommand) detailedOutput(members []*consulapi.AgentMember) []string {
|
func (c *cmd) detailedOutput(members []*consulapi.AgentMember) []string {
|
||||||
result := make([]string, 0, len(members))
|
result := make([]string, 0, len(members))
|
||||||
header := "Node|Address|Status|Tags"
|
header := "Node|Address|Status|Tags"
|
||||||
result = append(result, header)
|
result = append(result, header)
|
||||||
@ -204,6 +213,13 @@ func (c *MembersCommand) detailedOutput(members []*consulapi.AgentMember) []stri
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MembersCommand) Synopsis() string {
|
func (c *cmd) Synopsis() string {
|
||||||
return "Lists the members of a Consul cluster"
|
return "Lists the members of a Consul cluster"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const usage = `
|
||||||
|
Usage: consul members [options]
|
||||||
|
|
||||||
|
Outputs the members of a running Consul agent.
|
||||||
|
|
||||||
|
`
|
@ -1,4 +1,4 @@
|
|||||||
package command
|
package members
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -9,27 +9,15 @@ import (
|
|||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testMembersCommand(t *testing.T) (*cli.MockUi, *MembersCommand) {
|
|
||||||
ui := cli.NewMockUi()
|
|
||||||
return ui, &MembersCommand{
|
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
UI: ui,
|
|
||||||
Flags: FlagSetClientHTTP,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMembersCommand_implements(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
var _ cli.Command = &MembersCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMembersCommandRun(t *testing.T) {
|
func TestMembersCommandRun(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
a := agent.NewTestAgent(t.Name(), ``)
|
a := agent.NewTestAgent(t.Name(), ``)
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
ui, c := testMembersCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
c.flags.SetOutput(ui.ErrorWriter)
|
||||||
|
|
||||||
args := []string{"-http-addr=" + a.HTTPAddr()}
|
args := []string{"-http-addr=" + a.HTTPAddr()}
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
@ -58,7 +46,10 @@ func TestMembersCommandRun_WAN(t *testing.T) {
|
|||||||
a := agent.NewTestAgent(t.Name(), ``)
|
a := agent.NewTestAgent(t.Name(), ``)
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
ui, c := testMembersCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
c.flags.SetOutput(ui.ErrorWriter)
|
||||||
|
|
||||||
args := []string{"-http-addr=" + a.HTTPAddr(), "-wan"}
|
args := []string{"-http-addr=" + a.HTTPAddr(), "-wan"}
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
@ -76,7 +67,10 @@ func TestMembersCommandRun_statusFilter(t *testing.T) {
|
|||||||
a := agent.NewTestAgent(t.Name(), ``)
|
a := agent.NewTestAgent(t.Name(), ``)
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
ui, c := testMembersCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
c.flags.SetOutput(ui.ErrorWriter)
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
"-status=a.*e",
|
"-status=a.*e",
|
||||||
@ -97,7 +91,10 @@ func TestMembersCommandRun_statusFilter_failed(t *testing.T) {
|
|||||||
a := agent.NewTestAgent(t.Name(), ``)
|
a := agent.NewTestAgent(t.Name(), ``)
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
ui, c := testMembersCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
c.flags.SetOutput(ui.ErrorWriter)
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
"-status=(fail|left)",
|
"-status=(fail|left)",
|
Loading…
x
Reference in New Issue
Block a user