Ejemplo n.º 1
0
// 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
}
Ejemplo n.º 2
0
// 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
}
Ejemplo n.º 3
0
// 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}
}
Ejemplo n.º 4
0
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))
	}
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
0
// setConfigPath set the primary source of configuration for the service.
func setConfigPath(v *viper.Viper, uri string) error {
	p, err := url.Parse(uri)

	if err != nil {
		return err
	}

	// Local path.
	if p.Scheme == "" {
		v.SetConfigFile(uri)
		return nil
	}

	// Consul.
	if p.Scheme == "consul" {
		ep := fmt.Sprintf("http://%s", p.Host)
		v.AddRemoteProvider("consul", ep, trimExt(p.Path))
		v.SetConfigType(filepath.Ext(p.Path)[1:])
		return nil
	}

	return nil
}