-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
188 lines (159 loc) · 4.13 KB
/
run.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"github.com/BurntSushi/toml"
)
type Slack struct {
Hook string `toml:"hook"`
}
type Config struct {
Name *string
Level *string
Dir string
Prog string
Args []string
Slack Slack
}
type Level int
const (
Always Level = iota
Error_Or_Output
Error
)
const CONFIG_ENV_VAR = "RSLACKER_CONFIG"
func main() {
configPtr := flag.String("config", "./config.toml", "config file")
dryRunPtr := flag.Bool("dry-run", false, "dry-run")
flag.Parse()
config := getConfigOrPanic(*configPtr)
level := getLevelOrPanic(config)
if *dryRunPtr {
fmt.Printf("running with config: %#v", config)
return
}
cmd := exec.Command(config.Prog, config.Args...)
cmd.Dir = config.Dir
out, err := cmd.CombinedOutput()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
outMessage := fmt.Sprintf("exitError %#v\n-------\n%s", exitError.Error(), string(out))
exitCode := exitError.ExitCode()
slack(&config, runFailedMessage(&config, &exitCode, outMessage))
} else {
outMessage := fmt.Sprintf("error %#v\n-------\n%s", err.Error(), string(out))
slack(&config, runFailedMessage(&config, nil, outMessage))
}
return
}
if level == Error {
return
}
output := string(out)
if strings.TrimSpace(output) == "" && level == Error_Or_Output {
return
}
slack(&config, runOkMessage(&config, output))
}
func getLevelOrPanic(config Config) Level {
if config.Level == nil || *config.Level == "Always" {
return Always
} else if *config.Level == "Error_Or_Output" {
return Error_Or_Output
} else if *config.Level == "Error" {
return Error
}
panic(fmt.Sprintf("Unknown Level configured: %#v", config.Level))
}
func getConfigOrPanic(file string) Config {
config := Config{}
confFromEnv := os.Getenv(CONFIG_ENV_VAR)
if confFromEnv == "" {
if _, err := os.Stat(file); os.IsNotExist(err) {
panic("No config file given and RSLACKER_CONFIG not set")
}
_, err := toml.DecodeFile(file, &config)
if err != nil {
panic(fmt.Sprintf("Error reading config file: %#v", err))
}
} else {
_, err := toml.Decode(confFromEnv, &config)
if err != nil {
panic(fmt.Sprintf("Error reading config from environment: %#v", err))
}
}
return config
}
func runFailedMessage(conf *Config, exitCode *int, output string) *SlackBlock {
name := getProgName(conf)
var exitCodeStr string
if exitCode != nil {
exitCodeStr = fmt.Sprintf("%d", *exitCode)
} else {
exitCodeStr = "unknown"
}
text := ""
if strings.TrimSpace(output) != "" {
text = fmt.Sprintf("\n```%s```", output)
}
return section(markdownText(fmt.Sprintf(":red_circle: %s failed (exit code %s)%s", name, exitCodeStr, text)))
}
func runOkMessage(conf *Config, output string) *SlackBlock {
name := getProgName(conf)
text := ""
if strings.TrimSpace(output) != "" {
text = fmt.Sprintf("\n```%s```", output)
}
return section(markdownText(fmt.Sprintf(":large_blue_circle: %s ok%s", name, text)))
}
func getProgName(conf *Config) string {
name := fmt.Sprintf("`%s`", conf.Prog)
if conf.Name != nil {
name = *conf.Name
}
return name
}
func slack(conf *Config, msg *SlackBlock) {
payloadStr, err := json.Marshal(SlackMessage{Blocks: []*SlackBlock{msg}})
if err != nil {
panic(fmt.Sprintf("Could not create slack message: %#v", err))
}
resp, err := http.Post(conf.Slack.Hook, "application/json", strings.NewReader(string(payloadStr)))
if err != nil {
panic(fmt.Sprintf("Unexpected error from slack: %#v", err))
}
if resp.StatusCode != 200 {
bodyBytes, err := ioutil.ReadAll(resp.Body)
body := string(bodyBytes)
if err != nil {
body = fmt.Sprintf("err reading body from response: %#v", resp)
}
panic(fmt.Sprintf("Got unexpected response from slack: %s", body))
}
}
type SlackMessage struct {
Blocks []*SlackBlock `json:"blocks"`
}
type SlackBlock struct {
Type string `json:"type"`
Text *Text `json:"text"`
}
func section(text *Text) *SlackBlock {
return &SlackBlock{
Type: "section",
Text: text,
}
}
type Text struct {
Type string `json:"type"`
Text string `json:"text"`
}
func markdownText(msg string) *Text {
return &Text{Type: "mrkdwn", Text: msg}
}