This repository was archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
110 lines (96 loc) · 2.3 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"os"
"time"
"github.com/gizak/termui"
"github.com/tscolari/plag/output"
"github.com/tscolari/plag/parser"
"github.com/urfave/cli"
datadog "github.com/zorkian/go-datadog-api"
)
func main() {
app := cli.NewApp()
app.Name = "plag"
app.Usage = "--message garden.streamIn [OPTIONS]"
app.Description = "Plot lager time series"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "message",
Usage: "message name to look for",
},
cli.StringFlag{
Name: "csv",
Usage: "export csv file",
},
cli.StringFlag{
Name: "datadog-api-key",
Usage: "datadog api key",
},
cli.StringFlag{
Name: "datadog-app-key",
Usage: "datadog app key",
},
cli.StringFlag{
Name: "datadog-metric-name",
Usage: "metric name to post to datadog",
},
cli.BoolFlag{
Name: "graph",
Usage: "plot realtime graph on terminal",
},
cli.DurationFlag{
Name: "graph-decay",
Usage: "duration for graph EWMA function",
Value: 5 * time.Second,
},
}
app.Action = func(c *cli.Context) error {
if !c.IsSet("message") {
return cli.NewExitError("missing `--message` argument. Don't know what to look for.", 1)
}
parser := parser.New()
data := parser.Parse(os.Stdin, c.String("message"))
if c.IsSet("graph") {
if err := termui.Init(); err != nil {
panic(err)
}
defer termui.Close()
termui.Handle("/sys/kbd/q", func(termui.Event) {
close(data)
termui.StopLoop()
})
}
outputer := output.NewMulti()
addOutputers(outputer, c)
if c.IsSet("graph") {
go outputer.Write(data)
termui.Loop()
} else {
_ = outputer.Write(data)
}
return nil
}
_ = app.Run(os.Args)
}
func addOutputers(multi *output.Multi, c *cli.Context) {
if c.IsSet("csv") {
csv, _ := output.NewCsv(c.String("csv"))
multi.Add(csv)
}
if c.IsSet("datadog-api-key") {
if !c.IsSet("datadog-app-key") {
panic("Missing `--datadog-app-key` flag")
}
client := datadog.NewClient(c.String("datadog-api-key"), c.String("datadog-app-key"))
metricName := c.String("message")
if c.IsSet("datadog-metric-name") {
metricName = c.String("datadog-metric-name")
}
datadog := output.NewDatadog(client, metricName)
multi.Add(datadog)
}
if c.IsSet("graph") {
graph := output.NewGraph(c.String("message"), c.Duration("graph-decay"))
multi.Add(graph)
}
}