-
Notifications
You must be signed in to change notification settings - Fork 43
/
config.go
83 lines (70 loc) · 2.08 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
package config
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/albertwidi/go-project-example/internal/kothak"
"github.com/albertwidi/go-project-example/internal/pkg/envfile"
"github.com/albertwidi/go-project-example/internal/pkg/tempe"
"gopkg.in/yaml.v2"
)
// DefaultConfig for the project
type DefaultConfig struct {
Servers DefaultServers `json:"servers" yaml:"servers" toml:"servers"`
Log DefaultLog `json:"log" yaml:"log" toml:"log"`
Resources kothak.Config `json:"resources" yaml:"resources" toml:"resources"`
}
// DefaultLog config for the project
type DefaultLog struct {
Level string `json:"level" yaml:"level" toml:"level"`
File string `json:"file" yaml:"file" toml:"file"`
Color bool `json:"use_color" yaml:"use_color" toml:"use_color"`
}
// DefaultServers struct
type DefaultServers struct {
Main ServerConfig `json:"main" yaml:"main" toml:"main"`
Debug ServerConfig `json:"debug" yaml:"debug" toml:"debug"`
Admin ServerConfig `json:"admin" yaml:"admin" toml:"admin"`
}
// ServerConfig struct
type ServerConfig struct {
Address string `yaml:"address" toml:"address"`
}
// ParseFile for parsing config file and return DefaultConfig struct
func ParseFile(configFile string, dest interface{}, envFiles ...string) error {
// prepare to replace ${ENV_VAR_NAME} with environment variable
t, err := tempe.New(tempe.EnvVarPattern, tempe.EnvVarReplacerFunc)
if err != nil {
return err
}
if err := envfile.Load(envFiles...); err != nil {
return err
}
out, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
// replacing with environment variables
out, err = t.ReplaceBytes(out)
if err != nil {
return err
}
ext := filepath.Ext(configFile)
switch ext {
case ".toml":
err = toml.Unmarshal(out, dest)
case ".yaml":
err = yaml.Unmarshal(out, dest)
default:
err = fmt.Errorf("config: file ext is not valid. got %s", ext)
}
if err != nil {
return err
}
return nil
}
// Print configuration in json schema
// all config with tag=protected:1 will be hidden from the print
func Print(v interface{}) {
}