-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
112 lines (97 loc) · 3.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package config_root
import (
"fmt"
"os"
"github.com/fatih/structs"
"github.com/leebenson/conform"
"github.com/sanity-io/litter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Setting through a file, environment vars, and/or cli parameters.
var Setting *setting
var initial = &setting{
LogBasepath: "/var/log/fishler",
Config: ".fishler",
Debug: false,
DockerImagename: "fishler",
DockerBasepath: "docker",
}
// Create private data struct to hold setting options.
// `mapstructure` => viper tags
// `struct` => fatih structs tag
// `env` => environment variable name
type setting struct {
LogBasepath string `mapstructure:"log-basepath" structs:"log-basepath" env:"FISHLER_LOG_BASEPATH"`
Config string `mapstructure:"config" structs:"config" env:"FISHLER_CONFIG"`
Debug bool `mapstructure:"debug" structs:"debug" env:"FISHLER_DEBUG"`
DockerImagename string `mapstructure:"docker-imagename" structs:"docker-imagename" env:"FISHLER_DOCKER_IMAGENAME"`
DockerBasepath string `mapstructure:"docker-basepath" structs:"docker-basepath" env:"FISHLER_DOCKER_BASEPATH"`
}
func Load() {
// Priority of configuration options
// 1: CLI Parameters
// 2: environment
// 3: config.yaml
// 4: defaults
// Create a map of the default config
defaultsAsMap := structs.Map(initial)
// Set defaults
for key, value := range defaultsAsMap {
viper.SetDefault(key, value)
}
// Read config from file
viper.SetConfigName(".fishler")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
// Unmarshal config into struct
Setting = &setting{}
err := viper.Unmarshal(Setting)
if err != nil {
fmt.Printf("Failed: %v\n", err)
os.Exit(1)
}
}
// configInit must be called from the packages' init() func
func CommandInit(command *cobra.Command) error {
// Keep cli parameters in sync with the config struct
command.PersistentFlags().String("docker-imagename", initial.DockerImagename, "The image user for the docker container")
command.PersistentFlags().String("docker-basepath", initial.DockerBasepath, "The path to the docker folder ./docker if run from the root of the project")
command.PersistentFlags().StringP("log-basepath", "l", initial.LogBasepath, "The base filepath where logs will be stored")
command.PersistentFlags().StringP("config", "c", initial.Config, ".fishler.yaml")
command.PersistentFlags().BoolP("debug", "d", initial.Debug, "Output debug information")
for _, field := range structs.Fields(&setting{}) {
// Get the struct tag values
key := field.Tag("structs")
env := field.Tag("env")
if key == "" {
continue
}
// Bind cobra flags to viper
err := viper.BindPFlag(key, command.PersistentFlags().Lookup(key))
if err != nil {
return err
}
err = viper.BindEnv(key, env)
if err != nil {
return err
}
}
return nil
}
// Print the config object
// but remove sensitive data
func (c *setting) Print() {
cp := *c
_ = conform.Strings(&cp)
litter.Dump(cp)
}
// String the config object
// but remove sensitive data
func (c *setting) String() string {
cp := *c
_ = conform.Strings(&cp)
return litter.Sdump(cp)
}