/
step.go
60 lines (55 loc) · 1.31 KB
/
step.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package internal
import (
"context"
"errors"
"github.com/rs/zerolog/log"
)
// Step is one step in a sequence
type Step struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Config map[string]any `yaml:"config"`
scope *Scope
Filters []Filter `yaml:"filters"`
Hide bool `yaml:"hide"`
}
// GetImplementation will return the proper IOperation implementation for the current step
func (s *Step) GetImplementation() (IOperation, error) {
switch s.Type {
case OpNixShell:
return NewNixShellIOp(s.Config, s.scope)
case OpTemplate:
return NewTemplateOp(s.Config, s.scope)
case OpWait:
return NewWaitOp(s.Config)
case OpSql:
return NewSqlOp(s.Config, s.scope)
case OpMongo:
return NewMongoOp(s.Config, s.scope)
case OpFile:
return NewFileOp(s.Config, s.scope)
default:
return nil, errors.New("could not find a proper implementation for " + s.Type)
}
}
// Run will run the step
func (s *Step) Run(ctx context.Context) (any, error) {
op, err := s.GetImplementation()
if err != nil {
return nil, err
}
err = op.Run(ctx)
if err != nil {
return nil, err
}
res := op.GetResult()
for _, f := range s.Filters {
rx, err := f.Run(ctx, res)
if err == nil {
res = rx
} else {
log.Err(err).Str("filter", f.Type).Msg("could not run filter")
}
}
return res, nil
}