-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
34 lines (32 loc) 路 899 Bytes
/
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
package config
import (
"fmt"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"strings"
)
func Parse() (err error) {
flag.Parse()
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if err := viper.BindPFlag(strings.ReplaceAll(f.Name, "-", "."), f); err != nil {
return
}
})
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/mnemonic-ninja/")
viper.AddConfigPath("$HOME/.mnemonic-ninja")
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.SetEnvPrefix("MNEMONIC_NINJA")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error
} else {
// Config file was found but another error was produced
return fmt.Errorf("Fatal error reading config file: %w \n", err)
}
}
return nil
}