forked from xornivore/incidentist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (99 loc) · 3.05 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
package main
import (
"fmt"
"os"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/xornivore/incidentist/report"
)
var (
authToken = kingpin.Flag("auth", "Auth token").String()
teams = kingpin.Flag("team", "Team names").Required().Strings()
pdTeams = kingpin.Flag("pd-team", "Team names in PagerDuty if different from Team").Strings()
since = kingpin.Flag("since", "Since date/time").Required().String()
until = kingpin.Flag("until", "Until date/time").Required().String()
urgency = kingpin.Flag("urgency", "Urgency").Default("high").String()
replace = kingpin.Flag("replace", "Replace titles with regex").Strings()
tagFilters = kingpin.Flag("tags", "Filter PagerDuty incidents by Datadog tags").Strings()
// Params for uploading the report
subdomain = kingpin.Flag("confluence-subdomain", "Confluence subdomain").String()
spaceKey = kingpin.Flag("confluence-space", "Confluence space key").String()
parentId = kingpin.Flag("confluence-parent", "Confluence parent page id").String()
)
func errorf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", a...)
}
func exit(format string, a ...interface{}) {
errorf(format, a...)
os.Exit(-1)
}
func main() {
kingpin.Parse()
for i, team := range *teams {
(*teams)[i] = strings.ToLower(team)
}
if *authToken == "" {
*authToken = os.Getenv("PD_AUTH_TOKEN")
}
if *authToken == "" {
exit("missing auth token (--auth or PD_AUTH_TOKEN)")
}
ddApiKey := os.Getenv("DD_API_KEY")
if ddApiKey == "" {
exit("missing datadog api key (DD_API_KEY)")
}
ddAppKey := os.Getenv("DD_APP_KEY")
if ddAppKey == "" {
exit("missing datadog app key (DD_APP_KEY)")
}
var confUsername, confToken string
doUpload := *subdomain != ""
if doUpload {
// Only check these credentials if we want to upload to confluence
confUsername = os.Getenv("CONFLUENCE_USERNAME")
if confUsername == "" {
exit("missing confluence username (CONFLUENCE_USERNAME)")
}
confToken = os.Getenv("CONFLUENCE_API_TOKEN")
if confToken == "" {
exit("missing confluence auth token (CONFLUENCE_API_TOKEN)")
}
if *spaceKey == "" {
exit("missing space key (--confluence-space)")
}
}
generateRequest := report.GenerateRequest{
Teams: *teams,
PdTeams: *pdTeams,
Since: *since,
Until: *until,
TagFilters: *tagFilters,
AuthToken: *authToken,
Urgency: *urgency,
Replace: *replace,
DdApiKey: ddApiKey,
DdAppKey: ddAppKey,
}
content, err := report.Generate(generateRequest)
if err != nil {
exit("error generating report: %v", err)
} else if doUpload {
uploadRequest := report.UploadRequest{
ConfluenceSubdomain: *subdomain,
ConfluenceUsername: confUsername,
ConfluenceToken: confToken,
SpaceKey: *spaceKey,
ParentId: *parentId,
MarkdownContent: content,
}
err = report.Upload(uploadRequest)
if err != nil {
exit("error uploading report: %v", err)
} else {
fmt.Println("Report uploaded successfully")
}
} else {
// If not uploading, just dump to stdout.
fmt.Println(content)
}
}