From 1f8b8ecca14e48e3e432b0cb174736313a3fec16 Mon Sep 17 00:00:00 2001 From: Srevin Saju Date: Tue, 14 Nov 2023 22:58:41 +0300 Subject: [PATCH] fix: pass arguments to child modules --- examples/nested-filter/child/togomak.hcl | 16 +++++++++ examples/nested-filter/togomak.hcl | 7 ++++ internal/ci/conductor.go | 3 ++ internal/ci/pipeline_run.go | 8 +---- internal/ci/run.go | 43 ++++++++++++++++-------- internal/ci/run_test.go | 21 +++++++----- internal/ci/stage_run.go | 3 -- internal/rules/model.go | 6 ++-- 8 files changed, 71 insertions(+), 36 deletions(-) create mode 100644 examples/nested-filter/child/togomak.hcl create mode 100644 examples/nested-filter/togomak.hcl diff --git a/examples/nested-filter/child/togomak.hcl b/examples/nested-filter/child/togomak.hcl new file mode 100644 index 0000000..048c049 --- /dev/null +++ b/examples/nested-filter/child/togomak.hcl @@ -0,0 +1,16 @@ +togomak { + version = 2 +} + +stage "limited" { + script = "echo limited" + + lifecycle { + phase = ["build_phase"] + } +} + +stage "example" { + name = "example" + script = "echo hello world" +} diff --git a/examples/nested-filter/togomak.hcl b/examples/nested-filter/togomak.hcl new file mode 100644 index 0000000..7cfac50 --- /dev/null +++ b/examples/nested-filter/togomak.hcl @@ -0,0 +1,7 @@ +togomak { + version = 2 +} + +module "example" { + source = "./child" +} diff --git a/internal/ci/conductor.go b/internal/ci/conductor.go index 6aefbc7..68fe667 100644 --- a/internal/ci/conductor.go +++ b/internal/ci/conductor.go @@ -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" @@ -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 diff --git a/internal/ci/pipeline_run.go b/internal/ci/pipeline_run.go index 57e9269..b1d7e16 100644 --- a/internal/ci/pipeline_run.go +++ b/internal/ci/pipeline_run.go @@ -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 @@ -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 diff --git a/internal/ci/run.go b/internal/ci/run.go index b9a1cc6..6f0c8fe 100644 --- a/internal/ci/run.go +++ b/internal/ci/run.go @@ -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" ) @@ -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) @@ -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 @@ -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 diff --git a/internal/ci/run_test.go b/internal/ci/run_test.go index 1f8c5f9..8026150 100644 --- a/internal/ci/run_test.go +++ b/internal/ci/run_test.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -210,7 +212,8 @@ 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 @@ -218,7 +221,7 @@ func TestCanRun(t *testing.T) { 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 diff --git a/internal/ci/stage_run.go b/internal/ci/stage_run.go index 5b0bcf9..2c53759 100644 --- a/internal/ci/stage_run.go +++ b/internal/ci/stage_run.go @@ -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(), diff --git a/internal/rules/model.go b/internal/rules/model.go index 3afb328..58e0c6c 100644 --- a/internal/rules/model.go +++ b/internal/rules/model.go @@ -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{