/
dummyrunner.go
61 lines (46 loc) · 1.44 KB
/
dummyrunner.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
61
package jobs
import (
"bytes"
"context"
"errors"
"text/template"
log "github.com/sirupsen/logrus"
)
type DummyRunner struct {
Template string
}
// NewDummyRunner creates a new dummy runner that doesn't do anything, but requires a
// template and returns that executed template when called
func NewDummyRunner(config map[string]interface{}) (*DummyRunner, error) {
log.Debug("creating new dummy job runner")
var template string
if v, ok := config["template"].(string); ok {
template = v
}
if template == "" {
return nil, errors.New("template cannot be empty")
}
return &DummyRunner{Template: template}, nil
}
// Run executes the DummyRunner, ignoring the parameters
func (r *DummyRunner) Run(ctx context.Context, account string, parameters interface{}) (string, error) {
if account == "" {
return "", errors.New("account is required")
}
log.Debugf("initializing dummy runner %+v in account %s, with parameters %+v", r, account, parameters)
input := struct {
Account string
}{account}
tmpl, err := template.New("dummy").Parse(r.Template)
if err != nil {
return "", NewRunnerError(ErrPreExecFailure, "template parsing failed", err)
}
var out bytes.Buffer
if err := tmpl.Execute(&out, &input); err != nil {
return "", NewRunnerError(ErrPreExecFailure, "template execution failed", err)
}
log.Infof("dummy runner executing for account %s", account)
msg := out.String()
log.Debugf("output of template: %s", msg)
return msg, nil
}