-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple_executor.go
132 lines (109 loc) · 2.98 KB
/
simple_executor.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package core
import (
"context"
"errors"
"fmt"
)
type LinksSpecFn func(e Executor) Links
type RelatedSpecFn func() map[string]Executor
type ExecutorSpecFn func(executor Executor, context context.Context, parameters Parameters, configs Configs) (result Result, err error)
type ExecutorSpec struct {
DescriptorSpec
ParametersSchema *Schema
ConfigsSchema *Schema
ResultSchema *Schema
Links LinksSpecFn
Related RelatedSpecFn
PositionalArgs []string
HiddenFlags []string
Execute ExecutorSpecFn
}
var errNil = errors.New("cannot be nil")
func (s *ExecutorSpec) Validate() (err error) {
err = s.DescriptorSpec.Validate()
if err != nil {
return
}
if s.ParametersSchema == nil {
return &ChainedError{Name: "ParametersSchema", Err: errNil}
} else if s.ParametersSchema.Type != "object" {
return &ChainedError{Name: "ParametersSchema", Err: fmt.Errorf("want object, got %q", s.ParametersSchema.Type)}
}
if s.ConfigsSchema == nil {
return &ChainedError{Name: "ConfigsSchema", Err: errNil}
} else if s.ConfigsSchema.Type != "object" {
return &ChainedError{Name: "ConfigsSchema", Err: fmt.Errorf("want object, got %q", s.ConfigsSchema.Type)}
}
if s.ResultSchema == nil {
return &ChainedError{Name: "ResultSchema", Err: errNil}
}
if s.Execute == nil {
return &ChainedError{Name: "Execute", Err: errNil}
}
return nil
}
type SimpleExecutor struct {
SimpleDescriptor
parametersSchema *Schema
configsSchema *Schema
resultSchema *Schema
links LinksSpecFn
related RelatedSpecFn
positionalArgs []string
hiddenFlags []string
execute ExecutorSpecFn
}
func NewSimpleExecutor(spec ExecutorSpec) *SimpleExecutor {
err := spec.Validate()
if err != nil {
logger().Fatalw("invalid spec", "spec", spec, "err", err)
}
return &SimpleExecutor{
SimpleDescriptor{spec.DescriptorSpec},
spec.ParametersSchema,
spec.ConfigsSchema,
spec.ResultSchema,
spec.Links,
spec.Related,
spec.PositionalArgs,
spec.HiddenFlags,
spec.Execute,
}
}
func (e *SimpleExecutor) ParametersSchema() *Schema {
return e.parametersSchema
}
func (e *SimpleExecutor) ConfigsSchema() *Schema {
return e.configsSchema
}
func (e *SimpleExecutor) ResultSchema() *Schema {
return e.resultSchema
}
func (e *SimpleExecutor) Links() Links {
if e.links == nil {
return nil
}
return e.links(e)
}
func (e *SimpleExecutor) Related() map[string]Executor {
if e.related == nil {
return nil
}
return e.related()
}
func (e *SimpleExecutor) PositionalArgs() []string {
return e.positionalArgs
}
func (e *SimpleExecutor) HiddenFlags() []string {
if e.hiddenFlags == nil {
return []string{}
}
return e.hiddenFlags
}
func (*SimpleExecutor) EmptyResult() Result {
return NewSimpleResult(ResultSource{}, nil, nil)
}
func (e *SimpleExecutor) Execute(context context.Context, parameters Parameters, configs Configs) (result Result, err error) {
return e.execute(e, context, parameters, configs)
}
var _ Executor = (*SimpleExecutor)(nil)