|
1 | 1 | package pluginengine |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | 4 | "fmt" |
6 | | - "strings" |
7 | 5 | "time" |
8 | 6 |
|
9 | 7 | "github.com/merico-dev/stream/internal/pkg/configloader" |
10 | 8 | "github.com/merico-dev/stream/internal/pkg/statemanager" |
11 | 9 | "github.com/merico-dev/stream/pkg/util/log" |
12 | 10 | ) |
13 | 11 |
|
14 | | -const REF_PREFIX = "${{" |
15 | | -const REF_SUFFIX = "}}" |
16 | | - |
17 | | -// eg. ${{name.kind.outputs.key}},name setgment number is 0 |
18 | | -const NAME_SEGMENT_NUM = 0 |
19 | | - |
20 | | -// eg. ${{name.kind.outputs.key}},kind setgment number is 1 |
21 | | -const KIND_SEGMENT_NUM = 1 |
22 | | - |
23 | | -// eg. ${{name.kind.outputs.key}},key setgment number is 3 |
24 | | -const REF_SEGMENT_NUM = 3 |
25 | | - |
26 | 12 | // Change is a wrapper with a single Tool and its Action should be execute. |
27 | 13 | type Change struct { |
28 | 14 | Tool *configloader.Tool |
@@ -111,12 +97,19 @@ func execute(smgr statemanager.Manager, changes []*Change) map[string]error { |
111 | 97 | var returnValue map[string]interface{} |
112 | 98 |
|
113 | 99 | log.Debugf("Tool's raw changes are: %s.", c.Tool.Options) |
114 | | - // fill ref inputs |
115 | | - err = fillRefValueWithOutputs(smgr, c.Tool.Options) |
116 | | - if err != nil { |
| 100 | + |
| 101 | + errs := HandleOutputsReferences(smgr, c.Tool.Options) |
| 102 | + if len(errs) != 0 { |
117 | 103 | succeeded = false |
| 104 | + |
| 105 | + for _, e := range errs { |
| 106 | + log.Errorf("Error: %s.", e) |
| 107 | + } |
| 108 | + log.Errorf("The outputs reference in tool %s (%s) can't be resolved. Please double check your config.", c.Tool.Name, c.Tool.Plugin.Kind) |
| 109 | + |
| 110 | + // not executing this change since its input isn't valid |
| 111 | + continue |
118 | 112 | } |
119 | | - log.Debugf("Tool's changes with filled inputs: %s.", c.Tool.Options) |
120 | 113 |
|
121 | 114 | switch c.ActionName { |
122 | 115 | case statemanager.ActionCreate: |
@@ -192,56 +185,3 @@ func handleResult(smgr statemanager.Manager, change *Change) error { |
192 | 185 | log.Successf("Plugin %s(%s) %s done.", change.Tool.Name, change.Tool.Plugin.Kind, change.ActionName) |
193 | 186 | return nil |
194 | 187 | } |
195 | | - |
196 | | -// fillRefValueWithOutputs fill inputs from state |
197 | | -func fillRefValueWithOutputs(smgr statemanager.Manager, options map[string]interface{}) error { |
198 | | - for key, value := range options { |
199 | | - log.Debugf("Key: %s, Value: %s.", key, value) |
200 | | - // judge whether the value is a string |
201 | | - if inst, ok := value.(string); ok { |
202 | | - // judge whether the format is ${{xxx}} |
203 | | - if isValidRefFormat(inst) { |
204 | | - ref := getRefFormatString(inst) |
205 | | - log.Debug("Ref inputs: ", ref) |
206 | | - refParam := strings.Split(ref, ".") |
207 | | - if len(refParam) <= 3 { |
208 | | - return errors.New("incorrect output reference: " + ref) |
209 | | - } |
210 | | - |
211 | | - outputs, err := smgr.GetOutputs(statemanager.GenStateKey(refParam[NAME_SEGMENT_NUM], refParam[KIND_SEGMENT_NUM])) |
212 | | - if err != nil { |
213 | | - return err |
214 | | - } |
215 | | - log.Debug("Ref outputs: ", outputs) |
216 | | - |
217 | | - if outs, ok := outputs.(map[string]interface{}); ok { |
218 | | - log.Debug("Ref outs: ", outs) |
219 | | - log.Debug("Ref param: ", refParam[REF_SEGMENT_NUM]) |
220 | | - if value == nil { |
221 | | - return errors.New("ref input value is null: " + refParam[REF_SEGMENT_NUM]) |
222 | | - } |
223 | | - if options[key], ok = outs[refParam[REF_SEGMENT_NUM]]; !ok { |
224 | | - return fmt.Errorf("can not find %s in dependency outputs", refParam[REF_SEGMENT_NUM]) |
225 | | - } |
226 | | - } |
227 | | - } |
228 | | - } |
229 | | - // judge wheter the format is map[string]interface{}, if true, then recursion |
230 | | - if _, ok := value.(map[string]interface{}); ok { |
231 | | - if err := fillRefValueWithOutputs(smgr, value.(map[string]interface{})); err != nil { |
232 | | - return err |
233 | | - } |
234 | | - } |
235 | | - } |
236 | | - return nil |
237 | | -} |
238 | | - |
239 | | -// isValidRefFormat if the format is ${{abc}} |
240 | | -func isValidRefFormat(ref string) bool { |
241 | | - return strings.HasPrefix(ref, REF_PREFIX) && strings.HasSuffix(ref, REF_SUFFIX) |
242 | | -} |
243 | | - |
244 | | -// getRefFormatString get abc from ${{abc}} or ${{ abc }} |
245 | | -func getRefFormatString(rawFormatString string) string { |
246 | | - return strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(rawFormatString, REF_PREFIX), REF_SUFFIX)) |
247 | | -} |
0 commit comments