-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
152 lines (124 loc) · 2.72 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"flag"
"log"
"os"
"github.com/nekottyo/alfred-datadog-workflow/pkg/dd"
"gopkg.in/zorkian/go-datadog-api.v2"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/keychain"
"go.deanishe.net/fuzzy"
)
const (
apiKeyName = "apikey"
appKeyName = "appkey"
)
var (
maxResults = 200
// Command-line flags
flagAPIKey bool
flagAPPKey bool
flagDashboard bool
flagMonitor bool
flagService bool
// credentials
apiKey string
appKey string
arg string
// Workflow
sopts []fuzzy.Option
wf *aw.Workflow
kc *keychain.Keychain
client *datadog.Client
)
func init() {
flag.BoolVar(&flagAPIKey, "apikey", false, "register apikey")
flag.BoolVar(&flagAPPKey, "appkey", false, "register appkey")
flag.BoolVar(&flagDashboard, "dashboard", false, "list dashboard")
flag.BoolVar(&flagMonitor, "monitor", false, "list monitor")
flag.BoolVar(&flagService, "service", false, "list service")
initWorkflow()
}
func initWorkflow() {
sopts = []fuzzy.Option{
fuzzy.AdjacencyBonus(10.0),
fuzzy.LeadingLetterPenalty(-0.1),
fuzzy.MaxLeadingLetterPenalty(-3.0),
fuzzy.UnmatchedLetterPenalty(-0.5),
}
wf = aw.New(aw.HelpURL("https://github.com/nekottyo/alfred-datadog-workflow"),
aw.MaxResults(maxResults),
aw.SortOptions(sopts...))
kc = keychain.New("net.nekottyo.alfred-datadog.workflow")
}
func getSecrets(kc *keychain.Keychain) (string, string, error) {
var apiKey, appKey string
var err error
apiKey, err = kc.Get(apiKeyName)
if err != nil {
return apiKey, appKey, err
}
appKey, err = kc.Get(appKeyName)
if err != nil {
return apiKey, appKey, err
}
return apiKey, appKey, nil
}
func setupDatadogClient() {
var err error
apiKey, appKey, err = getSecrets(kc)
if err != nil {
wf.FatalError(err)
}
client = datadog.NewClient(apiKey, appKey)
}
func run() {
wf.Args()
flag.Parse()
arg = flag.Arg(0)
if flagAPIKey {
if err := kc.Set(apiKeyName, arg); err != nil {
wf.FatalError(err)
}
wf.SendFeedback()
os.Exit(0)
}
if flagAPPKey {
if err := kc.Set(appKeyName, arg); err != nil {
wf.FatalError(err)
}
wf.SendFeedback()
os.Exit(0)
}
setupDatadogClient()
if flagDashboard {
d := dd.NewBoard(client, wf)
if err := d.ListBoards(); err != nil {
wf.FatalError(err)
}
}
if flagMonitor {
d := dd.NewMonitor(client, wf)
if err := d.ListMonitors(); err != nil {
wf.FatalError(err)
}
}
if flagService {
d, err := dd.NewServices("config/service.yaml", wf)
if err != nil {
wf.FatalError(err)
}
if err := d.ListServices(); err != nil {
wf.FatalError(err)
}
}
if arg != "" {
wf.Filter(arg)
}
log.Printf("[main] query=%s", arg)
wf.WarnEmpty("No matching", "Try a different query")
wf.SendFeedback()
}
func main() {
wf.Run(run)
}