コード例 #1
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}
}
コード例 #2
0
ファイル: config.go プロジェクト: chop-dbhi/dcc
// 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
}