forked from hashicorp/serf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
82 lines (68 loc) · 2 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package command
import (
"flag"
"fmt"
"github.com/mitchellh/cli"
"strings"
)
// EventCommand is a Command implementation that queries a running
// Serf agent what members are part of the cluster currently.
type EventCommand struct {
Ui cli.Ui
}
func (c *EventCommand) Help() string {
helpText := `
Usage: serf event [options] name payload
Dispatches a custom event across the Serf cluster.
Options:
-coalesce=true/false Whether this event can be coalesced. This means
that repeated events of the same name within a
short period of time are ignored, except the last
one received. Default is true.
-rpc-addr=127.0.0.1:7373 RPC address of the Serf agent.
`
return strings.TrimSpace(helpText)
}
func (c *EventCommand) Run(args []string) int {
var coalesce bool
cmdFlags := flag.NewFlagSet("event", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
cmdFlags.BoolVar(&coalesce, "coalesce", true, "coalesce")
rpcAddr := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) < 1 {
c.Ui.Error("An event name must be specified.")
c.Ui.Error("")
c.Ui.Error(c.Help())
return 1
} else if len(args) > 2 {
c.Ui.Error("Too many command line arguments. Only a name and payload must be specified.")
c.Ui.Error("")
c.Ui.Error(c.Help())
return 1
}
event := args[0]
var payload []byte
if len(args) == 2 {
payload = []byte(args[1])
}
client, err := RPCClient(*rpcAddr)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Serf agent: %s", err))
return 1
}
defer client.Close()
if err := client.UserEvent(event, payload, coalesce); err != nil {
c.Ui.Error(fmt.Sprintf("Error sending event: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf("Event '%s' dispatched! Coalescing enabled: %#v",
event, coalesce))
return 0
}
func (c *EventCommand) Synopsis() string {
return "Send a custom event through the Serf cluster"
}