Convert event command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-08 16:56:58 -05:00
parent 405aa17d0e
commit 6cc2299123
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
5 changed files with 56 additions and 49 deletions

View File

@ -58,6 +58,10 @@ func (c *Command) HTTPClient() (*api.Client, error) {
return api.NewClient(config)
}
func (c *Command) HTTPDatacenter() string {
return c.datacenter.String()
}
// httpFlagsClient is the list of flags that apply to HTTP connections.
func (c *Command) httpFlagsClient(f *flag.FlagSet) *flag.FlagSet {
if f == nil {
@ -198,6 +202,10 @@ func printTitle(w io.Writer, s string) {
func printFlag(w io.Writer, f *flag.Flag) {
example, _ := flag.UnquoteUsage(f)
if example != "" {
// Change 'value' to 'string' for consistency
if example == "value" {
example = "string"
}
fmt.Fprintf(w, " -%s=<%s>\n", f.Name, example)
} else {
fmt.Fprintf(w, " -%s\n", f.Name)

View File

@ -1,19 +1,18 @@
package command
import (
"flag"
"fmt"
"regexp"
"strings"
consulapi "github.com/hashicorp/consul/api"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/base"
)
// EventCommand is a Command implementation that is used to
// fire new events
type EventCommand struct {
Ui cli.Ui
base.Command
}
func (c *EventCommand) Help() string {
@ -24,33 +23,25 @@ Usage: consul event [options] [payload]
a name, but a payload is optional. Events support filtering using
regular expressions on node name, service, and tag definitions.
Options:
` + c.Command.Help()
-http-addr=127.0.0.1:8500 HTTP address of the Consul agent.
-datacenter="" Datacenter to dispatch in. Defaults to that of agent.
-name="" Name of the event.
-node="" Regular expression to filter on node names
-service="" Regular expression to filter on service instances
-tag="" Regular expression to filter on service tags. Must be used
with -service.
-token="" ACL token to use during requests. Defaults to that
of the agent.
`
return strings.TrimSpace(helpText)
}
func (c *EventCommand) Run(args []string) int {
var datacenter, name, node, service, tag, token string
cmdFlags := flag.NewFlagSet("event", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
cmdFlags.StringVar(&datacenter, "datacenter", "", "")
cmdFlags.StringVar(&name, "name", "", "")
cmdFlags.StringVar(&node, "node", "", "")
cmdFlags.StringVar(&service, "service", "", "")
cmdFlags.StringVar(&tag, "tag", "", "")
cmdFlags.StringVar(&token, "token", "", "")
httpAddr := HTTPAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
var name, node, service, tag string
f := c.Command.NewFlagSet(c)
f.StringVar(&name, "name", "",
"Name of the event.")
f.StringVar(&node, "node", "",
"Regular expression to filter on node names.")
f.StringVar(&service, "service", "",
"Regular expression to filter on service instances")
f.StringVar(&tag, "tag", "",
"Regular expression to filter on service tags. Must be used with -service.")
if err := c.Command.Parse(args); err != nil {
return 1
}
@ -88,7 +79,7 @@ func (c *EventCommand) Run(args []string) int {
// Check for a payload
var payload []byte
args = cmdFlags.Args()
args = f.Args()
switch len(args) {
case 0:
case 1:
@ -101,7 +92,7 @@ func (c *EventCommand) Run(args []string) int {
}
// Create and test the HTTP client
client, err := HTTPClient(*httpAddr)
client, err := c.Command.HTTPClient()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
@ -121,13 +112,9 @@ func (c *EventCommand) Run(args []string) int {
ServiceFilter: service,
TagFilter: tag,
}
opts := &consulapi.WriteOptions{
Datacenter: datacenter,
Token: token,
}
// Fire the event
id, _, err := event.Fire(params, opts)
id, _, err := event.Fire(params, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error firing event: %s", err))
return 1

View File

@ -1,13 +1,14 @@
package command
import (
"github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli"
"strings"
"testing"
)
func TestEventCommand_implements(t *testing.T) {
var _ cli.Command = &WatchCommand{}
var _ cli.Command = &EventCommand{}
}
func TestEventCommandRun(t *testing.T) {
@ -15,7 +16,12 @@ func TestEventCommandRun(t *testing.T) {
defer a1.Shutdown()
ui := new(cli.MockUi)
c := &EventCommand{Ui: ui}
c := &EventCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetClientHTTP,
},
}
args := []string{"-http-addr=" + a1.httpAddr, "-name=cmd"}
code := c.Run(args)

View File

@ -41,14 +41,20 @@ func init() {
"event": func() (cli.Command, error) {
return &command.EventCommand{
Ui: ui,
Command: base.Command{
Flags: base.FlagSetHTTP,
Ui: ui,
},
}, nil
},
"exec": func() (cli.Command, error) {
return &command.ExecCommand{
ShutdownCh: makeShutdownCh(),
Ui: ui,
Command: base.Command{
Flags: base.FlagSetHTTP,
Ui: ui,
},
}, nil
},
@ -61,6 +67,15 @@ func init() {
}, nil
},
"info": func() (cli.Command, error) {
return &command.InfoCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetClientHTTP,
},
}, nil
},
"kv": func() (cli.Command, error) {
return &command.KVCommand{
Ui: ui,
@ -156,12 +171,6 @@ func init() {
}, nil
},
"info": func() (cli.Command, error) {
return &command.InfoCommand{
Ui: ui,
}, nil
},
"reload": func() (cli.Command, error) {
return &command.ReloadCommand{
Ui: ui,

View File

@ -39,13 +39,12 @@ Usage: `consul event [options] [payload]`
The only required option is `-name` which specifies the event name. An optional
payload can be provided as the final argument.
The list of available flags are:
#### API Options
* `-http-addr` - Address to the HTTP server of the agent you want to contact
to send this command. If this isn't specified, the command will contact
`127.0.0.1:8500` which is the default HTTP address of a Consul agent.
<%= partial "docs/commands/http_api_options_client" %>
<%= partial "docs/commands/http_api_options_server" %>
* `-datacenter` - Datacenter to query. Defaults to that of agent.
#### Command Options
* `-name` - The name of the event.
@ -57,5 +56,3 @@ The list of available flags are:
a matching tag. This must be used with `-service`. As an example, you may
do `-service mysql -tag secondary`.
* `-token` - The ACL token to use when firing the event. This token must have
write-level privileges for the event specified. Defaults to that of the agent.