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

View File

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

View File

@ -44,7 +44,7 @@ func (a *agentWrapper) Shutdown() {
} }
func testAgent(t *testing.T) *agentWrapper { 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) { 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 { 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") l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil { if err != nil {
t.Fatalf("err: %s", err) 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) mult := io.MultiWriter(os.Stderr, lw)
conf := nextConfig() conf := nextConfig()
cb(conf) if cb != nil {
cb(conf)
}
dir, err := ioutil.TempDir("", "agent") dir, err := ioutil.TempDir("", "agent")
if err != nil { if err != nil {
@ -74,7 +80,7 @@ func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper {
} }
conf.DataDir = dir conf.DataDir = dir
a, err := agent.Create(conf, lw, nil, nil) a, err := agent.Create(conf, lw, nil, reloadCh)
if err != nil { if err != nil {
os.RemoveAll(dir) os.RemoveAll(dir)
t.Fatalf(fmt.Sprintf("err: %v", err)) t.Fatalf(fmt.Sprintf("err: %v", err))

View File

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

View File

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