-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
63 lines (52 loc) · 1.14 KB
/
config.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
)
type ConfigStruct struct {
Redis struct {
Hostname string `json:"hostname"`
AuthKey string `json:"authKey"`
}
Ingest struct {
UseHttps bool `json:"useHttps"`
Hostname string `json:"hostname"`
Username string `json:"username"`
Password string `json:"password"`
AuthKey string `json:"authKey"`
}
}
var Config *ConfigStruct
func NewConfig(configPath string) error {
Config = &ConfigStruct{}
d, err := os.ReadFile(configPath)
if err != nil {
log.Fatalf("unable to read config %v", err)
}
if err := json.Unmarshal(d, &Config); err != nil {
log.Fatalf("unable to read config %v", err)
}
return nil
}
func ParseFlags() (string, error) {
var configPath string
flag.StringVar(&configPath, "config", "./config.json", "path to config file")
flag.Parse()
if err := ValidateConfigPath(configPath); err != nil {
return "", err
}
return configPath, nil
}
func ValidateConfigPath(path string) error {
s, err := os.Stat(path)
if err != nil {
return err
}
if s.IsDir() {
return fmt.Errorf("'%s' is a directory, not a normal file", path)
}
return nil
}