Skip to content

Commit

Permalink
fix: config/secret override was writing merged configs (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored Mar 8, 2024
1 parent 33ee783 commit f989eda
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions common/configuration/projectconfig_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ var _ Resolver[Secrets] = ProjectConfigResolver[Secrets]{}
func (p ProjectConfigResolver[R]) Role() R { var r R; return r }

func (p ProjectConfigResolver[R]) Get(ctx context.Context, ref Ref) (*url.URL, error) {
mapping, err := p.getMapping(ctx, ref.Module)
config, err := p.loadConfig(ctx)
if err != nil {
return nil, err
}
mapping, err := p.getMapping(config, ref.Module)
if err != nil {
return nil, err
}
Expand All @@ -52,7 +56,7 @@ func (p ProjectConfigResolver[R]) List(ctx context.Context) ([]Entry, error) {
moduleNames = append(moduleNames, "")
for _, moduleName := range moduleNames {
module := optional.Zero(moduleName)
mapping, err := p.getMapping(ctx, module)
mapping, err := p.getMapping(config, module)
if err != nil {
return nil, err
}
Expand All @@ -72,21 +76,29 @@ func (p ProjectConfigResolver[R]) List(ctx context.Context) ([]Entry, error) {
}

func (p ProjectConfigResolver[R]) Set(ctx context.Context, ref Ref, key *url.URL) error {
mapping, err := p.getMapping(ctx, ref.Module)
config, err := p.loadWritableConfig(ctx)
if err != nil {
return err
}
mapping, err := p.getMapping(config, ref.Module)
if err != nil {
return err
}
mapping[ref.Name] = (*pc.URL)(key)
return p.setMapping(ctx, ref.Module, mapping)
return p.setMapping(config, ref.Module, mapping)
}

func (p ProjectConfigResolver[From]) Unset(ctx context.Context, ref Ref) error {
mapping, err := p.getMapping(ctx, ref.Module)
config, err := p.loadWritableConfig(ctx)
if err != nil {
return err
}
mapping, err := p.getMapping(config, ref.Module)
if err != nil {
return err
}
delete(mapping, ref.Name)
return p.setMapping(ctx, ref.Module, mapping)
return p.setMapping(config, ref.Module, mapping)
}

func (p ProjectConfigResolver[R]) configPaths() []string {
Expand All @@ -101,23 +113,28 @@ func (p ProjectConfigResolver[R]) configPaths() []string {
return []string{}
}

func (p ProjectConfigResolver[R]) loadWritableConfig(ctx context.Context) (pc.Config, error) {
configPaths := p.configPaths()
if len(configPaths) == 0 {
return pc.Config{}, nil
}
target := configPaths[len(configPaths)-1]
log.FromContext(ctx).Tracef("Loading config from %s", target)
return pc.Load(target)
}

func (p ProjectConfigResolver[R]) loadConfig(ctx context.Context) (pc.Config, error) {
logger := log.FromContext(ctx)
configPaths := p.configPaths()
logger.Debugf("Loading config from %s", strings.Join(configPaths, " "))
logger.Tracef("Loading config from %s", strings.Join(configPaths, " "))
config, err := pc.Merge(configPaths...)
if err != nil {
return pc.Config{}, err
}
return config, nil
}

func (p ProjectConfigResolver[R]) getMapping(ctx context.Context, module optional.Option[string]) (map[string]*pc.URL, error) {
config, err := p.loadConfig(ctx)
if err != nil {
return nil, err
}

func (p ProjectConfigResolver[R]) getMapping(config pc.Config, module optional.Option[string]) (map[string]*pc.URL, error) {
var k R
get := func(dest pc.ConfigAndSecrets) map[string]*pc.URL {
switch any(k).(type) {
Expand Down Expand Up @@ -145,12 +162,7 @@ func (p ProjectConfigResolver[R]) getMapping(ctx context.Context, module optiona
return mapping, nil
}

func (p ProjectConfigResolver[R]) setMapping(ctx context.Context, module optional.Option[string], mapping map[string]*pc.URL) error {
config, err := p.loadConfig(ctx)
if err != nil {
return err
}

func (p ProjectConfigResolver[R]) setMapping(config pc.Config, module optional.Option[string], mapping map[string]*pc.URL) error {
var k R
set := func(dest *pc.ConfigAndSecrets, mapping map[string]*pc.URL) {
switch any(k).(type) {
Expand Down

0 comments on commit f989eda

Please sign in to comment.