/
config.go
56 lines (48 loc) · 1.16 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
package config
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"github.com/BurntSushi/toml"
)
type Config struct {
Addr string `toml:"bind_addr" env:"MTG_BIND_ADDR"`
DbType string `toml:"db_type" env:"MTG_DB_TYPE"`
DbPath string `toml:"db_path" env:"MTG_DB_PATH"`
}
func New() *Config {
c := Config{Addr: ":3000", DbType: "sqlite3", DbPath: "db/development.db"}
return &c
}
func (c *Config) LoadFile(path string) error {
_, err := toml.DecodeFile(path, c)
return err
}
func (c *Config) LoadEnv() error {
value := reflect.Indirect(reflect.ValueOf(c))
typ := value.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
// Retrieve environment variable.
v := strings.TrimSpace(os.Getenv(field.Tag.Get("env")))
if v == "" {
continue
}
// Set the appropriate type.
switch field.Type.Kind() {
case reflect.Bool:
value.Field(i).SetBool(v != "0" && v != "false")
case reflect.Int:
newValue, err := strconv.ParseInt(v, 10, 0)
if err != nil {
return fmt.Errorf("Parse error: %s: %s", field.Tag.Get("env"), err)
}
value.Field(i).SetInt(newValue)
case reflect.String:
value.Field(i).SetString(v)
}
}
return nil
}