-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
41 lines (35 loc) · 830 Bytes
/
main.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
package main
import (
"fmt"
"os"
"github.com/spf13/viper"
)
type DatabaseConfig struct {
Host string `mapstructure:"hostname"`
Port string
User string `mapstructure:"username"`
Pass string `mapstructure:"password"`
}
type OutputConfig struct {
File string
}
type Config struct {
Db DatabaseConfig `mapstructure:"database"`
Out OutputConfig `mapstructure:"output"`
}
func main() {
v := viper.New()
v.SetConfigName("config")
v.AddConfigPath(".")
if err := v.ReadInConfig(); err != nil {
fmt.Printf("couldn't load config: %s", err)
os.Exit(1)
}
var c Config
if err := v.Unmarshal(&c); err != nil {
fmt.Printf("couldn't read config: %s", err)
}
fmt.Printf("host=%s port=%s user=%s pass=%s\n", c.Db.Host, c.Db.Port, c.Db.User, c.Db.Pass)
fmt.Printf("output=%s\n", c.Out.File)
fmt.Printf("%#v", c)
}