github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/config/mutators.go (about)

     1  // Copyright (c) 2018-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package config
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // Mutator is a function that can mutate the configuration
    14  type Mutator interface {
    15  	Mutate(*Config, *logrus.Entry)
    16  }
    17  
    18  var mutators = []Mutator{}
    19  var mutatorNames = []string{}
    20  var mu = &sync.Mutex{}
    21  
    22  // RegisterMutator registers a new configuration mutator
    23  func RegisterMutator(name string, m Mutator) {
    24  	mu.Lock()
    25  	defer mu.Unlock()
    26  
    27  	mutators = append(mutators, m)
    28  	mutatorNames = append(mutatorNames, name)
    29  }
    30  
    31  // MutatorNames are the names of known configuration mutators
    32  func MutatorNames() []string {
    33  	return mutatorNames
    34  }
    35  
    36  // Mutate calls all registered mutators on the given configuration
    37  func Mutate(c *Config, log *logrus.Entry) {
    38  	mu.Lock()
    39  	defer mu.Unlock()
    40  
    41  	for _, mutator := range mutators {
    42  		mutator.Mutate(c, log)
    43  	}
    44  }