| 83 | } |
| 84 | |
| 85 | func run(ctx context.Context, cfg config, stdout, stderr io.Writer, getenv func(string) string) error { |
| 86 | if cfg.JSONFile == "" { |
| 87 | return xerrors.New("--jsonfile is required") |
| 88 | } |
| 89 | if cfg.MarkdownOut == "" { |
| 90 | cfg.MarkdownOut = "-" |
| 91 | } |
| 92 | if cfg.MaxOutputBytes < 0 { |
| 93 | return xerrors.New("--max-output-bytes must be non-negative") |
| 94 | } |
| 95 | if cfg.MaxFailures < 0 { |
| 96 | return xerrors.New("--max-failures must be non-negative") |
| 97 | } |
| 98 | if cfg.FailuresCapBytes <= 0 { |
| 99 | cfg.FailuresCapBytes = defaultFailuresCapBytes |
| 100 | } |
| 101 | |
| 102 | stat, err := os.Stat(cfg.JSONFile) |
| 103 | if err != nil { |
| 104 | if errors.Is(err, os.ErrNotExist) { |
| 105 | return writeEmptyOutputs(cfg) |
| 106 | } |
| 107 | return xerrors.Errorf("stat json file: %w", err) |
| 108 | } |
| 109 | if stat.Size() == 0 { |
| 110 | return writeEmptyOutputs(cfg) |
| 111 | } |
| 112 | |
| 113 | file, err := os.Open(cfg.JSONFile) |
| 114 | if err != nil { |
| 115 | return xerrors.Errorf("open json file: %w", err) |
| 116 | } |
| 117 | defer file.Close() |
| 118 | |
| 119 | result, err := summarize(ctx, file, cfg.MaxOutputBytes, stderr) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | if cfg.FailuresOut != "" { |
| 124 | if err := writeFailuresNDJSON(cfg.FailuresOut, result.Failures, cfg.FailuresCapBytes); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | } |
| 128 | if len(result.Failures) == 0 { |
| 129 | if cfg.MarkdownOut != "-" { |
| 130 | return os.WriteFile(cfg.MarkdownOut, nil, 0o600) |
| 131 | } |
| 132 | return nil |
| 133 | } |
| 134 | markdown := renderMarkdown(result, cfg.MaxFailures, cfg.FailuresOut, getenv("GITHUB_JOB")) |
| 135 | if cfg.MarkdownOut == "-" { |
| 136 | _, err = io.WriteString(stdout, markdown) |
| 137 | return err |
| 138 | } |
| 139 | return os.WriteFile(cfg.MarkdownOut, []byte(markdown), 0o600) |
| 140 | } |
| 141 | |
| 142 | func writeEmptyOutputs(cfg config) error { |