|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + |
| 7 | + api "github.com/bootdotdev/bootdev/client" |
| 8 | +) |
| 9 | + |
| 10 | +func captureNames(step api.CLIStep) []string { |
| 11 | + var names []string |
| 12 | + if step.CLICommand != nil { |
| 13 | + for _, capture := range step.CLICommand.StdoutVariables { |
| 14 | + names = append(names, capture.Name) |
| 15 | + } |
| 16 | + } |
| 17 | + if step.HTTPRequest != nil { |
| 18 | + for _, capture := range step.HTTPRequest.ResponseVariables { |
| 19 | + names = append(names, capture.Name) |
| 20 | + } |
| 21 | + for _, capture := range step.HTTPRequest.ResponseHeaderVariables { |
| 22 | + names = append(names, capture.Name) |
| 23 | + } |
| 24 | + } |
| 25 | + return names |
| 26 | +} |
| 27 | + |
| 28 | +// Missing and invalidated workflow bindings both block execution. Keeping the |
| 29 | +// namespace separate from values prevents failed or forward captures being |
| 30 | +// mistaken for shell variables. |
| 31 | +func missingDependencies(step api.CLIStep, namespace map[string]bool, values map[string]string) []string { |
| 32 | + var names []string |
| 33 | + add := func(text string) { |
| 34 | + for _, name := range InterpolationNames(text) { |
| 35 | + if _, available := values[name]; namespace[name] && !available { |
| 36 | + names = append(names, name) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + var visit func(any) |
| 41 | + visit = func(value any) { |
| 42 | + switch value := value.(type) { |
| 43 | + case string: |
| 44 | + add(value) |
| 45 | + case []any: |
| 46 | + for _, item := range value { |
| 47 | + visit(item) |
| 48 | + } |
| 49 | + case map[string]any: |
| 50 | + for _, item := range value { |
| 51 | + visit(item) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + if step.CLICommand != nil { |
| 56 | + add(step.CLICommand.Command) |
| 57 | + } |
| 58 | + if step.HTTPRequest != nil { |
| 59 | + request := step.HTTPRequest.Request |
| 60 | + add(request.FullURL) |
| 61 | + for _, value := range request.Headers { |
| 62 | + add(value) |
| 63 | + } |
| 64 | + if request.BodyJSON != nil { |
| 65 | + visit(request.BodyJSON) |
| 66 | + } else { |
| 67 | + for _, value := range request.BodyForm { |
| 68 | + add(value) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + slices.Sort(names) |
| 73 | + return slices.Compact(names) |
| 74 | +} |
| 75 | + |
| 76 | +// Finalize even on early execution errors so previous output values cannot leak |
| 77 | +// into later steps. Extraction writes only to captured, never to values. |
| 78 | +func publishCaptures(names []string, values, captured map[string]string, captureErr string) string { |
| 79 | + for _, name := range names { |
| 80 | + delete(values, name) |
| 81 | + if _, found := captured[name]; !found && captureErr == "" { |
| 82 | + captureErr = fmt.Sprintf("missing value for variable '%s'", name) |
| 83 | + } |
| 84 | + } |
| 85 | + if captureErr == "" { |
| 86 | + for _, name := range names { |
| 87 | + values[name] = captured[name] |
| 88 | + } |
| 89 | + } |
| 90 | + return captureErr |
| 91 | +} |
0 commit comments