commands: move reload command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-17 08:47:09 +02:00 committed by Frank Schröder
parent eb0640efd0
commit 1ba816b0ae
4 changed files with 71 additions and 62 deletions

View File

@ -34,6 +34,7 @@ import (
"github.com/hashicorp/consul/command/operraft"
"github.com/hashicorp/consul/command/operraftlist"
"github.com/hashicorp/consul/command/operraftremove"
"github.com/hashicorp/consul/command/reload"
"github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version"
"github.com/mitchellh/cli"
@ -177,12 +178,7 @@ func init() {
},
"reload": func() (cli.Command, error) {
return &ReloadCommand{
BaseCommand: BaseCommand{
Flags: FlagSetClientHTTP,
UI: ui,
},
}, nil
return reload.New(ui), nil
},
"rtt": func() (cli.Command, error) {

View File

@ -1,47 +0,0 @@
package command
import (
"fmt"
)
// ReloadCommand is a Command implementation that instructs
// the Consul agent to reload configurations
type ReloadCommand struct {
BaseCommand
}
func (c *ReloadCommand) Help() string {
c.InitFlagSet()
return c.HelpCommand(`
Usage: consul reload
Causes the agent to reload configurations. This can be used instead
of sending the SIGHUP signal to the agent.
`)
}
func (c *ReloadCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
return 1
}
client, err := c.HTTPClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
if err := client.Agent().Reload(); err != nil {
c.UI.Error(fmt.Sprintf("Error reloading: %s", err))
return 1
}
c.UI.Output("Configuration reload triggered")
return 0
}
func (c *ReloadCommand) Synopsis() string {
return "Triggers the agent to reload configuration files"
}

63
command/reload/reload.go Normal file
View File

@ -0,0 +1,63 @@
package reload
import (
"flag"
"fmt"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
)
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.init()
return c
}
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 *cmd) Synopsis() string {
return "Triggers the agent to reload configuration files"
}
func (c *cmd) Help() string {
return c.usage
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
if err := client.Agent().Reload(); err != nil {
c.UI.Error(fmt.Sprintf("Error reloading: %s", err))
return 1
}
c.UI.Output("Configuration reload triggered")
return 0
}
const usage = `Usage: consul reload
Causes the agent to reload configurations. This can be used instead
of sending the SIGHUP signal to the agent.`

View File

@ -1,4 +1,4 @@
package command
package reload
import (
"strings"
@ -8,9 +8,11 @@ import (
"github.com/mitchellh/cli"
)
func TestReloadCommand_implements(t *testing.T) {
func TestReloadCommand_noTabs(t *testing.T) {
t.Parallel()
var _ cli.Command = &ReloadCommand{}
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
t.Fatal("usage has tabs")
}
}
func TestReloadCommandRun(t *testing.T) {
@ -25,12 +27,7 @@ func TestReloadCommandRun(t *testing.T) {
}()
ui := cli.NewMockUi()
c := &ReloadCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetClientHTTP,
},
}
c := New(ui)
args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args)