@@ -10,46 +10,46 @@ import (
1010 "path/filepath"
1111 "slices"
1212
13+ "github.com/appuio/gandalf/pkg/spells"
1314 "github.com/appuio/gandalf/pkg/state"
14- "github.com/appuio/gandalf/pkg/steps"
1515 "github.com/appuio/gandalf/pkg/workflow"
1616 "go.uber.org/multierr"
1717)
1818
1919type Step struct {
2020 Match string
21- MatchedStep steps. Step
21+ Spell spells. Spell
2222 NamedMatches map [string ]string
2323}
2424
2525type Matcher struct {
26- Workflow workflow.Workflow
27- Steps []steps. Step
26+ Workflow workflow.Workflow
27+ AvailableSpells []spells. Spell
2828
29- preparedMatches map [string ]Step
29+ preparedSteps map [string ]Step
3030
31- variableTypes map [string ]steps .VariableType
31+ variableTypes map [string ]spells .VariableType
3232}
3333
3434func (m * Matcher ) Prepare () error {
3535 if len (m .Workflow .Steps ) == 0 {
3636 return fmt .Errorf ("workflow has no steps" )
3737 }
3838 // Match workflow steps to available steps.
39- m .preparedMatches = make (map [string ]Step )
39+ m .preparedSteps = make (map [string ]Step )
4040 var errors []error
41- for _ , wfStep := range m .Workflow .Steps {
42- err := m .matchStep ( wfStep )
41+ for _ , step := range m .Workflow .Steps {
42+ err := m .matchSpell ( step )
4343 if err != nil {
4444 errors = append (errors , err )
4545 }
4646 }
4747 if err := multierr .Combine (errors ... ); err != nil {
48- return fmt .Errorf ("failed to match workflow steps: %w" , err )
48+ return fmt .Errorf ("failed to match workflow steps to spells : %w" , err )
4949 }
50- for _ , wfStep := range m .Workflow .Steps {
51- step := m .preparedMatches [ wfStep ]
52- err := m .addVariables (step .MatchedStep , step .Match )
50+ for _ , step := range m .Workflow .Steps {
51+ step := m .preparedSteps [ step ]
52+ err := m .addVariables (step .Spell , step .Match )
5353 if err != nil {
5454 errors = append (errors , err )
5555 }
@@ -63,12 +63,12 @@ func (m *Matcher) Prepare() error {
6363// PreparedSteps returns the list of steps matched to the workflow in order.
6464// Returns an error if the matcher has not been prepared.
6565func (m * Matcher ) PreparedSteps () ([]Step , error ) {
66- if m .preparedMatches == nil {
66+ if m .preparedSteps == nil {
6767 return nil , fmt .Errorf ("matcher not prepared" )
6868 }
6969 prepared := make ([]Step , len (m .Workflow .Steps ))
70- for i , wfStep := range m .Workflow .Steps {
71- prepared [i ] = m .preparedMatches [ wfStep ]
70+ for i , step := range m .Workflow .Steps {
71+ prepared [i ] = m .preparedSteps [ step ]
7272 }
7373
7474 return prepared , nil
@@ -87,24 +87,24 @@ func (m *Matcher) IsSensitive(variable string) bool {
8787 return false
8888}
8989
90- func (m * Matcher ) addVariables (step steps. Step , matchedName string ) error {
90+ func (m * Matcher ) addVariables (spell spells. Spell , matchedName string ) error {
9191 if m .variableTypes == nil {
92- m .variableTypes = make (map [string ]steps .VariableType )
92+ m .variableTypes = make (map [string ]spells .VariableType )
9393 }
94- for _ , input := range step .Inputs {
94+ for _ , input := range spell .Inputs {
9595 if t , ok := m .variableTypes [input .Name ]; ok {
9696 if t != input .Type && ! input .Type .IsRegular () {
97- return fmt .Errorf ("Variable %s of type %s is re-defined in Step `%s` as type %s" , input .Name , t .String (), matchedName , input .Type .String ())
97+ return fmt .Errorf ("Variable %s of type %s is re-defined in spell `%s` as type %s" , input .Name , t .String (), matchedName , input .Type .String ())
9898 }
9999 }
100100 if ! input .Type .IsRegular () {
101101 m .variableTypes [input .Name ] = input .Type
102102 }
103103 }
104- for _ , output := range step .Outputs {
104+ for _ , output := range spell .Outputs {
105105 if t , ok := m .variableTypes [output .Name ]; ok {
106106 if t != output .Type && ! output .Type .IsRegular () {
107- return fmt .Errorf ("Variable %s of type %s is re-defined in Step `%s` as type %s" , output .Name , t .String (), matchedName , output .Type .String ())
107+ return fmt .Errorf ("Variable %s of type %s is re-defined in spell `%s` as type %s" , output .Name , t .String (), matchedName , output .Type .String ())
108108 }
109109 }
110110 if ! output .Type .IsRegular () {
@@ -114,36 +114,36 @@ func (m *Matcher) addVariables(step steps.Step, matchedName string) error {
114114 return nil
115115}
116116
117- func (m * Matcher ) matchStep ( wfStep string ) error {
118- var matchedSteps []Step
119- for _ , step := range m .Steps {
120- if match := step .Match .FindStringSubmatch (wfStep ); len (match ) > 0 {
117+ func (m * Matcher ) matchSpell ( step string ) error {
118+ var steps []Step
119+ for _ , spell := range m .AvailableSpells {
120+ if match := spell .Match .FindStringSubmatch (step ); len (match ) > 0 {
121121 namedMatches := make (map [string ]string )
122- for i , name := range step .Match .SubexpNames () {
122+ for i , name := range spell .Match .SubexpNames () {
123123 if i != 0 {
124124 namedMatches [name ] = match [i ]
125125 }
126126 }
127- matchedSteps = append (matchedSteps , Step {
128- Match : wfStep ,
129- MatchedStep : step ,
127+ steps = append (steps , Step {
128+ Match : step ,
129+ Spell : spell ,
130130 NamedMatches : namedMatches ,
131131 })
132132 }
133133 }
134134
135- switch len (matchedSteps ) {
135+ switch len (steps ) {
136136 case 0 :
137- return fmt .Errorf ("unmatched step %q" , wfStep )
137+ return fmt .Errorf ("unmatched step %q" , step )
138138 case 1 :
139139 // ok
140140 default :
141- return fmt .Errorf ("multiple matching steps for %q" , wfStep )
141+ return fmt .Errorf ("multiple matching spells for step %q" , step )
142142 }
143143
144- matchedStep := matchedSteps [0 ]
144+ preparedStep := steps [0 ]
145145
146- m .preparedMatches [ wfStep ] = matchedStep
146+ m .preparedSteps [ step ] = preparedStep
147147
148148 return nil
149149}
@@ -170,8 +170,8 @@ func (e *Executor) Prepare() error {
170170 // Read initial inputs from environment.
171171 // Allows users to predefine inputs.
172172 // TODO separate from outputs
173- for _ , step := range e .Steps {
174- for _ , input := range step .Inputs {
173+ for _ , spell := range e .AvailableSpells {
174+ for _ , input := range spell .Inputs {
175175 if os .Getenv ("INPUT_" + input .Name ) != "" {
176176 err := e .StateManager .SetOutputFromEnv (input .Name , os .Getenv ("INPUT_" + input .Name ))
177177 if err != nil {
@@ -205,14 +205,14 @@ func (e *Executor) Prepare() error {
205205 return nil
206206}
207207
208- func (e * Executor ) CurrentStep () (i int , matchedStep Step , err error ) {
209- currentWFStep := e .Workflow .Steps [e .currentStepIndex ]
210- matchedStep , ok := e .preparedMatches [ currentWFStep ]
208+ func (e * Executor ) CurrentStep () (i int , step Step , err error ) {
209+ currentStep := e .Workflow .Steps [e .currentStepIndex ]
210+ step , ok := e .preparedSteps [ currentStep ]
211211 if ! ok {
212- return 0 , Step {}, fmt .Errorf ("step %q not prepared" , currentWFStep )
212+ return 0 , Step {}, fmt .Errorf ("step %q not prepared" , currentStep )
213213 }
214214
215- return e .currentStepIndex , matchedStep , nil
215+ return e .currentStepIndex , step , nil
216216}
217217
218218func (e * Executor ) NextStep () (i int , matchedStep Step , err error ) {
@@ -232,12 +232,12 @@ func (e *Executor) NextStep() (i int, matchedStep Step, err error) {
232232}
233233
234234func (e * Executor ) CurrentStepCmd (ctx context.Context ) (* Cmd , error ) {
235- _ , matchedStep , err := e .CurrentStep ()
235+ _ , step , err := e .CurrentStep ()
236236 if err != nil {
237237 return nil , err
238238 }
239239
240- script := matchedStep . MatchedStep .Run
240+ script := step . Spell .Run
241241 if script == "" {
242242 script = ":"
243243 }
@@ -252,11 +252,11 @@ func (e *Executor) CurrentStepCmd(ctx context.Context) (*Cmd, error) {
252252 cmd := exec .CommandContext (ctx , "bash" , "-c" , script )
253253 cmd .Env = os .Environ ()
254254 outputs := e .StateManager .Outputs ()
255- cmd .Env = append (cmd .Env , fmt .Sprintf ("GANDALF_STEPFILE_DIR =%s" , matchedStep . MatchedStep . StepFileDir ))
256- for _ , input := range matchedStep . MatchedStep .Inputs {
255+ cmd .Env = append (cmd .Env , fmt .Sprintf ("GANDALF_SPELLBOOK_DIR =%s" , step . Spell . SpellbookDir ))
256+ for _ , input := range step . Spell .Inputs {
257257 cmd .Env = append (cmd .Env , fmt .Sprintf ("INPUT_%s=%s" , input .Name , outputs [input .Name ].Value ))
258258 }
259- for k , v := range matchedStep .NamedMatches {
259+ for k , v := range step .NamedMatches {
260260 cmd .Env = append (cmd .Env , fmt .Sprintf ("MATCH_%s=%s" , k , v ))
261261 }
262262 outputDir , err := os .MkdirTemp ("." , "outputs-" )
0 commit comments