Adopt the native CommandKit application boundary

This commit is contained in:
2026-11-20 04:13:05 +00:00
parent 86692d58f9
commit a3c58141a0
25 changed files with 420 additions and 148 deletions

View File

@@ -2,70 +2,31 @@ package app
import (
"context"
"fmt"
"io"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/buildinfo"
"go.maximumdirect.net/commandkit"
)
var supportedCommands = []string{"version", "run", "regenerate-artifacts", "run-stage", "analyze", "publish", "clean", "session", "config"}
// Execute dispatches CLI commands and returns a process exit code.
func Execute(args []string, stdout, stderr io.Writer) int {
return executeWithDependencies(args, stdout, stderr, commandDependencies{}, executePlan)
// NewApplication constructs the native command boundary without acquiring configuration
// or initializing external services.
func NewApplication() (*commandkit.App, error) {
return newApplication(commandDependencies{}, executePlan)
}
func executeWithPipelineExecutor(args []string, stdout, stderr io.Writer, executor pipelineExecutor) int {
return executeWithDependencies(args, stdout, stderr, commandDependencies{}, executor)
func newApplication(deps commandDependencies, executor pipelineExecutor) (*commandkit.App, error) {
return commandkit.New(
commandkit.Identity{Name: "narratio", Version: buildinfo.Version},
commandkit.Group("artifacts", "Inspect and regenerate configured artifacts."),
commandkit.Group("session", "Manage and inspect sessions."),
)
}
func executeWithDependencies(args []string, stdout, stderr io.Writer, deps commandDependencies, executor pipelineExecutor) int {
if deps.diagnostics == nil {
deps.diagnostics = stderr
}
deps = normalizeCommandDependencies(deps)
if len(args) == 0 {
printUsage(stderr)
return 1
}
ctx := context.Background()
cmd := args[0]
cmdArgs := args[1:]
var err error
switch cmd {
case "version":
err = Version(cmdArgs, stdout)
case "run":
err = runWithDependencies(ctx, cmdArgs, stdout, executor, deps)
case "regenerate-artifacts":
err = regenerateArtifactsWithDependencies(ctx, cmdArgs, stdout, executor, deps)
case "run-stage":
err = runStageWithDependencies(ctx, cmdArgs, stdout, executor, deps)
case "analyze":
err = analyzeWithDependencies(ctx, cmdArgs, stdout, executor, deps)
case "publish":
err = publishWithDependencies(ctx, cmdArgs, stdout, executor, deps)
case "session":
err = sessionWithDependencies(ctx, cmdArgs, stdout, deps)
case "clean":
err = cleanWithDependencies(ctx, cmdArgs, stdout, deps)
case "config":
err = configWithDependencies(ctx, cmdArgs, stdout, deps)
default:
fmt.Fprintf(stderr, "unknown command: %q\n\n", cmd)
printUsage(stderr)
return 1
}
// Execute runs an invocation with caller-owned context and streams and returns
// its finalized native outcome. The process boundary owns exit handling.
func Execute(ctx context.Context, args []string, streams commandkit.Streams) commandkit.Outcome {
application, err := NewApplication()
if err != nil {
fmt.Fprintf(stderr, "%v\n", err)
return 1
return operationOutcome(err)
}
return 0
}
func printUsage(w io.Writer) {
fmt.Fprintf(w, "Usage: narratio <%s>\n", strings.Join(supportedCommands, "|"))
return application.Execute(ctx, args, streams)
}