-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (47 loc) · 1000 Bytes
/
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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type content struct {
Text string `json:"text"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
url := os.Getenv("SLACK_WEBHOOK_URL")
if url == "" {
log.Printf("SLACK_WEBHOOK_URL is missing")
os.Exit(1)
}
data, err := io.ReadAll(os.Stdin)
if err != nil {
log.Printf("unknown error: %s", err.Error())
os.Exit(1)
}
body, err := json.Marshal(content{
Text: fmt.Sprintf("```%s```", string(data)),
})
if err != nil {
log.Printf("unknown error: %s", err.Error())
os.Exit(1)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
log.Printf("unknown error: %s", err.Error())
os.Exit(1)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("unknown error: %s", err.Error())
os.Exit(1)
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, req.Body)
}