package cli import ( "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/processreport" 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 || len(failures) > 0 { phase := "processing" if primary == nil { phase = "publication" } report = processreport.Failed(snapshot, cause, phase, 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() }