package cli import ( "context" "errors" "fmt" "time" "gitea.maximumdirect.net/eric/audita/v3/internal/core/chunking" "gitea.maximumdirect.net/eric/audita/v3/internal/core/diagnostics" "gitea.maximumdirect.net/eric/audita/v3/internal/core/reporting" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes" commandkit "go.maximumdirect.net/commandkit" ) // finishProcess delivers the transcript and reports before emitting success information. func finishProcess(inv commandkit.Invocation, input processInput, prepared *preparedProcess, snapshot reporting.Snapshot, transcript []byte, primary error) commandkit.Outcome { reporter := inv.Reporter() for _, warning := range prepared.Prepared.Warnings { _ = reporter.Warn("Debug artifact: " + warning.Error()) } completed := time.Now().UTC() snapshot.RunID, snapshot.StartedAt, snapshot.CompletedAt = prepared.RunID, prepared.Started, &completed snapshot.TranscriptPath, snapshot.OutputPath = input.Transcript, input.Output.Value snapshot.Status = reporting.StatusCompleted reportPaths := prepared.Prepared.Output.ReportPaths() var render diagnostics.ReportRenderer if len(reportPaths) > 0 { render = func(cause error, failures []diagnostics.FileFailure) (diagnostics.ReportBundle, error) { report := snapshot if cause != nil { report = failedProcessingSnapshot(report, cause, inv.Context.Err()) } if len(failures) > 0 { report = failedOutputSnapshot(report, failures) } return renderReportBundle(report) } } result := prepared.Prepared.Output.Finish(inv.Context, primary, transcript, render) if result != nil { var infeasible *chunking.InfeasibleTargetError var outputFailure diagnostics.FileFailure if inv.Context.Err() == nil && errors.As(result, &infeasible) && !errors.As(result, &outputFailure) { return commandkit.Usage(infeasible.Error()).WithCause(result) } return commandkit.Failure(result.Error()).WithCause(result) } _ = reporter.AtEnd().Info(reporting.CompletionLine(snapshot)) for _, path := range reportPaths { _ = reporter.AtEnd().Info(fmt.Sprintf("Report: %s", path)) } if prepared.Prepared.Debug != nil { _ = reporter.AtEnd().Info(fmt.Sprintf("Diagnostics: %s", prepared.Prepared.Debug.Path())) } return commandkit.Success() } func failedProcessingSnapshot(snapshot reporting.Snapshot, primary, contextErr error) reporting.Snapshot { for _, record := range snapshot.Errors { if record.Scope == "run" { return failedOutputSnapshot(snapshot, nil) } } record := reporting.ErrorRecord{Scope: "run", Reason: "processing_failure", Message: "Processing failed"} if errors.Is(primary, context.Canceled) || (contextErr != nil && errors.Is(primary, contextErr)) { record.Phase = "processing" record.Reason = string(outcomes.ReasonCanceled) record.Message = "Processing was canceled." } snapshot.Errors = append(snapshot.Errors, record) return failedOutputSnapshot(snapshot, nil) } // 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 }