本文整理汇总了Golang中github.com/spf13/viper.Viper.AddRemoteProvider方法的典型用法代码示例。如果您正苦于以下问题:Golang Viper.AddRemoteProvider方法的具体用法?Golang Viper.AddRemoteProvider怎么用?Golang Viper.AddRemoteProvider使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/spf13/viper.Viper
的用法示例。
在下文中一共展示了Viper.AddRemoteProvider方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetEnvironmentSpecificConfig
// 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: setConfigPath
// 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
}