-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
76 lines (68 loc) · 2.42 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strconv"
"wait4it/internal/banner"
"wait4it/pkg/check"
"wait4it/pkg/model"
)
func defaultEnv(env string, def string) string {
v := os.Getenv(env)
if v == "" {
return def
}
return v
}
func defaultEnvInt(env string, def int) int {
v := os.Getenv(env)
if v == "" {
return def
}
i, err := strconv.ParseInt(v, 10, 0)
if err != nil {
return def
}
return int(i)
}
func defaultEnvBool(env string, def bool) bool {
v := os.Getenv(env)
if v == "" {
return def
}
if v == "true" || v == "1" {
return true
}
return false
}
func main() {
cfg := &model.CheckContext{}
flag.StringVar(&cfg.Config.CheckType, "type", defaultEnv("W4IT_TYPE", "tcp"), "define the type of check")
flag.IntVar(&cfg.Config.Timeout, "t", defaultEnvInt("W4IT_TIMEOUT", 30), "Timeout, amount of time wait4it waits for the port in seconds")
flag.StringVar(&cfg.Host, "h", defaultEnv("W4IT_HOST", "127.0.0.1"), "IP of the host you want to test against")
flag.IntVar(&cfg.Port, "p", defaultEnvInt("W4IT_PORT", 0), "Port")
flag.StringVar(&cfg.Username, "u", defaultEnv("W4IT_USERNAME", ""), "Username of the service")
flag.StringVar(&cfg.Password, "P", defaultEnv("W4IT_PASSWORD", ""), "Password of the service, it picks the W4IT_PASSWORD env if it is empty")
flag.StringVar(&cfg.DatabaseName, "n", defaultEnv("W4IT_DBNAME", ""), "Name of the database")
flag.StringVar(&cfg.DBConf.SSLMode, "ssl", defaultEnv("W4IT_SSL_MODE", "disable"), "Enable or Disable ssl mode (for some database or services)")
flag.StringVar(&cfg.DBConf.OperationMode, "operation-mode", defaultEnv("W4IT_OPERATION_MODE", "standalone"), "choose operation mode (for some database or services)")
flag.IntVar(&cfg.HttpConf.StatusCode, "status-code", defaultEnvInt("W4IT_HTTP_STATUS_CODE", 200), "Status code to be expected from http call")
flag.StringVar(&cfg.HttpConf.Text, "http-text", defaultEnv("W4IT_HTTP_TEXT", ""), "Text to check inside http response")
flag.BoolVar(&cfg.HttpConf.FollowRedirect, "http-follow-redirect", defaultEnvBool("W4IT_HTTP_FOLLOW_REDIRECT", true), "Whether to follow the redirect while doing the HTTP check")
flag.Parse()
// We don't want to show password in help message
if cfg.Password == "" {
defaultEnv("W4IT_PASSWORD", "")
}
cfg.Progress = func(s string) {
fmt.Print(s)
}
banner.Banner()
if err := check.RunCheck(context.Background(), cfg); err != nil {
log.Fatal(err)
}
log.Println("Success!")
}