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 8529383
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 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
10 changes: 7 additions & 3 deletions pkg/execute/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@ func (e *ConfigExecutor) renderBotkubeConfiguration() (string, error) {
val.CloudTeams.Server.TLS.CACertificate = nil

// Replace private channel names with aliases
for i, channel := range val.CloudSlack.Channels {
cloudSlackChannels := make(config.IdentifiableMap[config.CloudSlackChannel])
for _, channel := range val.CloudSlack.Channels {
if channel.Alias == nil {
cloudSlackChannels[channel.ChannelBindingsByName.Name] = channel
continue
}

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

// maps are not addressable: https://stackoverflow.com/questions/42605337/cannot-assign-to-struct-field-in-a-map
cfg.Communications[key] = val
Expand Down

0 comments on commit 8529383

Please sign in to comment.