forked from dwisiswant0/slackcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (86 loc) · 1.68 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
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", "", "Feishu Webhook URL")
flag.BoolVar(&oneLine, "1", false, "Send message line-by-line")
flag.BoolVar(&verboseMode, "v", false, "Verbose mode")
flag.Parse()
webhookEnv := os.Getenv("FEISHU_WEBHOOK_URL")
if webhookEnv != "" {
webhookURL = webhookEnv
} else {
if webhookURL == "" {
if verboseMode {
fmt.Println("Feishu 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 != "" {
msg := data{
MsgType: "text",
Content: struct{
Text string `json:"text"`
}{Text: stripansi.Strip(line)},
}
wg.Add(1)
go feishuCat(msg,webhookURL, line)
}
} else {
lines += line
lines += "\n"
}
}
if !oneLine {
msg := data{
MsgType: "text",
Content: struct{
Text string `json:"text"`
}{Text: stripansi.Strip(lines)},
}
wg.Add(1)
go feishuCat(msg,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 {
MsgType string `json:"msg_type"`
Content struct {
Text string `json:"text"`
} `json:"content"`
}
func feishuCat(msg data,url string, line string) {
data, _ := json.Marshal(msg)
http.Post(url, "application/json", strings.NewReader(string(data)))
wg.Done()
}