-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
45 lines (38 loc) · 860 Bytes
/
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
package common
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func LoadConfig(filenames []string, obj interface{}) error {
for _, filename := range filenames {
if _, err := os.Stat(filename); os.IsNotExist(err) {
continue
}
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if err := json.Unmarshal(bytes, obj); err != nil {
return err
}
}
return nil
}
func LoadEnvironmentConfig(envVars []string, obj interface{}) error {
for _, envVar := range envVars {
envValue := os.Getenv(envVar)
if envValue == "" {
return fmt.Errorf("%s not found", envVar)
}
if err := json.Unmarshal([]byte(envValue), obj); err != nil {
return fmt.Errorf("%s errored: %s", envVar, err.Error())
}
}
return nil
}
type ServiceConfig struct {
Addr string `json:"address"`
Service string `json:"service"`
}