Files
notarius/internal/cli/run_result.go

79 lines
2.8 KiB
Go

package cli
import (
"fmt"
"io"
"strings"
"text/template"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"go.maximumdirect.net/commandkit"
)
// runResult is the small completion value. Domain files, not a saved report or
// execution ledger, are the durable deliverables.
type runResult struct {
Pipeline string `json:"pipeline"`
ArtifactCount int `json:"artifact_count"`
OutputDirectory string `json:"output_directory"`
DebugDirectory string `json:"debug_directory,omitempty"`
FallbackCount int `json:"fallback_count,omitempty"`
}
var runResultTemplate = template.Must(commandkit.ParseTextTemplate("run-completion", `{{if .FallbackCount}}Success with fallback: {{else}}Success: {{end}}{{.Pipeline}} produced {{.ArtifactCount}} artifacts in {{.OutputDirectory}}
{{if .FallbackCount}}{{.FallbackCount}} operations used fallback.
{{end}}{{if .DebugDirectory}}Debug capture: {{.DebugDirectory}}
{{end}}`, nil))
func reportRunResult(invocation commandkit.Invocation, result runResult) commandkit.Outcome {
message := "Could not present the completed run." + runLocations(result.OutputDirectory, result.DebugDirectory)
if err := invocation.Reporter().Report(commandkit.Report{Value: result, TextTemplate: runResultTemplate}); err != nil {
// The reporter retains render/write failures in Presentation. They are
// not a replacement operation Cause, and capture is already finalized.
return commandkit.Failure(message)
}
// Deferred diagnostic delivery can still fail during native finalization.
// Supply useful locations without presenting a failure on successful runs.
return commandkit.Success().WithExplanation(message)
}
func runLocations(outputDirectory, debugDirectory string) string {
var locations string
if outputDirectory != "" {
locations += "\nArtifacts: " + outputDirectory
}
if debugDirectory != "" {
locations += "\nDebug capture: " + debugDirectory
}
return locations
}
func reportRunDiagnostics(reporter *commandkit.Reporter, collection contracts.DiagnosticCollection) {
for _, group := range collection.Groups {
origin := group.Origin
scope := []string{string(origin.Stage)}
for _, value := range []string{origin.StepID, origin.LaneID, origin.ModuleKey, origin.ValidatorKey} {
if value != "" {
scope = append(scope, value)
}
}
heading := strings.Join(scope, " / ") + ": " + group.ReasonCode
detail := func(writer io.Writer) error {
for _, sample := range group.Samples {
if _, err := fmt.Fprintf(writer, "%s: %s\n", sample.Scope, sample.Message); err != nil {
return err
}
}
return nil
}
if group.Disposition == contracts.DiagnosticDispositionWarning {
_ = reporter.AtEnd().Warn(heading, detail)
} else {
_ = reporter.AtEnd().Info(heading, detail)
}
}
if collection.Truncated {
_ = reporter.AtEnd().Info("Additional pipeline diagnostics were omitted.")
}
}