// EnvironmentSpecificConfig sets the configuration to match the given env. func EnvironmentSpecificConfig(v *viper.Viper, env string) *viper.Viper { if v == nil { v = viper.New() v.SetDefault("env", env) } // Read common config v.AddConfigPath(".") v.AddConfigPath("../") v.AddConfigPath("config/") v.AddConfigPath("../config/") v.SetConfigName("config-common") v.SetConfigType("yml") if err := v.ReadInConfig(); err != nil { panic(fmt.Errorf("Fatal error reading common config file: %s \n", err)) } // Merge in environment specific config mergeInConfig(v, "config-"+env+".yml", "yml") // Merge in version config mergeInConfig(v, "version.properties", "properties") // Read config from consul // v.AddRemoteProvider("consul", "http://someconsul/endpoint", "some/path/to/config.json") // v.SetConfigType("yaml") // err = v.ReadRemoteConfig() // Find and read the config file // if err != nil { // Handle errors reading the config file // panic(fmt.Errorf("Fatal error reading remote config file: %s \n", err)) // } return v }
// ParseViper tries to parse out a Viper from a configuration file. func ParseViper(v *viper.Viper, configFile string) error { filename := filepath.Base(configFile) ext := filepath.Ext(configFile) configPath := filepath.Dir(configFile) v.SetConfigType(strings.TrimPrefix(ext, ".")) v.SetConfigName(strings.TrimSuffix(filename, ext)) v.AddConfigPath(configPath) if err := v.ReadInConfig(); err != nil { return fmt.Errorf("Could not read config at :%s, viper error: %v", configFile, err) } return nil }
// SetEnvironmentSpecificConfig sets the configuration to match the given env. func SetEnvironmentSpecificConfig(v *viper.Viper, env string) { if v == nil { v = viper.New() v.SetDefault("env", env) } // Read common config v.AddConfigPath(".") v.AddConfigPath("../") v.SetConfigName("config-common") if err := v.ReadInConfig(); err != nil { panic(fmt.Errorf("Fatal error reading common config file: %s \n", err)) } // Merge in environment specific config configName := "config-" + env + ".yml" configPaths := []string{configName, "../" + configName} configFilePath := "" for _, path := range configPaths { if b, _ := exists(path); b { configFilePath = path continue } } if configFilePath == "" { panic(fmt.Errorf("Could not find config file: %s \n", configName)) } configBytes, err := ioutil.ReadFile(configFilePath) if err != nil { panic(fmt.Errorf("Could not read config file: %s \n", err)) } err = v.MergeConfig(bytes.NewBuffer(configBytes)) // Find and read the config file if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } // Read config from consul v.AddRemoteProvider("consul", v.GetString("ConsulEndpoint"), v.GetString("ConsulSupportOptimizationWorkflowConfig")) v.SetConfigType("yaml") // err = v.ReadRemoteConfig() // Find and read the config file // if err != nil { // Handle errors reading the config file // panic(fmt.Errorf("Fatal error reading remote config file: %s \n", err)) // } Viper = &config{v} }
func singalhandler(sig os.Signal, v *viper.Viper) error { switch sig { case syscall.SIGHUP: log.Println("got hup signal, now reloading conf") err := v.ReadInConfig() if err != nil { glog.Infoln("Fatal error config file ", err) return utils.ErrReadConfig } case syscall.SIGTERM: log.Println("receive SIGTERM, exit") //maybe graceful stop is better:) os.Exit(0) default: log.Println(sig) glog.Infoln("not ready to process ", sig.String()) } return nil }
func getSetup(fileName string, cfg *viper.Viper) error { // setup file abs, err := filepath.Abs(fileName) if err != nil { return fmt.Errorf("\nSorry, the marmots were unable to find the absolute path to the account types file.") } path := filepath.Dir(abs) file := filepath.Base(abs) extName := filepath.Ext(file) bName := file[:len(file)-len(extName)] cfg.AddConfigPath(path) cfg.SetConfigName(bName) cfg.SetConfigType(strings.Replace(extName, ".", "", 1)) // load file if err := cfg.ReadInConfig(); err != nil { return fmt.Errorf("\nSorry, the marmots were unable to load the file: (%s). Please check your path.\nERROR =>\t\t\t%v", fileName, err) } return nil }
func singalhandler(sig os.Signal, v *viper.Viper, dp *dialer.DialerPool) error { switch sig { case syscall.SIGHUP: log.Println("HUP") glog.Infof("got hup signal, now reloading conf\n", sig.String()) err := v.ReadInConfig() if err != nil { glog.Infoln("Fatal error config file ", err) return utils.ErrReadConfig } zones := v.GetStringMap("zones") dp.AddByZones(zones) case syscall.SIGTERM: glog.Infoln("receive SIGTERM, exit") //maybe graceful stop is better:) os.Exit(0) default: log.Println(sig) glog.Infoln("not ready to process ", sig.String()) } return nil }