-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (114 loc) · 3.63 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
package main
import (
"errors"
"fmt"
"log"
"os"
"github.com/mentimeter/morty/mortems"
"github.com/sethvargo/go-githubactions"
)
var ErrMissingInput = errors.New("missing required input")
func main() {
if len(os.Args) <= 1 {
exitHelp()
}
if os.Args[1] == "check" {
if err := runCheck(); err != nil {
log.Fatalf("Oh jeez: %s\n", err)
}
} else if os.Args[1] == "local" {
if err := runLocal(); err != nil {
log.Fatalf("Oh jeez: %s\n", err)
}
} else if len(os.Args) == 3 && os.Args[1] == "git" && os.Args[2] == "check" {
if err := runGitCheck(); err != nil {
log.Fatalf("Oh jeez: %s\n", err)
}
} else if len(os.Args) == 2 && os.Args[1] == "git" {
if err := runGitCollect(); err != nil {
log.Fatalf("Oh jeez: %s\n", err)
}
} else {
exitHelp()
}
fmt.Println("Post-mortems successfully parsed and organized")
}
func exitHelp() {
fmt.Println("What do you want me to do? Options are: ")
fmt.Println(" `check` - for local parsing")
fmt.Println(" `git` - to parse and commit stats")
fmt.Println(" `git check` - to check parsing in your github action")
log.Fatalln("Please give me an argument!")
}
func runGitCollect() error {
token := githubactions.GetInput("token")
if token == "" {
fmt.Println("Want to check that your post-mortem is correctly formatted? Use `./morty check`")
return fmt.Errorf("no github action token supplied: %w", ErrMissingInput)
}
repository := os.Getenv("GITHUB_REPOSITORY")
if repository == "" {
return fmt.Errorf("no repository name set as env variable: %w", ErrMissingInput)
}
ref := os.Getenv("GITHUB_REF")
if ref == "" {
return fmt.Errorf("no ref: %w", ErrMissingInput)
}
gitService := mortems.NewGitHubService(token, repository, ref)
var mortemCollector mortems.MortemCollector
_, ddApiExists := os.LookupEnv("DD_API_KEY")
_, ddAppExists := os.LookupEnv("DD_APP_KEY")
if ddApiExists && ddAppExists {
ddService := mortems.NewDatadogReporter()
mortemCollector = mortems.NewMortemReportingCollector(gitService, ddService)
} else {
mortemCollector = mortems.NewMortemCollector(gitService)
}
return mortemCollector.Collect()
}
func runGitCheck() error {
token := githubactions.GetInput("token")
if token == "" {
fmt.Println("Want to check that your post-mortem is correctly formatted? Use `./morty check`")
return fmt.Errorf("no github action token supplied: %w", ErrMissingInput)
}
repository := os.Getenv("GITHUB_REPOSITORY")
if repository == "" {
return fmt.Errorf("no repository name set as env variable: %w", ErrMissingInput)
}
ref := os.Getenv("GITHUB_REF")
if ref == "" {
return fmt.Errorf("no ref: %w", ErrMissingInput)
}
gitService := mortems.NewGitHubService(token, repository, ref)
mortemCollector := mortems.NewMortemCollector(gitService)
_, err := mortemCollector.Check()
return err
}
func runLocal() error {
repoPath, err := os.Getwd()
if err != nil {
return fmt.Errorf("could not get wd: %w", err)
}
fileService := mortems.NewLocalFileService(repoPath)
var mortemCollector mortems.MortemCollector
_, ddApiExists := os.LookupEnv("DD_API_KEY")
_, ddAppExists := os.LookupEnv("DD_APP_KEY")
if ddApiExists && ddAppExists {
ddService := mortems.NewDatadogReporter()
mortemCollector = mortems.NewMortemReportingCollector(fileService, ddService)
} else {
mortemCollector = mortems.NewMortemCollector(fileService)
}
return mortemCollector.Collect()
}
func runCheck() error {
repoPath, err := os.Getwd()
if err != nil {
return fmt.Errorf("could not get wd: %w", err)
}
fileService := mortems.NewLocalFileService(repoPath)
mortemCollector := mortems.NewMortemCollector(fileService)
_, err = mortemCollector.Check()
return err
}