mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 22:06:20 +00:00
commands: move event command to separate pkg
This commit is contained in:
parent
d870c6289b
commit
85bfd8f339
@ -8,6 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/command/event"
|
||||||
"github.com/hashicorp/consul/command/join"
|
"github.com/hashicorp/consul/command/join"
|
||||||
"github.com/hashicorp/consul/command/validate"
|
"github.com/hashicorp/consul/command/validate"
|
||||||
"github.com/hashicorp/consul/version"
|
"github.com/hashicorp/consul/version"
|
||||||
@ -72,12 +73,7 @@ func init() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
"event": func() (cli.Command, error) {
|
"event": func() (cli.Command, error) {
|
||||||
return &EventCommand{
|
return event.New(ui), nil
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetHTTP,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"exec": func() (cli.Command, error) {
|
"exec": func() (cli.Command, error) {
|
||||||
|
@ -1,39 +1,48 @@
|
|||||||
package command
|
package event
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
consulapi "github.com/hashicorp/consul/api"
|
"github.com/hashicorp/consul/api"
|
||||||
|
"github.com/hashicorp/consul/command/flags"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EventCommand is a Command implementation that is used to
|
func New(ui cli.Ui) *cmd {
|
||||||
// fire new events
|
c := &cmd{UI: ui}
|
||||||
type EventCommand struct {
|
c.initFlags()
|
||||||
BaseCommand
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
// flags
|
type cmd struct {
|
||||||
|
UI cli.Ui
|
||||||
|
flags *flag.FlagSet
|
||||||
|
http *flags.HTTPFlags
|
||||||
name string
|
name string
|
||||||
node string
|
node string
|
||||||
service string
|
service string
|
||||||
tag string
|
tag string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EventCommand) initFlags() {
|
func (c *cmd) initFlags() {
|
||||||
c.InitFlagSet()
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
c.FlagSet.StringVar(&c.name, "name", "",
|
c.flags.StringVar(&c.name, "name", "",
|
||||||
"Name of the event.")
|
"Name of the event.")
|
||||||
c.FlagSet.StringVar(&c.node, "node", "",
|
c.flags.StringVar(&c.node, "node", "",
|
||||||
"Regular expression to filter on node names.")
|
"Regular expression to filter on node names.")
|
||||||
c.FlagSet.StringVar(&c.service, "service", "",
|
c.flags.StringVar(&c.service, "service", "",
|
||||||
"Regular expression to filter on service instances.")
|
"Regular expression to filter on service instances.")
|
||||||
c.FlagSet.StringVar(&c.tag, "tag", "",
|
c.flags.StringVar(&c.tag, "tag", "",
|
||||||
"Regular expression to filter on service tags. Must be used with -service.")
|
"Regular expression to filter on service tags. Must be used with -service.")
|
||||||
|
|
||||||
|
c.http = &flags.HTTPFlags{}
|
||||||
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EventCommand) Run(args []string) int {
|
func (c *cmd) Run(args []string) int {
|
||||||
c.initFlags()
|
if err := c.flags.Parse(args); err != nil {
|
||||||
if err := c.FlagSet.Parse(args); err != nil {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +80,7 @@ func (c *EventCommand) Run(args []string) int {
|
|||||||
|
|
||||||
// Check for a payload
|
// Check for a payload
|
||||||
var payload []byte
|
var payload []byte
|
||||||
args = c.FlagSet.Args()
|
args = c.flags.Args()
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
@ -84,7 +93,7 @@ func (c *EventCommand) Run(args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create and test the HTTP client
|
// Create and test the HTTP client
|
||||||
client, err := c.HTTPClient()
|
client, err := c.http.APIClient()
|
||||||
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
|
||||||
@ -97,7 +106,7 @@ func (c *EventCommand) Run(args []string) int {
|
|||||||
|
|
||||||
// Prepare the request
|
// Prepare the request
|
||||||
event := client.Event()
|
event := client.Event()
|
||||||
params := &consulapi.UserEvent{
|
params := &api.UserEvent{
|
||||||
Name: c.name,
|
Name: c.name,
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
NodeFilter: c.node,
|
NodeFilter: c.node,
|
||||||
@ -117,18 +126,16 @@ func (c *EventCommand) Run(args []string) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EventCommand) Help() string {
|
func (c *cmd) Synopsis() string {
|
||||||
c.initFlags()
|
return "Fire a new event"
|
||||||
return c.HelpCommand(`
|
}
|
||||||
Usage: consul event [options] [payload]
|
|
||||||
|
func (c *cmd) Help() string {
|
||||||
|
s := `Usage: consul event [options] [payload]
|
||||||
|
|
||||||
Dispatches a custom user event across a datacenter. An event must provide
|
Dispatches a custom user event across a datacenter. An event must provide
|
||||||
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.`
|
||||||
|
|
||||||
`)
|
return flags.Usage(s, c.flags, c.http.ClientFlags(), nil)
|
||||||
}
|
|
||||||
|
|
||||||
func (c *EventCommand) Synopsis() string {
|
|
||||||
return "Fire a new event"
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package command
|
package event
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@ -8,9 +8,10 @@ import (
|
|||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEventCommand_implements(t *testing.T) {
|
func TestEventCommand_noTabs(t *testing.T) {
|
||||||
t.Parallel()
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
||||||
var _ cli.Command = &EventCommand{}
|
t.Fatal("usage has tabs")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEventCommandRun(t *testing.T) {
|
func TestEventCommandRun(t *testing.T) {
|
||||||
@ -19,15 +20,10 @@ func TestEventCommandRun(t *testing.T) {
|
|||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := cli.NewMockUi()
|
ui := cli.NewMockUi()
|
||||||
c := &EventCommand{
|
cmd := New(ui)
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
UI: ui,
|
|
||||||
Flags: FlagSetClientHTTP,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
args := []string{"-http-addr=" + a1.HTTPAddr(), "-name=cmd"}
|
args := []string{"-http-addr=" + a1.HTTPAddr(), "-name=cmd"}
|
||||||
|
|
||||||
code := c.Run(args)
|
code := cmd.Run(args)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user