package cli import ( "fmt" "time" "gitea.maximumdirect.net/eric/audita/v3/internal/core/diagnostics" "gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction" "gitea.maximumdirect.net/eric/audita/v3/internal/core/reporting" commandkit "go.maximumdirect.net/commandkit" ) // finishProcess delivers the transcript and reports before emitting success information. func finishProcess(inv commandkit.Invocation, input processInput, loaded *processInputs, prepared *preparedProcess, snapshot reporting.Snapshot, transcript []byte, primary error) commandkit.Outcome { reporter := inv.Reporter() secrets := loaded.Runtime.ConfiguredSecrets(inv.Context) redact := func(err error) string { return loaded.Runtime.RedactError(inv.Context, err) } for _, warning := range prepared.Prepared.Warnings { _ = reporter.Warn("Debug artifact: " + redact(warning)) } completed := time.Now().UTC() snapshot.RunID, snapshot.StartedAt, snapshot.CompletedAt = prepared.RunID, prepared.Started, &completed snapshot.TranscriptPath, snapshot.OutputPath = input.Transcript, input.Output.Value if primary != nil { snapshot.Status = reporting.StatusFailed snapshot = failedOutputSnapshot(snapshot, nil) if *snapshot.Summary.RunErrors == 0 { snapshot.Errors = append(snapshot.Errors, reporting.ErrorRecord{Scope: "run", Reason: "processing_failure", Message: "Processing failed"}) snapshot = failedOutputSnapshot(snapshot, nil) } } else { snapshot.Status = reporting.StatusCompleted } reports, err := renderReportBundle(snapshot, secrets...) if err != nil { if primary == nil { primary = err } snapshot.Status = reporting.StatusFailed } failed := func(failures []diagnostics.FileFailure) diagnostics.ReportBundle { result, bundleErr := renderReportBundle(failedOutputSnapshot(snapshot, failures), secrets...) if bundleErr != nil { return reports } return result } result := prepared.Prepared.Output.Finish(primary, transcript, reports, failed) if result != nil { return commandkit.Failure(redact(result)).WithCause(result) } safe := redaction.New(secrets) _ = reporter.AtEnd().Info(reporting.CompletionLine(snapshot)) for _, path := range prepared.Prepared.Output.ReportPaths() { _ = reporter.AtEnd().Info(fmt.Sprintf("Report: %s", safe.String(path))) } if prepared.Prepared.Debug != nil { _ = reporter.AtEnd().Info(fmt.Sprintf("Diagnostics: %s", safe.String(prepared.Prepared.Debug.Path()))) } return commandkit.Success() } // failedOutputSnapshot derives the count from the same run records sent to each report. func failedOutputSnapshot(snapshot reporting.Snapshot, failures []diagnostics.FileFailure) reporting.Snapshot { snapshot.Status = reporting.StatusFailed if len(failures) > 0 { snapshot.Errors = append([]reporting.ErrorRecord(nil), snapshot.Errors...) } for _, failure := range failures { record := reporting.ErrorRecord{Scope: "run", Reason: "output_failure", Message: "Required output delivery failed", OutputPath: failure.Path} if failure.Kind == "report initialization" { record.Phase = "initialization" record.Message = "Required report initialization failed" } snapshot.Errors = append(snapshot.Errors, record) } count := 0 for _, record := range snapshot.Errors { if record.Scope == "run" { count++ } } snapshot.Summary.RunErrors = &count return snapshot }