-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
74 lines (66 loc) · 1.59 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
package main
import (
"fmt"
"github.com/larrabee/ewn-go/ewn"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
cli := ewn.GetCliArgs()
lock := ewn.Lock{Key: cli.DontDuplicateKey}
msg := ewn.Message{Args: cli}
if (cli.InitConfig == false) && (cli.Command == "") {
fmt.Fprintln(os.Stderr, "error: --command is required")
os.Exit(1)
}
if cli.InitConfig {
err := ewn.InitConfig(cli.Config)
if err != nil {
fmt.Fprintln(os.Stderr, "Config initialization failed with error: ", err)
os.Exit(1)
}
fmt.Printf("Default config file successfully created in %s file\nPlease update it with your values\n", cli.Config)
os.Exit(0)
}
cfg, err1 := ewn.GetConfig(cli.Config)
if err1 != nil {
panic(err1)
}
if len(cli.Recipients) != 0 {
cfg.Set("email.recipients", cli.Recipients)
}
if cli.Daemon {
fmt.Fprintln(os.Stderr, "Daemonization not implementet. Running in normal mode.")
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP)
msg.Host, _ = os.Hostname()
if cli.DontDuplicate {
err2 := lock.Acquire()
if err2 != nil {
msg.GeneralError = err2
ewn.Notify(&msg, cfg)
os.Exit(1)
}
defer lock.Release()
}
RetryLoop:
for retryCounter := 1; retryCounter <= cli.Retry; retryCounter++ {
retry, err := ewn.Popen(cli.Command, time.Duration(cli.Timeout)*time.Second, cli.Tty)
if err != nil {
msg.GeneralError = err
ewn.Notify(&msg, cfg)
os.Exit(1)
}
retry.Retry = retryCounter
msg.Retries = append(msg.Retries, retry)
for _, v := range cli.ValidExitCode {
if retry.ExitCode == v {
break RetryLoop
}
}
}
ewn.Notify(&msg, cfg)
}