Created
September 27, 2019 10:33
-
-
Save mrsoftware/3b16f5feb3095b2a20017726d845d879 to your computer and use it in GitHub Desktop.
With this method you can open any config file that supported by viper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// go run main.go --config ./config/config.yaml | |
// go run main.go --config ./config/config.json | |
// go run main.go --config ./config/config.toml | |
// file name also can be any name : config, app and etc. | |
var configFile = flag.String("config", "./config/config.yaml", "Address of config file") | |
func parseConfig() { | |
dir, file := path.Split(*configFile) | |
ext := path.Ext(file) | |
viper.SetConfigName(strings.ReplaceAll(file, ext, "")) | |
viper.SetConfigType(strings.ReplaceAll(ext, ".", "")) | |
viper.AddConfigPath(dir) | |
if err := viper.ReadInConfig(); err != nil { | |
panic(fmt.Errorf("Fatal error config file: %s ", err)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment