-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
51 lines (45 loc) · 1.22 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
package app
import (
"fmt"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
"sort"
"strings"
)
var cfgFile string
// addConfigFlag adds flags for a specific server to the specified FlagSet
// object.
func addConfigFlag(basename string, fs *pflag.FlagSet) {
viper.SetEnvPrefix(strings.Replace(strings.ToUpper(basename), "-", "_", -1))
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
cobra.OnInitialize(initCobra)
fs.StringVarP(&cfgFile, "config", "C", cfgFile, "Read configuration from specified `FILE`, support JSON, TOML, YAML, HCL, or Java properties formats.")
}
func initCobra() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: failed to read configuration file: %v\n", err)
os.Exit(1)
}
}
}
func printConfig() {
keys := viper.AllKeys()
sort.Strings(keys)
if len(keys) > 0 {
fmt.Printf("%v Configuration items:\n", progressMessage)
table := uitable.New()
table.Separator = " "
table.MaxColWidth = 80
table.RightAlign(0)
for _, k := range keys {
table.AddRow(fmt.Sprintf("%s:", k), viper.Get(k))
}
fmt.Println(table)
}
}