Convert reload command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-09 19:32:22 -05:00
parent 369b4b6d73
commit 8c14f93fb1
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
5 changed files with 41 additions and 27 deletions

View File

@ -1,16 +1,15 @@
package command
import (
"flag"
"fmt"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/base"
"strings"
)
// ReloadCommand is a Command implementation that instructs
// the Consul agent to reload configurations
type ReloadCommand struct {
Ui cli.Ui
base.Command
}
func (c *ReloadCommand) Help() string {
@ -20,29 +19,25 @@ Usage: consul reload
Causes the agent to reload configurations. This can be used instead
of sending the SIGHUP signal to the agent.
Options:
` + c.Command.Help()
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent.
`
return strings.TrimSpace(helpText)
}
func (c *ReloadCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("reload", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
rpcAddr := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
c.Command.NewFlagSet(c)
if err := c.Command.Parse(args); err != nil {
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()
if err := client.Reload(); err != nil {
if err := client.Agent().Reload(); err != nil {
c.Ui.Error(fmt.Sprintf("Error reloading: %s", err))
return 1
}

View File

@ -1,9 +1,11 @@
package command
import (
"github.com/mitchellh/cli"
"strings"
"testing"
"github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli"
)
func TestReloadCommand_implements(t *testing.T) {
@ -11,12 +13,24 @@ func TestReloadCommand_implements(t *testing.T) {
}
func TestReloadCommandRun(t *testing.T) {
a1 := testAgent(t)
reloadCh := make(chan chan error)
a1 := testAgentWithConfigReload(t, nil, reloadCh)
defer a1.Shutdown()
// Setup a dummy response to errCh to simulate a successful reload
go func() {
errCh := <- reloadCh
errCh <- nil
}()
ui := new(cli.MockUi)
c := &ReloadCommand{Ui: ui}
args := []string{"-rpc-addr=" + a1.addr}
c := &ReloadCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetClientHTTP,
},
}
args := []string{"-http-addr=" + a1.httpAddr}
code := c.Run(args)
if code != 0 {

View File

@ -44,7 +44,7 @@ func (a *agentWrapper) Shutdown() {
}
func testAgent(t *testing.T) *agentWrapper {
return testAgentWithConfig(t, func(c *agent.Config) {})
return testAgentWithConfig(t, nil)
}
func testAgentWithAPIClient(t *testing.T) (*agentWrapper, *api.Client) {
@ -57,6 +57,10 @@ func testAgentWithAPIClient(t *testing.T) (*agentWrapper, *api.Client) {
}
func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper {
return testAgentWithConfigReload(t, cb, nil)
}
func testAgentWithConfigReload(t *testing.T, cb func(c *agent.Config), reloadCh chan chan error) *agentWrapper {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("err: %s", err)
@ -66,7 +70,9 @@ func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper {
mult := io.MultiWriter(os.Stderr, lw)
conf := nextConfig()
cb(conf)
if cb != nil {
cb(conf)
}
dir, err := ioutil.TempDir("", "agent")
if err != nil {
@ -74,7 +80,7 @@ func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper {
}
conf.DataDir = dir
a, err := agent.Create(conf, lw, nil, nil)
a, err := agent.Create(conf, lw, nil, reloadCh)
if err != nil {
os.RemoveAll(dir)
t.Fatalf(fmt.Sprintf("err: %v", err))

View File

@ -179,7 +179,10 @@ func init() {
"reload": func() (cli.Command, error) {
return &command.ReloadCommand{
Ui: ui,
Command: base.Command{
Flags: base.FlagSetClientHTTP,
Ui: ui,
},
}, nil
},

View File

@ -29,10 +29,6 @@ section on the agent options page for details on which options are supported.
Usage: `consul reload`
The command-line flags are all optional. The list of available flags are:
* `-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".
#### API Options
<%= partial "docs/commands/http_api_options_client" %>