From 9e156286c7606c84be054a3716b950de83603da1 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Wed, 8 Feb 2017 17:14:02 -0500 Subject: [PATCH] Convert join command to use base.Command --- command/join.go | 37 +++++++++---------- command/join_test.go | 26 ++++++++----- commands.go | 15 +++++--- ...n.html.markdown => join.html.markdown.erb} | 10 ++--- 4 files changed, 48 insertions(+), 40 deletions(-) rename website/source/docs/commands/{join.html.markdown => join.html.markdown.erb} (81%) diff --git a/command/join.go b/command/join.go index ea22e1906..88255e54b 100644 --- a/command/join.go +++ b/command/join.go @@ -1,16 +1,15 @@ package command import ( - "flag" "fmt" - "github.com/mitchellh/cli" + "github.com/hashicorp/consul/command/base" "strings" ) // JoinCommand is a Command implementation that tells a running Consul // agent to join another. type JoinCommand struct { - Ui cli.Ui + base.Command } func (c *JoinCommand) Help() string { @@ -20,26 +19,21 @@ Usage: consul join [options] address ... Tells a running Consul agent (with "consul agent") to join the cluster by specifying at least one existing member. -Options: +` + c.Command.Help() - -rpc-addr=127.0.0.1:8400 RPC address of the Consul agent. - -wan Joins a server to another server in the WAN pool -` return strings.TrimSpace(helpText) } func (c *JoinCommand) Run(args []string) int { var wan bool - cmdFlags := flag.NewFlagSet("join", flag.ContinueOnError) - cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } - cmdFlags.BoolVar(&wan, "wan", false, "wan") - rpcAddr := RPCAddrFlag(cmdFlags) - if err := cmdFlags.Parse(args); err != nil { + f := c.Command.NewFlagSet(c) + f.BoolVar(&wan, "wan", false, "Joins a server to another server in the WAN pool.") + if err := c.Command.Parse(args); err != nil { return 1 } - addrs := cmdFlags.Args() + addrs := f.Args() if len(addrs) == 0 { c.Ui.Error("At least one address to join must be specified.") c.Ui.Error("") @@ -47,21 +41,24 @@ func (c *JoinCommand) Run(args []string) int { return 1 } - client, err := RPCClient(*rpcAddr) + client, err := c.Command.HTTPClient() if err != nil { c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 } - defer client.Close() - n, err := client.Join(addrs, wan) - if err != nil { - c.Ui.Error(fmt.Sprintf("Error joining the cluster: %s", err)) - return 1 + joins := 0 + for _, addr := range addrs { + err := client.Agent().Join(addr, wan) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error joining address '%s': %s", addr, err)) + } else { + joins++ + } } c.Ui.Output(fmt.Sprintf( - "Successfully joined cluster by contacting %d nodes.", n)) + "Successfully joined cluster by contacting %d nodes.", joins)) return 0 } diff --git a/command/join_test.go b/command/join_test.go index bbbf2023a..5f6e26798 100644 --- a/command/join_test.go +++ b/command/join_test.go @@ -2,11 +2,22 @@ package command import ( "fmt" + "github.com/hashicorp/consul/command/base" "github.com/mitchellh/cli" "strings" "testing" ) +func testJoinCommand(t *testing.T) (*cli.MockUi, *JoinCommand) { + ui := new(cli.MockUi) + return ui, &JoinCommand{ + Command: base.Command{ + Ui: ui, + Flags: base.FlagSetClientHTTP, + }, + } +} + func TestJoinCommand_implements(t *testing.T) { var _ cli.Command = &JoinCommand{} } @@ -17,10 +28,9 @@ func TestJoinCommandRun(t *testing.T) { defer a1.Shutdown() defer a2.Shutdown() - ui := new(cli.MockUi) - c := &JoinCommand{Ui: ui} + ui, c := testJoinCommand(t) args := []string{ - "-rpc-addr=" + a1.addr, + "-http-addr=" + a1.httpAddr, fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan), } @@ -40,10 +50,9 @@ func TestJoinCommandRun_wan(t *testing.T) { defer a1.Shutdown() defer a2.Shutdown() - ui := new(cli.MockUi) - c := &JoinCommand{Ui: ui} + ui, c := testJoinCommand(t) args := []string{ - "-rpc-addr=" + a1.addr, + "-http-addr=" + a1.httpAddr, "-wan", fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfWan), } @@ -59,9 +68,8 @@ func TestJoinCommandRun_wan(t *testing.T) { } func TestJoinCommandRun_noAddrs(t *testing.T) { - ui := new(cli.MockUi) - c := &JoinCommand{Ui: ui} - args := []string{"-rpc-addr=foo"} + ui, c := testJoinCommand(t) + args := []string{"-http-addr=foo"} code := c.Run(args) if code != 1 { diff --git a/commands.go b/commands.go index c8a85b30c..36a96c6c1 100644 --- a/commands.go +++ b/commands.go @@ -76,6 +76,15 @@ func init() { }, nil }, + "join": func() (cli.Command, error) { + return &command.JoinCommand{ + Command: base.Command{ + Ui: ui, + Flags: base.FlagSetClientHTTP, + }, + }, nil + }, + "kv": func() (cli.Command, error) { return &command.KVCommand{ Ui: ui, @@ -112,12 +121,6 @@ func init() { }, nil }, - "join": func() (cli.Command, error) { - return &command.JoinCommand{ - Ui: ui, - }, nil - }, - "keygen": func() (cli.Command, error) { return &command.KeygenCommand{ Ui: ui, diff --git a/website/source/docs/commands/join.html.markdown b/website/source/docs/commands/join.html.markdown.erb similarity index 81% rename from website/source/docs/commands/join.html.markdown rename to website/source/docs/commands/join.html.markdown.erb index cd9d624fc..298ab5114 100644 --- a/website/source/docs/commands/join.html.markdown +++ b/website/source/docs/commands/join.html.markdown.erb @@ -31,14 +31,14 @@ You may call join with multiple addresses if you want to try to join multiple clusters. Consul will attempt to join all addresses, and the join command will fail only if Consul was unable to join with any. -The command-line flags are all optional. The list of available flags are: +#### API Options + +<%= partial "docs/commands/http_api_options_client" %> + +#### Command Options * `-wan` - For agents running in server mode, the agent will attempt to join other servers gossiping in a WAN cluster. This is used to form a bridge between multiple datacenters. -* `-rpc-addr` - Address to the RPC server of the agent you want to contact - to send this command. If this isn't specified, the command checks the - CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC - address will be set to "127.0.0.1:8400".