mirror of https://github.com/status-im/consul.git
Convert event command to use base.Command
This commit is contained in:
parent
e4d6687fa2
commit
8985398c7e
|
@ -58,6 +58,10 @@ func (c *Command) HTTPClient() (*api.Client, error) {
|
||||||
return api.NewClient(config)
|
return api.NewClient(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) HTTPDatacenter() string {
|
||||||
|
return c.datacenter.String()
|
||||||
|
}
|
||||||
|
|
||||||
// httpFlagsClient is the list of flags that apply to HTTP connections.
|
// httpFlagsClient is the list of flags that apply to HTTP connections.
|
||||||
func (c *Command) httpFlagsClient(f *flag.FlagSet) *flag.FlagSet {
|
func (c *Command) httpFlagsClient(f *flag.FlagSet) *flag.FlagSet {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
|
@ -198,6 +202,10 @@ func printTitle(w io.Writer, s string) {
|
||||||
func printFlag(w io.Writer, f *flag.Flag) {
|
func printFlag(w io.Writer, f *flag.Flag) {
|
||||||
example, _ := flag.UnquoteUsage(f)
|
example, _ := flag.UnquoteUsage(f)
|
||||||
if example != "" {
|
if example != "" {
|
||||||
|
// Change 'value' to 'string' for consistency
|
||||||
|
if example == "value" {
|
||||||
|
example = "string"
|
||||||
|
}
|
||||||
fmt.Fprintf(w, " -%s=<%s>\n", f.Name, example)
|
fmt.Fprintf(w, " -%s=<%s>\n", f.Name, example)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, " -%s\n", f.Name)
|
fmt.Fprintf(w, " -%s\n", f.Name)
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
consulapi "github.com/hashicorp/consul/api"
|
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
|
// EventCommand is a Command implementation that is used to
|
||||||
// fire new events
|
// fire new events
|
||||||
type EventCommand struct {
|
type EventCommand struct {
|
||||||
Ui cli.Ui
|
base.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EventCommand) Help() string {
|
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
|
a name, but a payload is optional. Events support filtering using
|
||||||
regular expressions on node name, service, and tag definitions.
|
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)
|
return strings.TrimSpace(helpText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EventCommand) Run(args []string) int {
|
func (c *EventCommand) Run(args []string) int {
|
||||||
var datacenter, name, node, service, tag, token string
|
var name, node, service, tag string
|
||||||
cmdFlags := flag.NewFlagSet("event", flag.ContinueOnError)
|
|
||||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
f := c.Command.NewFlagSet(c)
|
||||||
cmdFlags.StringVar(&datacenter, "datacenter", "", "")
|
f.StringVar(&name, "name", "",
|
||||||
cmdFlags.StringVar(&name, "name", "", "")
|
"Name of the event.")
|
||||||
cmdFlags.StringVar(&node, "node", "", "")
|
f.StringVar(&node, "node", "",
|
||||||
cmdFlags.StringVar(&service, "service", "", "")
|
"Regular expression to filter on node names.")
|
||||||
cmdFlags.StringVar(&tag, "tag", "", "")
|
f.StringVar(&service, "service", "",
|
||||||
cmdFlags.StringVar(&token, "token", "", "")
|
"Regular expression to filter on service instances")
|
||||||
httpAddr := HTTPAddrFlag(cmdFlags)
|
f.StringVar(&tag, "tag", "",
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
"Regular expression to filter on service tags. Must be used with -service.")
|
||||||
|
|
||||||
|
if err := c.Command.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +79,7 @@ func (c *EventCommand) Run(args []string) int {
|
||||||
|
|
||||||
// Check for a payload
|
// Check for a payload
|
||||||
var payload []byte
|
var payload []byte
|
||||||
args = cmdFlags.Args()
|
args = f.Args()
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
|
@ -101,7 +92,7 @@ func (c *EventCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and test the HTTP client
|
// Create and test the HTTP client
|
||||||
client, err := HTTPClient(*httpAddr)
|
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
|
||||||
|
@ -121,13 +112,9 @@ func (c *EventCommand) Run(args []string) int {
|
||||||
ServiceFilter: service,
|
ServiceFilter: service,
|
||||||
TagFilter: tag,
|
TagFilter: tag,
|
||||||
}
|
}
|
||||||
opts := &consulapi.WriteOptions{
|
|
||||||
Datacenter: datacenter,
|
|
||||||
Token: token,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fire the event
|
// Fire the event
|
||||||
id, _, err := event.Fire(params, opts)
|
id, _, err := event.Fire(params, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error firing event: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error firing event: %s", err))
|
||||||
return 1
|
return 1
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/hashicorp/consul/command/base"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEventCommand_implements(t *testing.T) {
|
func TestEventCommand_implements(t *testing.T) {
|
||||||
var _ cli.Command = &WatchCommand{}
|
var _ cli.Command = &EventCommand{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEventCommandRun(t *testing.T) {
|
func TestEventCommandRun(t *testing.T) {
|
||||||
|
@ -15,7 +16,12 @@ func TestEventCommandRun(t *testing.T) {
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
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"}
|
args := []string{"-http-addr=" + a1.httpAddr, "-name=cmd"}
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
|
|
25
commands.go
25
commands.go
|
@ -41,14 +41,20 @@ func init() {
|
||||||
|
|
||||||
"event": func() (cli.Command, error) {
|
"event": func() (cli.Command, error) {
|
||||||
return &command.EventCommand{
|
return &command.EventCommand{
|
||||||
Ui: ui,
|
Command: base.Command{
|
||||||
|
Flags: base.FlagSetHTTP,
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
"exec": func() (cli.Command, error) {
|
"exec": func() (cli.Command, error) {
|
||||||
return &command.ExecCommand{
|
return &command.ExecCommand{
|
||||||
ShutdownCh: makeShutdownCh(),
|
ShutdownCh: makeShutdownCh(),
|
||||||
Ui: ui,
|
Command: base.Command{
|
||||||
|
Flags: base.FlagSetHTTP,
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -61,6 +67,15 @@ func init() {
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"info": func() (cli.Command, error) {
|
||||||
|
return &command.InfoCommand{
|
||||||
|
Command: base.Command{
|
||||||
|
Ui: ui,
|
||||||
|
Flags: base.FlagSetClientHTTP,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
|
||||||
"kv": func() (cli.Command, error) {
|
"kv": func() (cli.Command, error) {
|
||||||
return &command.KVCommand{
|
return &command.KVCommand{
|
||||||
Ui: ui,
|
Ui: ui,
|
||||||
|
@ -156,12 +171,6 @@ func init() {
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
"info": func() (cli.Command, error) {
|
|
||||||
return &command.InfoCommand{
|
|
||||||
Ui: ui,
|
|
||||||
}, nil
|
|
||||||
},
|
|
||||||
|
|
||||||
"reload": func() (cli.Command, error) {
|
"reload": func() (cli.Command, error) {
|
||||||
return &command.ReloadCommand{
|
return &command.ReloadCommand{
|
||||||
Ui: ui,
|
Ui: ui,
|
||||||
|
|
|
@ -39,13 +39,12 @@ Usage: `consul event [options] [payload]`
|
||||||
The only required option is `-name` which specifies the event name. An optional
|
The only required option is `-name` which specifies the event name. An optional
|
||||||
payload can be provided as the final argument.
|
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
|
<%= partial "docs/commands/http_api_options_client" %>
|
||||||
to send this command. If this isn't specified, the command will contact
|
<%= partial "docs/commands/http_api_options_server" %>
|
||||||
`127.0.0.1:8500` which is the default HTTP address of a Consul agent.
|
|
||||||
|
|
||||||
* `-datacenter` - Datacenter to query. Defaults to that of agent.
|
#### Command Options
|
||||||
|
|
||||||
* `-name` - The name of the event.
|
* `-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
|
a matching tag. This must be used with `-service`. As an example, you may
|
||||||
do `-service mysql -tag secondary`.
|
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.
|
|
Loading…
Reference in New Issue