-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (104 loc) · 2.82 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/user"
"path"
"strings"
)
func main() {
var ctx Context
ctx.ArgWhere = flag.String("where", "slack", "service to post [slack]")
ctx.ArgProfileName = flag.String("profile", "default", "profile name [default]")
ctx.ArgCode = flag.Bool("code", false, "paste code block [disabled]")
ctx.ArgHTML = flag.Bool("html", false, "use HTML for emails")
ctx.ArgFrom = flag.String("from", "default", "From field (email)")
ctx.ArgTo = flag.String("to", "default", "To field (email)")
ctx.ArgSubject = flag.String("subject", "default", "Subject field (email)")
ctx.ArgDry = flag.Bool("dry", false, "Dry mode (won't send anything)")
ctx.ArgVerbose = flag.Bool("verbose", false, "Verbose")
flag.Parse()
if *ctx.ArgVerbose {
for ai, arg := range flag.Args() {
fmt.Printf("arg %02d = %s\n", ai, arg)
}
}
validWheres := []string{"slack", "telegram", "mailgun", "twilio"}
found := false
for _, v := range validWheres {
if *ctx.ArgWhere == v {
found = true
}
}
if !found {
fmt.Printf("Option '%s' unsupported, supported types are: %s\n",
*ctx.ArgWhere, strings.Join(validWheres, ", "))
os.Exit(1)
}
configPath := getConfigPath()
cfg := NewConfig(configPath, *ctx.ArgProfileName)
ctx.Config = cfg
// @todo think of doing it so that listening for stdin could be
// continuous. if there's 100s delay and something gets spitted
// out, maybe we could send it to slack too
msgStr := getMsg()
// @todo adjust for HTML
if *ctx.ArgCode {
msgStr = "```\n" + msgStr + "```\n"
}
if *ctx.ArgDry {
fmt.Printf("# Dry mode, this would have been sent, but will be muffled\n")
fmt.Printf("%s\n", msgStr)
os.Exit(0)
}
if *ctx.ArgWhere == "slack" {
println("slack")
MsgSlack(&ctx, msgStr)
} else if *ctx.ArgWhere == "telegram" {
println("telegram")
MsgTelegram(&ctx, msgStr)
} else if *ctx.ArgWhere == "mailgun" {
println("mailgun")
MsgMailgun(&ctx, msgStr)
} else if *ctx.ArgWhere == "twilio" {
println("twilio")
MsgSMS(&ctx, msgStr)
} else {
println("usage")
flag.Usage()
}
}
func getMsg() string {
reader := bufio.NewReader(os.Stdin)
fullText := ""
for {
txt, err := reader.ReadString('\n')
if err != nil {
break
}
fullText += (strings.Trim(txt, "\n") + "\n")
}
return fullText
}
func getConfigPath() string {
u, err := user.Current()
if err != nil {
panic(err)
}
// @todo lookup what the modern standards are for config files
configPathAlt1 := path.Join(u.HomeDir, ".config", ".msgr.conf")
configPathAlt2 := path.Join(u.HomeDir, ".msgr.conf")
fmt.Print("files: ", configPathAlt1, configPathAlt2)
configPath := configPathAlt1
_, err = os.Stat(configPathAlt1)
if os.IsNotExist(err) {
_, err = os.Stat(configPathAlt2)
configPath = configPathAlt2
}
if os.IsNotExist(err) {
panic("can't find config file")
}
return configPath
}