I’m trying to override AllInputChannels
with an envvar. The main config is in config.yaml
which is picked up fine.
Running the below code viper.Unmarshal()
throws an error:
'AllInputChannels' expected a map, got 'string'
How should I present the value in the envvar so it’s recognised?
I’ve removed error checking code below for clarity, except where the error appears.
type ChannelMap struct {
Team string `mapstructure:"Team"`
Channel string `mapstructure:"Channel"`
}
type AllInputChannels struct {
Production []ChannelMap `mapstructure:"Production"`
Staging []ChannelMap `mapstructure:"Staging"`
}
type Config struct {
AllInputChannels AllInputChannels `mapstructure:"AllInputChannels"`
}
func NewConfig() Config { return Config{} }
func Build() Config {
conf := NewConfig()
_ = os.Setenv("ALLINPUTCHANNELS", `{"staging":[{"team":"Foo","channel":"DEF456"}]}`)
viper.AddConfigPath(".") // Local builds.
viper.SetConfigName("config")
viper.SetConfigType("yaml")
_ = viper.BindEnv("AllInputChannels")
_ = viper.ReadInConfig()
err := viper.Unmarshal(&conf)
if err != nil {
fmt.Printf("unable to decode config, %v", err)
}
return conf, nil
}
func main() {
_ = Build()
}