writeOutputJSON writes the output map to the specified JSON file.
(w *world.World)
| 24 | |
| 25 | // writeOutputJSON writes the output map to the specified JSON file. |
| 26 | func writeOutputJSON(w *world.World) error { |
| 27 | if w.Output == "" { |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | w.Logger.Info("writing output to file", "file", w.Output) |
| 32 | |
| 33 | // Create parent directory if not exists |
| 34 | if dir := filepath.Dir(w.Output); dir != "" { |
| 35 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 36 | return errors.Wrapf(err, "failed to create output directory: %s", dir) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | f, err := os.Create(w.Output) |
| 41 | if err != nil { |
| 42 | return errors.Wrapf(err, "failed to create output file: %s", w.Output) |
| 43 | } |
| 44 | defer f.Close() |
| 45 | |
| 46 | // Marshal OutputMap with protojson support for CheckResults |
| 47 | outputData := make(map[string]any) |
| 48 | if w.OutputMap.Release != "" { |
| 49 | outputData["release"] = w.OutputMap.Release |
| 50 | } |
| 51 | if w.OutputMap.Plan != "" { |
| 52 | outputData["plan"] = w.OutputMap.Plan |
| 53 | } |
| 54 | if w.OutputMap.Rollout != "" { |
| 55 | outputData["rollout"] = w.OutputMap.Rollout |
| 56 | } |
| 57 | if w.OutputMap.CheckResults != nil { |
| 58 | // Use protojson to marshal CheckResults with camelCase keys |
| 59 | checkResultsJSON, err := protojson.Marshal(w.OutputMap.CheckResults) |
| 60 | if err != nil { |
| 61 | return errors.Wrapf(err, "failed to marshal check results") |
| 62 | } |
| 63 | var checkResultsMap map[string]any |
| 64 | if err := json.Unmarshal(checkResultsJSON, &checkResultsMap); err != nil { |
| 65 | return errors.Wrapf(err, "failed to unmarshal check results") |
| 66 | } |
| 67 | outputData["checkResults"] = checkResultsMap |
| 68 | } |
| 69 | |
| 70 | j, err := json.MarshalIndent(outputData, "", " ") |
| 71 | if err != nil { |
| 72 | return errors.Wrapf(err, "failed to marshal output map") |
| 73 | } |
| 74 | |
| 75 | if _, err := f.Write(j); err != nil { |
| 76 | return errors.Wrapf(err, "failed to write output file: %s", w.Output) |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // writeGitHubStepSummary writes the GitHub step summary for rollout operations. |
| 82 | func writeGitHubStepSummary(w *world.World) error { |