-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (108 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bufio"
"flag"
"fmt"
"github.com/solutionstack/notifier-cli/notifier"
"io"
"net/url"
"os"
"os/signal"
"regexp"
"strconv"
"syscall"
)
type CliOptions struct {
url string
interval string
}
var cliOptions CliOptions
var fileData []string
func main() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
defer func() {
if r := recover(); r != nil {
fmt.Println("The program encountered and error: ", r)
}
}()
cliOptions = CliOptions{}
var helpFlag,helpFlagShort *bool
flag.StringVar(&cliOptions.url, "url", "", "")
flag.StringVar(&cliOptions.interval, "interval", "0s", "")
flag.StringVar(&cliOptions.interval, "i", "0s", "")
helpFlag = flag.Bool("help", false, "print help message")
helpFlagShort = flag.Bool("h", false, "print help message")
flag.Parse()
// if user does not supply flags, print usage
if flag.NFlag() == 0 {
printHelp()
os.Exit(0)
}
if *helpFlag == true || *helpFlagShort == true{
printHelp()
return
}
_, err := url.ParseRequestURI(cliOptions.url)
if err != nil {
panic(err)
}
readFile()
processData()
}
func readFile() {
if !stdInAvailable() {
panic("no input stream detected")
}
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("reading file...")
for scanner.Scan() {
text := scanner.Text()
fileData = append(fileData, text)
}
err := scanner.Err()
if err != nil && err != io.EOF {
panic(err)
}
fmt.Println("reading file complete")
}
func stdInAvailable() bool {
file := os.Stdin
fi, err := file.Stat()
if err != nil {
panic(fmt.Sprint("file.Stat(): ", err))
}
size := fi.Size()
if size > 0 {
return true
} else {
return false
}
}
func printHelp() {
fmt.Println("usage: notify --url=URL [<flags>]")
fmt.Println("Flags:\n\t--i --interval=5s Notification interval\n\t--h --help Print this help message")
}
func processNotifyEvents(event notifier.MessageEvent, messageId int, errBody string) {
switch event {
case notifier.CompletedEvent:
fmt.Println("\ninput processing is complete, exit.")
os.Exit(0)
default:
printEvent(event , messageId, errBody)
}
}
func printEvent(event notifier.MessageEvent, messageId int, errBody string) {
if event == notifier.SuccessEvent{
fmt.Printf("message_id[%d] text:%s | processed succesfully\n", messageId, fileData[messageId][0:10]+"..." )
}else{
fmt.Printf("message_id[%d] text:%s | failed with :%s | error: %s \n", messageId, fileData[messageId][0:10]+"...", event, errBody)
}
}
func processData() {
fmt.Println("\nstarting processing")
re := regexp.MustCompile("[0-9]+") //re to get the actual number from the interval
interval, _ := strconv.ParseInt(re.FindAllString(cliOptions.interval,1)[0], 10, 32)
n := notifier.NewNotifier(cliOptions.url, fileData, int(interval), processNotifyEvents)
n.ProcessMessages()
}