add TestMode to run actions without effect

This commit is contained in:
2023-08-16 01:27:28 +02:00
parent 381cc31d4b
commit 8f6fc72bfb
7 changed files with 56 additions and 20 deletions

View File

@@ -9,6 +9,7 @@ import (
type Action interface {
Execute()
DryExecute()
}
type Stage struct {
@@ -21,7 +22,7 @@ type StagedActions struct {
Stages []Stage
}
func (a StagedActions) Execute() {
func (a StagedActions) executeInternal(f func(Action)) {
for idx, stage := range a.Stages {
if idx < a.StageCount {
continue
@@ -29,7 +30,7 @@ func (a StagedActions) Execute() {
fmt.Printf("Execute Stage %v\n", idx+1)
for actionidx, _ := range stage.Actions {
go stage.Actions[actionidx].Execute()
go f(stage.Actions[actionidx])
}
for range stage.Actions {
@@ -38,6 +39,25 @@ func (a StagedActions) Execute() {
}
}
var TestRun bool
func Fire(a Action) {
if TestRun {
a.DryExecute()
return
}
a.Execute()
}
func (a StagedActions) DryExecute() {
a.executeInternal(func(a Action) { a.DryExecute() })
}
func (a StagedActions) Execute() {
a.executeInternal(func(a Action) { a.Execute() })
}
func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
if config.Type == "Print" {
return NewPrint(config, c)
@@ -47,6 +67,10 @@ func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error)
return NewTimeOut(config, c)
}
if config.Type == "Command" {
return NewCommand(config, c)
}
if config.Type == "Shutdown" {
return NewShutdown(config, c)
}
@@ -91,6 +115,7 @@ func GetDocumenters() []internal.Documenter {
return []internal.Documenter{
Printer{},
TimeOut{},
Command{},
Shutdown{},
}
}