Skip to content

Commit 462d4aa

Browse files
Merge pull request #19 from appuio/rename-to-spell
Rename "steps" and "step files" to "spells" and "spellbooks"
2 parents b7d3975 + 1dc151a commit 462d4aa

9 files changed

Lines changed: 123 additions & 123 deletions

File tree

cmd/render.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/appuio/gandalf/pkg/executor"
1313
"github.com/appuio/gandalf/pkg/renderer"
14-
"github.com/appuio/gandalf/pkg/steps"
14+
"github.com/appuio/gandalf/pkg/spells"
1515
"github.com/appuio/gandalf/pkg/workflow"
1616
"github.com/spf13/cobra"
1717
"sigs.k8s.io/yaml"
@@ -63,34 +63,34 @@ func (ro *renderOptions) Run(cmd *cobra.Command, args []string) error {
6363
return fmt.Errorf("failed to unmarshal workflow: %w", err)
6464
}
6565

66-
collectedSteps := []steps.Step{}
67-
for _, stepFilePath := range args[1:] {
68-
matches, err := filepath.Glob(stepFilePath)
66+
collectedSpells := []spells.Spell{}
67+
for _, spellbookPath := range args[1:] {
68+
matches, err := filepath.Glob(spellbookPath)
6969
if err != nil {
70-
return fmt.Errorf("failed to find step file %s: %w", stepFilePath, err)
70+
return fmt.Errorf("failed to find spellbook file %s: %w", spellbookPath, err)
7171
}
72-
for _, stepFile := range matches {
73-
rawStep, err := os.ReadFile(stepFile)
72+
for _, spellbook := range matches {
73+
rawStep, err := os.ReadFile(spellbook)
7474
if err != nil {
75-
return fmt.Errorf("failed to read step file %s: %w", stepFile, err)
75+
return fmt.Errorf("failed to read spellbook file %s: %w", spellbook, err)
7676
}
7777

7878
jsonBytes, err := yaml.YAMLToJSON(rawStep)
7979
if err != nil {
80-
return fmt.Errorf("failed to convert step file %s from YAML to JSON: %w", stepFile, err)
80+
return fmt.Errorf("failed to convert spellbook file %s from YAML to JSON: %w", spellbook, err)
8181
}
8282

83-
parsedFile := &steps.StepsFile{}
83+
parsedFile := &spells.Spellbook{}
8484
if err := json.Unmarshal(jsonBytes, parsedFile); err != nil {
85-
return fmt.Errorf("failed to unmarshal step file %s: %w", stepFile, err)
85+
return fmt.Errorf("failed to unmarshal spellbook file %s: %w", spellbook, err)
8686
}
87-
collectedSteps = append(collectedSteps, parsedFile.Steps...)
87+
collectedSpells = append(collectedSpells, parsedFile.Spells...)
8888
}
8989
}
9090

9191
matcher := &executor.Matcher{
92-
Workflow: wf,
93-
Steps: collectedSteps,
92+
Workflow: wf,
93+
AvailableSpells: collectedSpells,
9494
}
9595

9696
if err := matcher.Prepare(); err != nil {

cmd/run.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"strings"
99

1010
"github.com/appuio/gandalf/pkg/executor"
11+
"github.com/appuio/gandalf/pkg/spells"
1112
"github.com/appuio/gandalf/pkg/state"
12-
"github.com/appuio/gandalf/pkg/steps"
1313
"github.com/appuio/gandalf/pkg/workflow"
1414
"github.com/appuio/gandalf/ui"
1515
"github.com/spf13/cobra"
@@ -58,37 +58,37 @@ func (ro *runOptions) Run(cmd *cobra.Command, args []string) error {
5858
return fmt.Errorf("failed to unmarshal workflow: %w", err)
5959
}
6060

61-
collectedSteps := []steps.Step{}
62-
for _, stepFilePath := range args[1:] {
63-
matches, err := filepath.Glob(stepFilePath)
61+
collectedSpells := []spells.Spell{}
62+
for _, spellbookPath := range args[1:] {
63+
matches, err := filepath.Glob(spellbookPath)
6464
if err != nil {
65-
return fmt.Errorf("failed to find step file %s: %w", stepFilePath, err)
65+
return fmt.Errorf("failed to find spellbook file %s: %w", spellbookPath, err)
6666
}
67-
for _, stepFile := range matches {
68-
stepDir := filepath.Dir(stepFile)
69-
rawStep, err := os.ReadFile(stepFile)
67+
for _, spellbook := range matches {
68+
stepDir := filepath.Dir(spellbook)
69+
rawStep, err := os.ReadFile(spellbook)
7070
if err != nil {
71-
return fmt.Errorf("failed to read step file %s: %w", stepFile, err)
71+
return fmt.Errorf("failed to read spellbook file %s: %w", spellbook, err)
7272
}
7373

7474
jsonBytes, err := yaml.YAMLToJSON(rawStep)
7575
if err != nil {
76-
return fmt.Errorf("failed to convert step file %s from YAML to JSON: %w", stepFile, err)
76+
return fmt.Errorf("failed to convert spellbook file %s from YAML to JSON: %w", spellbook, err)
7777
}
7878

79-
parsedFile := &steps.StepsFile{}
79+
parsedFile := &spells.Spellbook{}
8080
if err := json.Unmarshal(jsonBytes, parsedFile); err != nil {
81-
return fmt.Errorf("failed to unmarshal step file %s: %w", stepFile, err)
81+
return fmt.Errorf("failed to unmarshal spellbook file %s: %w", spellbook, err)
8282
}
83-
for i := range parsedFile.Steps {
84-
parsedFile.Steps[i].StepFileDir = stepDir
83+
for i := range parsedFile.Spells {
84+
parsedFile.Spells[i].SpellbookDir = stepDir
8585
}
86-
collectedSteps = append(collectedSteps, parsedFile.Steps...)
86+
collectedSpells = append(collectedSpells, parsedFile.Spells...)
8787
}
8888
}
8989
matcher := &executor.Matcher{
90-
Workflow: wf,
91-
Steps: collectedSteps,
90+
Workflow: wf,
91+
AvailableSpells: collectedSpells,
9292
}
9393

9494
stateManager, err := state.NewStateManager(ro.StateFile, matcher)

pkg/executor/executor.go

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1919
type Step struct {
2020
Match string
21-
MatchedStep steps.Step
21+
Spell spells.Spell
2222
NamedMatches map[string]string
2323
}
2424

2525
type 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

3434
func (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.
6565
func (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

218218
func (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

234234
func (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-")

pkg/renderer/renderer.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func (r *Renderer) Render() error {
3838

3939
for i, step := range steps {
4040
r.write(r.Formatter.AddSectionID(r.Formatter.H2(step.Match), fmt.Sprintf("step-%d", i+1)))
41-
r.write(r.Formatter.Text(step.MatchedStep.Description))
41+
r.write(r.Formatter.Text(step.Spell.Description))
4242

43-
if len(step.MatchedStep.Inputs) > 0 {
43+
if len(step.Spell.Inputs) > 0 {
4444
r.write(r.Formatter.H3("Inputs"))
45-
for _, input := range step.MatchedStep.Inputs {
45+
for _, input := range step.Spell.Inputs {
4646
t := r.Formatter.InlineCode(input.Name)
4747
if input.Description != "" {
4848
t += ": " + r.Formatter.Text(input.Description)
@@ -52,9 +52,9 @@ func (r *Renderer) Render() error {
5252
r.write("\n")
5353
}
5454

55-
if len(step.MatchedStep.Outputs) > 0 {
55+
if len(step.Spell.Outputs) > 0 {
5656
r.write(r.Formatter.H3("Outputs"))
57-
for _, output := range step.MatchedStep.Outputs {
57+
for _, output := range step.Spell.Outputs {
5858
t := r.Formatter.InlineCode(output.Name)
5959
if output.Description != "" {
6060
t += ": " + r.Formatter.Text(output.Description)
@@ -64,16 +64,16 @@ func (r *Renderer) Render() error {
6464
r.write("\n")
6565
}
6666

67-
if step.MatchedStep.Run != "" {
67+
if step.Spell.Run != "" {
6868
script := new(strings.Builder)
6969

7070
script.WriteString("OUTPUT=$(mktemp)\n\n")
71-
for _, output := range step.MatchedStep.Inputs {
71+
for _, output := range step.Spell.Inputs {
7272
script.WriteString(fmt.Sprintf("# export INPUT_%s=\n", output.Name))
7373
}
7474
script.WriteString("\n")
7575

76-
script.WriteString(step.MatchedStep.Run)
76+
script.WriteString(step.Spell.Run)
7777
script.WriteString("\n\n")
7878

7979
script.WriteString("# echo \"# Outputs\"\n")

0 commit comments

Comments
 (0)