This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_events.go
90 lines (69 loc) · 1.86 KB
/
cmd_events.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
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/exograd/go-program"
)
func addEventCommands() {
var c *program.Command
// create-event
c = p.AddCommand("create-event", "create a new custom event",
cmdCreateEvent)
c.AddOption("t", "event-time", "timestamp", "",
"the date and time the event occurred (RFC 3339 format)")
c.AddArgument("connector", "the name of the connector")
c.AddArgument("event", "the name of the event")
c.AddArgument("data",
"the JSON object representing event data (\"-\" to read stdin)")
// replay-event
c = p.AddCommand("replay-event", "replay an existing event",
cmdReplayEvent)
c.AddArgument("event-id", "the identifier of the event")
}
func cmdCreateEvent(p *program.Program) {
app.IdentifyCurrentProject()
var eventTime time.Time
if p.IsOptionSet("event-time") {
eventTimeString := p.OptionValue("event-time")
var err error
eventTime, err = time.Parse(time.RFC3339, eventTimeString)
if err != nil {
p.Fatal("invalid event time: %v", err)
}
} else {
eventTime = time.Now().UTC()
}
connector := p.ArgumentValue("connector")
name := p.ArgumentValue("event")
data := []byte(p.ArgumentValue("data"))
if len(data) == 1 && data[0] == '-' {
var err error
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
p.Fatal("cannot read stdin: %v", err)
}
}
newEvent := NewEvent{
EventTime: &eventTime,
Connector: connector,
Name: name,
Data: data,
}
_, err := app.Client.CreateEvent(&newEvent)
if err != nil {
p.Fatal("cannot create event: %v", err)
}
p.Info("events created")
}
func cmdReplayEvent(p *program.Program) {
app.IdentifyCurrentProject()
EventId := p.ArgumentValue("event-id")
event, err := app.Client.ReplayEvent(EventId)
if err != nil {
p.Fatal("cannot replay event: %v", err)
}
p.Info("event %s created", event.Id)
fmt.Printf("%s\n", event.Id)
}