-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
58 lines (48 loc) · 1.33 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
package config
import (
"fmt"
"os"
"github.com/spf13/viper"
)
// Config struct
type Config struct {
}
// NewConfig constructor
func NewConfig() (*Config, error) {
if _, err := os.Stat("config.yaml"); err == nil {
viper.SetConfigName("config.yaml")
viper.SetConfigType("yaml")
viper.AddConfigPath("./config")
err := viper.ReadInConfig() // Find and read the config file
if err != nil {
return nil, fmt.Errorf("fatal error config file: %s ", err.Error())
}
}
viper.SetConfigName(".env")
viper.SetConfigType("env")
viper.AddConfigPath("./")
err := viper.MergeInConfig() // Find and read the config file
if err != nil {
return nil, fmt.Errorf("fatal error config file: %s ", err.Error())
}
if _, err := os.Stat("./.env.local"); err == nil {
viper.SetConfigName(".env.local")
err = viper.MergeInConfig() // Find and read the config file
if err != nil {
return nil, fmt.Errorf("fatal error config file: %s ", err.Error())
}
}
return &Config{}, nil
}
// GetString returns string from config
func (c Config) GetString(key string) string {
return viper.GetString(key)
}
// GetInt returns int from config
func (c Config) GetInt(key string) int {
return viper.GetInt(key)
}
// UnmarshalKey fill struct from config
func (c Config) UnmarshalKey(key string, strct interface{}) error {
return viper.UnmarshalKey(key, strct)
}