-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
140 lines (115 loc) · 2.62 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
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 (
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)
const (
ConfigEmail = "EMAIL"
ConfigToken = "API_TOKEN"
ConfigDomain = "DOMAIN"
ConfigHosts = "HOSTS"
)
type ConfigModel struct {
Email string `yaml:"Email"`
Token string `yaml:"Token"`
Domain string `yaml:"Domain"`
Hosts []string `yaml:"Hosts"`
}
type ConfigClient struct{}
func NewConfigClient() ConfigClient {
c := ConfigClient{}
c.RefreshEnv()
return c
}
func (cc *ConfigClient) LoadConfig() ConfigModel {
// load yaml first
model, err := LoadYaml("config.yaml")
if err != nil {
log.Print(err)
}
// refresh env to make sure its current
cc.RefreshEnv()
// if no domains pulled from yaml, load from env
if len(model.Hosts) == 0 {
envHosts := cc.GetConfig(ConfigHosts)
model.Hosts = append(model.Hosts, strings.Split(envHosts, ",")...)
}
if model.Domain == "" {
model.Domain = cc.GetConfig(ConfigDomain)
}
if model.Email == "" {
model.Email = cc.GetConfig(ConfigEmail)
}
if model.Token == "" {
model.Token = cc.GetConfig(ConfigToken)
}
return model
}
func (cc *ConfigClient) GetConfig(key string) string {
res, filled := os.LookupEnv(key)
if !filled {
log.Printf("Missing the a value for '%v'. Could generate errors.", key)
}
return res
}
func (cc *ConfigClient) GetFeature(flag string) (bool, error) {
cc.RefreshEnv()
res, filled := os.LookupEnv(flag)
if !filled {
errorMessage := fmt.Sprintf("'%v' was not found", flag)
return false, errors.New(errorMessage)
}
b, err := strconv.ParseBool(res)
if err != nil {
return false, err
}
return b, nil
}
// Use this when your ConfigClient has been opened for awhile and you want to ensure you have the most recent env changes.
func (cc *ConfigClient) RefreshEnv() {
// Check to see if we have the env file on the system
_, err := os.Stat(".env")
// We have the file, load it.
if err == nil {
_, err := os.Open(".env")
if err == nil {
loadEnvFile()
}
}
}
func loadEnvFile() {
err := godotenv.Load()
if err != nil {
log.Fatalln(err)
}
}
func (cc *ConfigClient) IsConfigInCurrentDirectory(fileName string) error {
_, err := os.Stat(fileName)
if err != nil {
return err
}
return nil
}
func LoadYaml(yamlFile string) (ConfigModel, error) {
var results = ConfigModel{}
// check for the config in the current directory
_, err := os.Stat(yamlFile)
if err != nil {
return ConfigModel{}, err
}
content, err := os.ReadFile(yamlFile)
if err != nil {
return ConfigModel{}, err
}
err = yaml.Unmarshal(content, &results)
if err != nil {
return ConfigModel{}, err
}
return results, nil
}