when.go

v0.3.0
Doc Versions Source
1
package steps
2
3
import "go.bigb.es/auxilia/legatus"
4
5
// When wraps a step with a precondition. Cond is evaluated in Check: when it is
6
// set and returns false the inner step is skipped (need=false); when it is nil or
7
// returns true, Check delegates to the inner step. Apply always delegates
8
// unchanged — When gates whether the step runs, never what it does.
9
type When struct {
10
	Cond func(*legatus.FlowCtx) bool
11
	Step legatus.Step
12
}
13
14
// Name returns the inner step's name, so logs, the reporter, and change tracking
15
// all key off the wrapped step.
16
func (w When) Name() string { return w.Step.Name() }
17
18
// Check skips (need=false) when Cond is set and false; otherwise it delegates to
19
// the inner step's Check.
20
func (w When) Check(fc *legatus.FlowCtx) (bool, error) {
21
	if w.Cond != nil && !w.Cond(fc) {
22
		return false, nil
23
	}
24
	return w.Step.Check(fc)
25
}
26
27
// Apply delegates to the inner step unchanged.
28
func (w When) Apply(fc *legatus.FlowCtx) (legatus.Outcome, error) {
29
	return w.Step.Apply(fc)
30
}
31
32
// OnChanged runs inner only if any of the watched earlier steps changed during
33
// this flow execution on this host — the restart-on-change pattern. Watch entries
34
// are step names, e.g. File{Path: p}.Name(). With no watch names it runs inner
35
// when any earlier step changed.
36
func OnChanged(inner legatus.Step, watch ...string) When {
37
	return When{
38
		Cond: func(fc *legatus.FlowCtx) bool { return fc.AnyChanged(watch...) },
39
		Step: inner,
40
	}
41
}
42

Source Files