Skip to content

Instantly share code, notes, and snippets.

@mrsoftware
Created September 27, 2019 10:33
Show Gist options
  • Save mrsoftware/3b16f5feb3095b2a20017726d845d879 to your computer and use it in GitHub Desktop.
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
// 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