-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
85 lines (71 loc) · 1.34 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"sync"
"github.com/acarl005/stripansi"
)
var wg sync.WaitGroup
func main() {
var oneLine, verboseMode bool
var webhookURL, lines string
flag.StringVar(&webhookURL, "u", "", "Slack Webhook URL")
flag.BoolVar(&oneLine, "1", false, "Send message line-by-line")
flag.BoolVar(&verboseMode, "v", false, "Verbose mode")
flag.Parse()
webhookEnv := os.Getenv("SLACK_WEBHOOK_URL")
if webhookEnv != "" {
webhookURL = webhookEnv
} else {
if webhookURL == "" {
if verboseMode {
fmt.Println("Slack Webhook URL not set!")
}
}
}
if !isStdin() {
os.Exit(1)
}
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
line := sc.Text()
fmt.Println(line)
if oneLine {
if webhookURL != "" {
wg.Add(1)
go slackCat(webhookURL, line)
}
} else {
lines += line
lines += "\n"
}
}
if !oneLine {
wg.Add(1)
go slackCat(webhookURL, lines)
}
wg.Wait()
}
func isStdin() bool {
f, e := os.Stdin.Stat()
if e != nil {
return false
}
if f.Mode()&os.ModeNamedPipe == 0 {
return false
}
return true
}
type data struct {
Text string `json:"text"`
}
func slackCat(url string, line string) {
data, _ := json.Marshal(data{Text: stripansi.Strip(line)})
http.Post(url, "application/json", strings.NewReader(string(data)))
wg.Done()
}