74 lines
3.0 KiB
Go
74 lines
3.0 KiB
Go
|
|
package cli
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"os"
|
||
|
|
|
||
|
|
"go.maximumdirect.net/commandkit"
|
||
|
|
"go.maximumdirect.net/promptkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
func reportFlags() []commandkit.Flag[runInput] {
|
||
|
|
return []commandkit.Flag[runInput]{
|
||
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "report", Usage: "Additionally write the native run result to a file.", Completion: commandkit.CompleteFiles()}, (*string)(nil), parseString,
|
||
|
|
func(input *runInput, value commandkit.Supplied[string]) { input.ReportPath = value }),
|
||
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "report-format", Usage: "Select the separate report format (omission uses native automatic rendering).", Completion: commandkit.CompleteChoices(commandkit.CompletionChoice{Value: "text"}, commandkit.CompletionChoice{Value: "yaml"}, commandkit.CompletionChoice{Value: "json"})}, (*commandkit.Format)(nil), parseReportFormat,
|
||
|
|
func(input *runInput, value commandkit.Supplied[commandkit.Format]) { input.ReportFormat = value }),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func parseReportFormat(raw string) (commandkit.Format, error) {
|
||
|
|
format := commandkit.Format(raw)
|
||
|
|
switch format {
|
||
|
|
case commandkit.FormatText, commandkit.FormatYAML, commandkit.FormatJSON:
|
||
|
|
return format, nil
|
||
|
|
default:
|
||
|
|
return "", errors.New("report format must be text, yaml, or json")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func checkReportInput(path commandkit.Supplied[string]) error {
|
||
|
|
if path.Set && path.Value == "" {
|
||
|
|
return errors.New("report path must not be empty")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func deliverRunResult(invocation commandkit.Invocation, input runInput, result *promptkit.RunResult) commandkit.Outcome {
|
||
|
|
outcome := completedRunOutcome(result)
|
||
|
|
if result == nil {
|
||
|
|
return outcome
|
||
|
|
}
|
||
|
|
// The invocation reporter records its own presentation failures. A requested
|
||
|
|
// file remains an independent effect and is attempted even after stdout fails.
|
||
|
|
_ = invocation.Reporter().Report(commandkit.Report{Value: result, TextTemplate: artifactTextTemplate})
|
||
|
|
if input.ReportPath.Set {
|
||
|
|
if err := writeNativeReport(input.ReportPath.Value, result, input.ReportFormat.Value); err != nil {
|
||
|
|
if outcome.Status == 0 {
|
||
|
|
outcome = commandkit.Failure("The report file could not be delivered.")
|
||
|
|
} else {
|
||
|
|
outcome.Explanation.Message += " The report file could not be delivered."
|
||
|
|
}
|
||
|
|
outcome.Cause = errors.Join(outcome.Cause, err)
|
||
|
|
outcome.Explanation.Details = append(outcome.Explanation.Details, func(writer io.Writer) error { _, writeErr := fmt.Fprintln(writer, err); return writeErr })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
_ = invocation.Reporter().Info("Prompt execution completed.")
|
||
|
|
return outcome
|
||
|
|
}
|
||
|
|
|
||
|
|
func writeNativeReport(path string, result *promptkit.RunResult, format commandkit.Format) error {
|
||
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("open report %q: %w", path, err)
|
||
|
|
}
|
||
|
|
renderErr := commandkit.RenderReport(file, commandkit.Report{Value: result}, format)
|
||
|
|
closeErr := file.Close()
|
||
|
|
if err := errors.Join(renderErr, closeErr); err != nil {
|
||
|
|
return fmt.Errorf("deliver report %q: %w", path, err)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|