/
config.go
75 lines (65 loc) · 1.58 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
package main
import (
"fmt"
"os"
"github.com/vrischmann/envconfig"
"gopkg.in/yaml.v2"
)
// Config struct
type Config struct {
DB struct {
Address string `yaml:"address"`
Port string `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Dbname string `yaml:"dbname"`
SSLRootCert string `yaml:"sslrootcert"`
} `yaml:"db"`
Hpcs struct {
Address string `yaml:"host"`
Port string `yaml:"port"`
InstanceId string `yaml:"instance_id"`
IAMKey string `yaml:"iam_key"`
IAMEndpoint string `yaml:"iam_endpoint"`
}
// mysql or pg
DbType string `yaml:"db_type"`
SecureEnclavePath string `yaml:"secure_enclave_path"`
}
// NewConfig returns a new decoded Config struct
func NewDBConfig(configPath string) (*Config, error) {
// Create config structure
config := &Config{}
// Open config file
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
// Init new YAML decode
d := yaml.NewDecoder(file)
// Start YAML decoding from file
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
// ValidateConfigPath just makes sure, that the path provided is a file,
// that can be read
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
}
func loadConfigFromEnv() (*Config, error) {
config := &Config{}
if err := envconfig.Init(&config); err != nil {
return nil, err
}
return config, nil
}