-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
77 lines (65 loc) · 1.69 KB
/
args.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
package main
import (
"flag"
"log"
"os"
"github.com/aeytom/silence-data/hass"
"github.com/go-yaml/yaml"
)
type DotEnv struct {
Silence struct {
Email string `yaml:"email" json:"email,omitempty"`
Password string `yaml:"password" json:"password,omitempty"`
} `yaml:"silence" json:"silence,omitempty"`
Influx struct {
Org string `yaml:"org,omitempty" json:"org,omitempty"`
Bucket string `yaml:"bucket,omitempty" json:"bucket,omitempty"`
Token string `yaml:"token" json:"token,omitempty"`
Url string `yaml:"url" json:"url,omitempty"`
} `yaml:"influx" json:"influx,omitempty"`
HomeAssistant hass.Config `yaml:"home_assistant,omitempty" json:"home_assistant,omitempty"`
}
// Command line args
var (
Conf DotEnv
)
// ParseArgs parses command line flags
func ParseArgs() {
envPath := getEnvArg("DOT_ENV", "dotEnv", ".env.yaml", "dot env path")
flag.Parse()
ed, err := os.ReadFile(*envPath)
if err != nil {
log.Fatalln(err)
}
err = yaml.Unmarshal([]byte(ed), &Conf)
if err != nil {
log.Fatalln(err)
}
if Conf.Silence.Email == "" {
log.Fatal("You must set .env.yaml silence.email")
}
if Conf.Silence.Password == "" {
log.Fatal("You must set .env.yaml silence.password")
}
if Conf.Influx.Token == "" {
log.Fatal("You must set .env.yaml influx.token")
}
if Conf.Influx.Bucket == "" {
Conf.Influx.Bucket = "silence"
}
if Conf.Influx.Org == "" {
Conf.Influx.Org = "primary"
}
if Conf.Influx.Url == "" {
log.Fatal("You must set .env.yaml influx.url")
}
log.Printf("%#v", Conf)
}
func getEnvArg(env string, arg string, dflt string, usage string) *string {
ev, avail := os.LookupEnv(env)
if avail {
dflt = ev
}
v := flag.String(arg, dflt, usage)
return v
}