Skip to content

Commit

Permalink
Properly load aliases from config
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Jul 2, 2024
1 parent 4149b8e commit 58b97ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 17 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
koanfyaml "github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/mitchellh/mapstructure"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -197,12 +198,12 @@ type IncomingWebhook struct {

// CloudSlackChannel contains configuration bindings per channel.
type CloudSlackChannel struct {
ChannelBindingsByName `yaml:",inline"`
ChannelBindingsByName `yaml:",inline" mapstructure:",squash"`

// ChannelID is the Slack ID of the channel.
ChannelID string `yaml:"channelID"`
// Alias is an optional public alias for a private channel.
Alias *string `yaml:"alias"`
Alias *string `yaml:"alias,omitempty"`
}

// ChannelBindingsByName contains configuration bindings per channel.
Expand Down Expand Up @@ -725,7 +726,20 @@ func LoadWithDefaults(configs [][]byte) (*Config, LoadWithDefaultsDetails, error
}

var cfg Config
err = k.Unmarshal("", &cfg)
err = k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{
DecoderConfig: &mapstructure.DecoderConfig{
Squash: true, // needed to properly unmarshal CloudSlack channel's ChannelBindingsByName

// also use defaults from koanf.UnmarshalWithConf
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
mapstructure.TextUnmarshallerHookFunc()),
Metadata: nil,
Result: &cfg,
WeaklyTypedInput: true,
},
})
if err != nil {
return nil, LoadWithDefaultsDetails{}, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/execute/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func (e *ConfigExecutor) renderBotkubeConfiguration() (string, error) {
}

outChannel := channel
outChannel.ChannelBindingsByName.Name = *channel.Alias
outChannel.ChannelBindingsByName.Name = fmt.Sprintf("%s (public alias)", *channel.Alias)
outChannel.Alias = nil
val.CloudSlack.Channels[i] = outChannel
}

Expand Down

0 comments on commit 58b97ac

Please sign in to comment.