Skip to content

Commit

Permalink
modify configuration parsing to allow inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofferlind committed Nov 13, 2022
1 parent cde4e8b commit 4dcbb34
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
17 changes: 13 additions & 4 deletions internal/configuration/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ type action struct {
Commands []string
}

func initializeActions(configurationActions []configurationAction) map[string]action {
func initializeActions(baseActions, configurationActions map[string]configurationAction) map[string]action {
actions := make(map[string]action)

for _, configurationAction := range configurationActions {
actions[configurationAction.Name] = action{
Commands: configurationAction.Commands,
for actionName, baseAction := range baseActions {
actions[actionName] = action(baseAction)
}

for actionName, configurationAction := range configurationActions {
actions[actionName] = action(configurationAction)
}

for actionName, action := range actions {
// remove empty actions (solution for allowing actions to be skipped)
if len(action.Commands) == 0 {
delete(actions, actionName)
}
}

Expand Down
7 changes: 2 additions & 5 deletions internal/configuration/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,22 @@ func initializeComponent(componentPath string, rootConfiguration rootConfigurati

component.Inputs = initializeInputs(configuration.Inputs, componentPath, rootPath)
component.Outputs = initializeOutputs(configuration.Outputs)
component.Actions = initializeActions(configuration.Actions)
component.Actions = initializeActions(rootConfiguration.DefaultConfiguration.Actions, configuration.Actions)
component.InputsHash = calculateHash(component.Inputs)
component.ChangeStatus = Unknown

return component
}

func initializeComponentConfiguration(componentConfigurationPath string, rootConfiguration rootConfiguration) componentConfiguration {
componentConfiguration := parseComponentConfiguration(componentConfigurationPath, rootConfiguration.DefaultConfiguration)
componentConfiguration := parseComponentConfiguration(componentConfigurationPath)

if componentConfiguration.Inputs == nil {
componentConfiguration.Inputs = rootConfiguration.DefaultConfiguration.Inputs
}
if componentConfiguration.Outputs == nil {
componentConfiguration.Outputs = rootConfiguration.DefaultConfiguration.Outputs
}
if componentConfiguration.Actions == nil {
componentConfiguration.Actions = rootConfiguration.DefaultConfiguration.Actions
}

return componentConfiguration
}
Expand Down
13 changes: 6 additions & 7 deletions internal/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ type configurationOutput struct {
}

type configurationAction struct {
Name string `yaml:"name"`
Commands []string `yaml:"commands"`
}

type componentConfiguration struct {
Name string `yaml:"name"`
Inputs []configurationInput `yaml:"inputs,omitempty"`
Outputs []configurationOutput `yaml:"outputs"`
Actions []configurationAction `yaml:"actions"`
Name string `yaml:"name"`
Inputs []configurationInput `yaml:"inputs,omitempty"`
Outputs []configurationOutput `yaml:"outputs"`
Actions map[string]configurationAction `yaml:"actions"`
}

func parseRootConfiguration(path string) rootConfiguration {
Expand All @@ -51,12 +50,12 @@ func parseRootConfiguration(path string) rootConfiguration {
return rootConfiguration
}

func parseComponentConfiguration(path string, defaultConfiguration componentConfiguration) componentConfiguration {
func parseComponentConfiguration(path string) componentConfiguration {
fileContent, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Error reading content of root config file: %v", err)
}
configuration := defaultConfiguration
configuration := componentConfiguration{}
err = yaml.Unmarshal(fileContent, &configuration)
if err != nil {
log.Fatalf("Error parsing root config: %v", err)
Expand Down

0 comments on commit 4dcbb34

Please sign in to comment.