Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func LoadSeclang(input string) types.ConfigurationList {

// PrintSeclang writes seclang directives to files specified in directive list ids.
func PrintSeclang(configList types.ConfigurationList, dir string) error {
configList.PhaseDefaultsToSeclang()

unfDirs := types.FromCRSLangToUnformattedDirectives(configList)

for _, dirList := range unfDirs.DirectiveList {
Expand All @@ -126,6 +128,13 @@ func ToCRSLang(configList types.ConfigurationList) *types.ConfigurationList {
configListWithConditions := types.ToDirectiveWithConditions(configList)

configListWithConditions.ExtractDefaultValues()

err := configListWithConditions.ExtractPhaseDefaults()

if err != nil {
panic(err)
}

return configListWithConditions
}

Expand Down
72 changes: 70 additions & 2 deletions types/configuration.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package types

import (
"fmt"
"slices"
)

type PhaseDefaults struct {
Comment string `yaml:"comment,omitempty"`
Transformations Transformations `yaml:",inline"`
Actions *SeclangActions `yaml:"actions"`
}

type DefaultConfigs struct {
Version string `yaml:"version,omitempty"`
Tags []string `yaml:"tags,omitempty"`
Version string `yaml:"version,omitempty"`
Tags []string `yaml:"tags,omitempty"`
Phases map[string]PhaseDefaults `yaml:"phases,omitempty"`
}

type ConfigurationList struct {
Expand Down Expand Up @@ -93,3 +101,63 @@ func (c *ConfigurationList) ExtractDefaultValues() {
c.Global.Version = version
c.Global.Tags = tags
}

// ExtractPhaseDefaults extract default actions from the directive list and add it to the global config
func (c *ConfigurationList) ExtractPhaseDefaults() error {
defaultActions := map[string]PhaseDefaults{}

for _, dirList := range c.DirectiveList {
for _, directive := range dirList.Directives {
if directive.GetKind() == DefaultActionKind {
da, ok := directive.(DefaultAction)
if !ok {
return fmt.Errorf("Error: casting directive to DefaultAction")
}
pd := PhaseDefaults{
Comment: da.Metadata.Comment,
Transformations: da.Transformations,
Actions: CopyActions(*da.Actions),
}
_, ok = defaultActions[da.Metadata.Phase]
if ok {
return fmt.Errorf("Error: duplicate default actions for phase %s", da.Metadata.Phase)
}
defaultActions[da.Metadata.Phase] = pd
}
}
}

for i := range c.DirectiveList {
c.DirectiveList[i].Directives = slices.DeleteFunc(c.DirectiveList[i].Directives, func(d SeclangDirective) bool {
return d.GetKind() == DefaultActionKind
})
}

c.Global.Phases = defaultActions
return nil
}

// ExtractPhaseDefaults extract default actions from the directive list and add it to the global config
func (c *ConfigurationList) PhaseDefaultsToSeclang() error {
if len(c.Global.Phases) > 0 {
seclangDirs := []SeclangDirective{}
for p, v := range c.Global.Phases {
da := NewDefaultAction()
da.Metadata.SetComment(v.Comment)
da.Metadata.SetPhase(p)
da.Actions = CopyActions(*v.Actions)
seclangDirs = append(seclangDirs, *da)
}

if len(c.DirectiveList) != 0 {
c.DirectiveList[0].Directives = append(seclangDirs, c.DirectiveList[0].Directives...)
} else {
dirList := DirectiveList{}
dirList.Id = "crs-setup"
dirList.Directives = seclangDirs
c.DirectiveList = append(c.DirectiveList, dirList)
}
}

return nil
}