Skip to content

Commit

Permalink
fix: pass arguments to child modules
Browse files Browse the repository at this point in the history
  • Loading branch information
srevinsaju committed Nov 14, 2023
1 parent b932ec3 commit 1f8b8ec
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 36 deletions.
16 changes: 16 additions & 0 deletions examples/nested-filter/child/togomak.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
togomak {
version = 2
}

stage "limited" {
script = "echo limited"

lifecycle {
phase = ["build_phase"]
}
}

stage "example" {
name = "example"
script = "echo hello world"
}
7 changes: 7 additions & 0 deletions examples/nested-filter/togomak.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
togomak {
version = 2
}

module "example" {
source = "./child"
}
3 changes: 3 additions & 0 deletions internal/ci/conductor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/srevinsaju/togomak/v1/internal/conductor"
"github.com/srevinsaju/togomak/v1/internal/logging"
"github.com/srevinsaju/togomak/v1/internal/meta"
"github.com/srevinsaju/togomak/v1/internal/rules"
"github.com/srevinsaju/togomak/v1/internal/x"
"os"
"path/filepath"
Expand Down Expand Up @@ -107,6 +108,8 @@ type Conductor struct {
// Eval has the evaluation context and mutexes associated with Eval Variable maps
eval *Eval

filterList *rules.Operations

parent *Conductor

variables Variables
Expand Down
8 changes: 1 addition & 7 deletions internal/ci/pipeline_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ func (pipe *Pipeline) Run(conductor *Conductor) (*Handler, dg.AbstractDiagnostic
h = h.Update(WithContext(ctx))
conductor.Update(ConductorWithContext(ctx))

// --> validate the pipeline
// TODO: validate the pipeline
// whitelist all stages if unspecified
filterList := cfg.Pipeline.Filtered
filterQuery := cfg.Pipeline.FilterQuery

// --> generate a dependency graph
// we will now generate a dependency graph from the pipeline
// this will be used to generate the pipeline
Expand Down Expand Up @@ -97,7 +91,7 @@ func (pipe *Pipeline) Run(conductor *Conductor) (*Handler, dg.AbstractDiagnostic
break
}

ok, overridden, d := BlockCanRun(runnable, conductor, filterList, filterQuery, runnableId, depGraph, opts...)
ok, overridden, d := BlockCanRun(runnable, conductor, runnableId, depGraph, opts...)
h.Diags.Extend(d)
if d.HasErrors() {
break
Expand Down
43 changes: 29 additions & 14 deletions internal/ci/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/srevinsaju/togomak/v1/internal/blocks"
"github.com/srevinsaju/togomak/v1/internal/rules"
"github.com/srevinsaju/togomak/v1/internal/runnable"
"github.com/zclconf/go-cty/cty"
"time"
)

Expand Down Expand Up @@ -71,8 +72,10 @@ func BlockRunWithRetries(conductor *Conductor, runnableId string, runnable Block
}
}

func BlockCanRun(runnable Block, conductor *Conductor, filterList rules.Operations, filterQuery QueryEngines, runnableId string, depGraph *depgraph.Graph, opts ...runnable.Option) (ok bool, overridden bool, diags hcl.Diagnostics) {
func BlockCanRun(runnable Block, conductor *Conductor, runnableId string, depGraph *depgraph.Graph, opts ...runnable.Option) (ok bool, overridden bool, diags hcl.Diagnostics) {

filterList := conductor.Config.Pipeline.Filtered
filterQuery := conductor.Config.Pipeline.FilterQuery
ok, d := runnable.CanRun(conductor, opts...)
if d.HasErrors() {
diags = diags.Extend(d)
Expand Down Expand Up @@ -109,6 +112,30 @@ func BlockCanRun(runnable Block, conductor *Conductor, filterList rules.Operatio
ok = false
overridden = false

// if the list is empty, we will assume that the runnable is not overridden
// and we will run all module blocks. This is so that the child processoe
var phases []cty.Value
var phasesDefined bool
stage := runnable.(PhasedBlock)
if stage.LifecycleConfig() != nil {
evalContext := conductor.Eval().Context()
conductor.Eval().Mutex().RLock()
phaseHcl, d := stage.LifecycleConfig().Phase.Value(evalContext)
conductor.Eval().Mutex().RUnlock()
if d.HasErrors() {
diags = diags.Extend(d)
return false, false, diags
}
phasesDefined = !phaseHcl.IsNull()
phases = phaseHcl.AsValueSlice()
}

if runnable.Type() == blocks.ModuleBlock && len(phases) == 0 && !phasesDefined {
ok = oldOk
overridden = false
return ok, overridden, diags
}

for _, rule := range filterList {
if rule.RunnableId() == runnableId && rule.Operation() == rules.OperationTypeAdd {
ok = true
Expand All @@ -127,19 +154,7 @@ func BlockCanRun(runnable Block, conductor *Conductor, filterList rules.Operatio
overridden = true
}
if runnable.Type() == blocks.StageBlock || runnable.Type() == blocks.ModuleBlock {
stage := runnable.(PhasedBlock)
if stage.LifecycleConfig() != nil {
evalContext := conductor.Eval().Context()
conductor.Eval().Mutex().RLock()
phaseHcl, d := stage.LifecycleConfig().Phase.Value(evalContext)
conductor.Eval().Mutex().RUnlock()

if d.HasErrors() {
diags = diags.Extend(d)
return false, false, diags
}
phases := phaseHcl.AsValueSlice()

if phasesDefined {
for _, phase := range phases {
if rule.RunnableId() == phase.AsString() {
overridden = false
Expand Down
21 changes: 12 additions & 9 deletions internal/ci/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ func TestCanRun(t *testing.T) {
t.Errorf("error while parsing rules: %s", d.Error())
return
}
conductor.Config.Pipeline.Filtered = filtered

ok, overridden, err := BlockCanRun(&stage1, conductor, filtered, nil, stage1.Identifier(), depGraph)
ok, overridden, err := BlockCanRun(&stage1, conductor, stage1.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand All @@ -140,7 +141,7 @@ func TestCanRun(t *testing.T) {
return
}

ok, overridden, err = BlockCanRun(&stage3, conductor, filtered, nil, stage3.Identifier(), depGraph)
ok, overridden, err = BlockCanRun(&stage3, conductor, stage3.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand All @@ -150,7 +151,7 @@ func TestCanRun(t *testing.T) {
return
}

ok, overridden, err = BlockCanRun(&stage2, conductor, filtered, nil, stage2.Identifier(), depGraph)
ok, overridden, err = BlockCanRun(&stage2, conductor, stage2.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand All @@ -160,7 +161,7 @@ func TestCanRun(t *testing.T) {
return
}

ok, overridden, err = BlockCanRun(&stage2, conductor, nil, nil, stage2.Identifier(), depGraph)
ok, overridden, err = BlockCanRun(&stage2, conductor, stage2.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand All @@ -170,7 +171,7 @@ func TestCanRun(t *testing.T) {
return
}

ok, overridden, err = BlockCanRun(&stage1, conductor, nil, nil, stage1.Identifier(), depGraph)
ok, overridden, err = BlockCanRun(&stage1, conductor, stage1.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand All @@ -185,8 +186,9 @@ func TestCanRun(t *testing.T) {
t.Errorf("error while parsing rules: %s", d.Error())
return
}
conductor.Config.Pipeline.Filtered = filtered

ok, overridden, err = BlockCanRun(&stage1, conductor, filtered, nil, stage1.Identifier(), depGraph)
ok, overridden, err = BlockCanRun(&stage1, conductor, stage1.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand All @@ -195,7 +197,7 @@ func TestCanRun(t *testing.T) {
t.Errorf("%s should be runnable", stage1.Identifier())
return
}
ok, overridden, err = BlockCanRun(&stage2, conductor, filtered, nil, stage2.Identifier(), depGraph)
ok, overridden, err = BlockCanRun(&stage2, conductor, stage2.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand All @@ -210,15 +212,16 @@ func TestCanRun(t *testing.T) {
t.Errorf("error while parsing rules: %s", d.Error())
return
}
ok, overridden, err = BlockCanRun(&stage1, conductor, filtered, nil, stage1.Identifier(), depGraph)
conductor.Config.Pipeline.Filtered = filtered
ok, overridden, err = BlockCanRun(&stage1, conductor, stage1.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
}
if ok {
t.Errorf("%s should not be runnable", stage1.Identifier())
}
ok, overridden, err = BlockCanRun(&stage2, conductor, filtered, nil, stage2.Identifier(), depGraph)
ok, overridden, err = BlockCanRun(&stage2, conductor, stage2.Identifier(), depGraph)
if err != nil {
t.Errorf("error while running BlockCanRun: %s", err.Error())
return
Expand Down
3 changes: 0 additions & 3 deletions internal/ci/stage_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ func (s *Stage) expandMacros(conductor *Conductor, opts ...runnable.Option) (*St
})
}
sourceEvaluated := source.AsString()
if !strings.HasSuffix(sourceEvaluated, ".hcl") {
sourceEvaluated = filepath.Join(sourceEvaluated, meta.ConfigFileName)
}
macro = &Macro{
Source: sourceEvaluated,
Id: uuid.New().String(),
Expand Down
6 changes: 3 additions & 3 deletions internal/rules/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const (
)

var (
operationAddMatcher = regexp.MustCompile(`^\+([a-zA-Z0-9.\-_]+)$`)
operationSubMatcher = regexp.MustCompile(`^\^([a-zA-Z0-9.\-_]+)$`)
operationAddMatcher = regexp.MustCompile(`^\+([a-zA-Z0-9.\-:_]+)$`)
operationSubMatcher = regexp.MustCompile(`^\^([a-zA-Z0-9.\-:_]+)$`)

operationAndMatcher = regexp.MustCompile(`^([a-zA-Z0-9.\-_]+)$`)
operationAndMatcher = regexp.MustCompile(`^([a-zA-Z0-9.\-:_]+)$`)
)

var OperationTypes = []OperationType{
Expand Down

0 comments on commit 1f8b8ec

Please sign in to comment.