Add preflight command and run alias
This commit is contained in:
@@ -118,7 +118,7 @@ func buildApplicationWithClient(client *http.Client, extra ...commandkit.Declara
|
||||
})
|
||||
definitions = ckprompt.WithSmokeTesting(definitions, capturePromptEngine, nil)
|
||||
definitions = withPromptBootstrapContext(definitions)
|
||||
declarations := []commandkit.Declaration{configCommand, commandkit.Completion("audita"), commandkit.WithInvocationFeature(processCommand(), bootstrapFeature())}
|
||||
declarations := []commandkit.Declaration{configCommand, commandkit.Completion("audita"), commandkit.WithInvocationFeature(processCommand(), bootstrapFeature()), commandkit.WithInvocationFeature(preflightCommand(), bootstrapFeature()), commandkit.Alias("run", "process")}
|
||||
declarations = append(declarations, ckprompt.Declarations(definitions)...)
|
||||
declarations = append(declarations, extra...)
|
||||
return commandkit.New(commandkit.Identity{Name: "audita", Version: buildinfo.Version()}, declarations...)
|
||||
|
||||
22
internal/cli/preflight.go
Normal file
22
internal/cli/preflight.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/diagnostics"
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func runPreflight(inv commandkit.Invocation, input processInput) commandkit.Outcome {
|
||||
loaded, err := loadProcessInputs(inv.Context, input, inv.ReportingMode())
|
||||
if err != nil {
|
||||
return acquisitionOutcome(inv, err)
|
||||
}
|
||||
prepared, err := prepareLocal(inv.Context, input, loaded, inv.Reporter().Output())
|
||||
if err != nil {
|
||||
return preparationOutcome(inv, err)
|
||||
}
|
||||
if err := diagnostics.ProbeOutput(inv.Context, prepared.Destinations); err != nil {
|
||||
return commandkit.Failure(err.Error()).WithCause(err)
|
||||
}
|
||||
_ = inv.Reporter().AtEnd().Info("Local preflight checks passed.")
|
||||
return commandkit.Success()
|
||||
}
|
||||
71
internal/cli/preflight_test.go
Normal file
71
internal/cli/preflight_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func TestPreflightBindingsAndRunAlias(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for name, contents := range map[string]string{
|
||||
"config.yml": "version: 3\npipeline: {stages: []}\nreporting: {enabled: false}\n",
|
||||
"transcript.json": `[{"speaker":"Alice","start":0,"end":1,"text":"hello"}]`,
|
||||
"glossary.yml": "party: {entries: [{id: alice, name: Alice, category: person}]}\n",
|
||||
"overlays/local.yml": "context: {description: Local example}\n",
|
||||
} {
|
||||
path := filepath.Join(dir, name)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
app, err := buildApplication()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, transcript, output := filepath.Join(dir, "config.yml"), filepath.Join(dir, "transcript.json"), filepath.Join(dir, "out.json")
|
||||
if err := os.WriteFile(output, []byte("existing"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
common := []string{"--config", cfg, "--overlay", "local", "--glossary-file", filepath.Join(dir, "glossary.yml"), "--glossaries", `["party"]`, "--output", output, transcript}
|
||||
var out, stderr bytes.Buffer
|
||||
invoke := func(args ...string) int {
|
||||
out.Reset()
|
||||
stderr.Reset()
|
||||
return app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr}).Status
|
||||
}
|
||||
args := append([]string{"preflight", "--smoke=false"}, common...)
|
||||
if status := invoke(args...); status != 0 {
|
||||
t.Fatalf("preflight %d: %s", status, stderr.String())
|
||||
}
|
||||
if data, err := os.ReadFile(output); err != nil || string(data) != "existing" {
|
||||
t.Fatalf("destination changed: %q %v", data, err)
|
||||
}
|
||||
if status := invoke("preflight", "--help"); status != 0 || !strings.Contains(out.String(), "--smoke") {
|
||||
t.Fatalf("help %d: %s %s", status, out.String(), stderr.String())
|
||||
}
|
||||
if status := invoke("preflight", "--quiet", "--config", filepath.Join(dir, "missing"), transcript); status != 2 || !strings.Contains(stderr.String(), "quiet") {
|
||||
t.Fatalf("quiet %d: %s", status, stderr.String())
|
||||
}
|
||||
if status := invoke("preflight", "--quiet", "--smoke=false", "--config", cfg, "--output", os.DevNull, transcript); status != 0 {
|
||||
t.Fatalf("null %d: %s", status, stderr.String())
|
||||
}
|
||||
args = append([]string{"run"}, common...)
|
||||
if status := invoke(args...); status != 0 {
|
||||
t.Fatalf("alias %d: %s", status, stderr.String())
|
||||
}
|
||||
if data, err := os.ReadFile(output); err != nil || string(data) == "existing" || !strings.Contains(string(data), "hello") {
|
||||
t.Fatalf("alias output: %q %v", data, err)
|
||||
}
|
||||
if status := invoke("process", "--smoke=false", transcript); status != 2 {
|
||||
t.Fatalf("process accepted smoke flag: %d", status)
|
||||
}
|
||||
}
|
||||
@@ -25,37 +25,11 @@ type processResult struct {
|
||||
func runProcess(inv commandkit.Invocation, input processInput) commandkit.Outcome {
|
||||
loaded, err := loadProcessInputs(inv.Context, input, inv.ReportingMode())
|
||||
if err != nil {
|
||||
if canceled := inv.Context.Err(); canceled != nil && !errors.Is(err, canceled) {
|
||||
err = errors.Join(err, canceled)
|
||||
}
|
||||
var loadErr *ck.LoadError
|
||||
if errors.As(err, &loadErr) {
|
||||
_, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", loadErr.Sources(), loadErr.Diagnostics())
|
||||
if inv.Context.Err() == nil && ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage {
|
||||
return commandkit.Usage(loadErr.Error(), explanation.Details...).WithCause(err)
|
||||
}
|
||||
return commandkit.Failure(loadErr.Error(), explanation.Details...).WithCause(err)
|
||||
}
|
||||
var secretsErr *bootstrapError
|
||||
if errors.As(err, &secretsErr) {
|
||||
return commandkit.Failure(secretsErr.Error()).WithCause(err)
|
||||
}
|
||||
var runtimeErr *runtimePreparationError
|
||||
if errors.As(err, &runtimeErr) {
|
||||
return commandkit.Failure(err.Error()).WithCause(err)
|
||||
}
|
||||
if inv.Context.Err() != nil || appconfig.IsAssetError(err) {
|
||||
return commandkit.Failure("Could not prepare processing.").WithCause(err)
|
||||
}
|
||||
return commandkit.Usage(err.Error()).WithCause(err)
|
||||
return acquisitionOutcome(inv, err)
|
||||
}
|
||||
prepared, err := prepareProcess(inv.Context, input, loaded, inv.Reporter().Output())
|
||||
if err != nil {
|
||||
var selection *preflight.SelectionError
|
||||
if inv.Context.Err() == nil && errors.As(err, &selection) {
|
||||
return commandkit.Usage(err.Error()).WithCause(err)
|
||||
}
|
||||
return commandkit.Failure(err.Error()).WithCause(err)
|
||||
return preparationOutcome(inv, err)
|
||||
}
|
||||
defer prepared.Prepared.Output.Close()
|
||||
var warningsMu sync.Mutex
|
||||
@@ -110,3 +84,37 @@ func (p *preparedProcess) DebugPath() string {
|
||||
}
|
||||
return p.Prepared.Debug.Path()
|
||||
}
|
||||
|
||||
func acquisitionOutcome(inv commandkit.Invocation, err error) commandkit.Outcome {
|
||||
if canceled := inv.Context.Err(); canceled != nil && !errors.Is(err, canceled) {
|
||||
err = errors.Join(err, canceled)
|
||||
}
|
||||
var loadErr *ck.LoadError
|
||||
if errors.As(err, &loadErr) {
|
||||
_, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", loadErr.Sources(), loadErr.Diagnostics())
|
||||
if inv.Context.Err() == nil && ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage {
|
||||
return commandkit.Usage(loadErr.Error(), explanation.Details...).WithCause(err)
|
||||
}
|
||||
return commandkit.Failure(loadErr.Error(), explanation.Details...).WithCause(err)
|
||||
}
|
||||
var secretsErr *bootstrapError
|
||||
if errors.As(err, &secretsErr) {
|
||||
return commandkit.Failure(secretsErr.Error()).WithCause(err)
|
||||
}
|
||||
var runtimeErr *runtimePreparationError
|
||||
if errors.As(err, &runtimeErr) {
|
||||
return commandkit.Failure(err.Error()).WithCause(err)
|
||||
}
|
||||
if inv.Context.Err() != nil || appconfig.IsAssetError(err) {
|
||||
return commandkit.Failure("Could not prepare processing.").WithCause(err)
|
||||
}
|
||||
return commandkit.Usage(err.Error()).WithCause(err)
|
||||
}
|
||||
|
||||
func preparationOutcome(inv commandkit.Invocation, err error) commandkit.Outcome {
|
||||
var selection *preflight.SelectionError
|
||||
if inv.Context.Err() == nil && errors.As(err, &selection) {
|
||||
return commandkit.Usage(err.Error()).WithCause(err)
|
||||
}
|
||||
return commandkit.Failure(err.Error()).WithCause(err)
|
||||
}
|
||||
|
||||
@@ -14,11 +14,12 @@ type processInput struct {
|
||||
Transcript string
|
||||
Output commandkit.Supplied[string]
|
||||
Debug commandkit.Supplied[bool]
|
||||
Smoke commandkit.Supplied[bool]
|
||||
Overrides []ck.Override
|
||||
GlossaryFiles []string
|
||||
}
|
||||
|
||||
func processCommand() commandkit.Command {
|
||||
func processingFlags() []commandkit.Flag[processInput] {
|
||||
stringValue := func(s string) (string, error) { return s, nil }
|
||||
flags := []commandkit.Flag[processInput]{
|
||||
commandkit.RepeatFlag(commandkit.FlagMeta{Name: "glossary-file", Usage: "Runtime glossary file (repeatable)."}, nil, stringValue, func(in *processInput, v commandkit.Supplied[[]string]) { in.GlossaryFiles = v.Value }),
|
||||
@@ -94,9 +95,20 @@ func processCommand() commandkit.Command {
|
||||
in.Overrides = append(in.Overrides, ck.Override{Path: ck.Keys("reporting", "enabled"), Value: v.Value, Flag: "--reporting"})
|
||||
}
|
||||
}))
|
||||
return commandkit.NewCommand(commandkit.CommandSpec[processInput]{Name: "process", Summary: "Polish a transcript.", Flags: flags, Run: runProcess,
|
||||
Arguments: []commandkit.Argument[processInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "transcript", Usage: "Transcript JSON file."}, true, stringValue, func(in *processInput, v commandkit.Supplied[string]) { in.Transcript = v.Value })},
|
||||
})
|
||||
return flags
|
||||
}
|
||||
|
||||
func transcriptArguments() []commandkit.Argument[processInput] {
|
||||
return []commandkit.Argument[processInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "transcript", Usage: "Transcript JSON file."}, true, func(s string) (string, error) { return s, nil }, func(in *processInput, v commandkit.Supplied[string]) { in.Transcript = v.Value })}
|
||||
}
|
||||
|
||||
func processCommand() commandkit.Command {
|
||||
return commandkit.NewCommand(commandkit.CommandSpec[processInput]{Name: "process", Summary: "Polish a transcript.", Flags: processingFlags(), Run: runProcess, Arguments: transcriptArguments()})
|
||||
}
|
||||
|
||||
func preflightCommand() commandkit.Command {
|
||||
flags := append(processingFlags(), commandkit.BoolFlag(commandkit.FlagMeta{Name: "smoke", Usage: "Verify structured output with selected profiles."}, true, func(in *processInput, v commandkit.Supplied[bool]) { in.Smoke = v }))
|
||||
return commandkit.NewCommand(commandkit.CommandSpec[processInput]{Name: "preflight", Summary: "Check processing readiness.", Flags: flags, Run: runPreflight, Arguments: transcriptArguments()})
|
||||
}
|
||||
|
||||
func parseCollectionNames(raw string) (any, error) {
|
||||
|
||||
Reference in New Issue
Block a user