// 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 mergeInConfig(v *viper.Viper, configName, configType string) { v.SetConfigType(configType) configPaths := []string{configName, "../" + configName, "config/" + configName, "../config/" + 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)) } }