Establish native CommandKit command composition
This commit is contained in:
55
internal/adapter/cli/app.go
Normal file
55
internal/adapter/cli/app.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
"go.maximumdirect.net/ckprompt"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
// Execute composes the native CLI and returns its finalized outcome. The caller
|
||||
// owns the context, streams, and process exit.
|
||||
func Execute(ctx context.Context, identity commandkit.Identity, args []string, stdout, stderr io.Writer) commandkit.Outcome {
|
||||
app, err := newApp(identity)
|
||||
if err != nil {
|
||||
return commandkit.Failure("Could not compose application commands.").WithCause(err)
|
||||
}
|
||||
return app.Execute(ctx, args, commandkit.Streams{Out: stdout, Err: stderr})
|
||||
}
|
||||
|
||||
func newApp(identity commandkit.Identity) (*commandkit.App, error) {
|
||||
application := ckconfig.FromConfigKit("app", "Scriptorium application configuration.", loadApplication)
|
||||
framework := ckprompt.ConfigScope(loadEngine)
|
||||
configuration := ckconfig.Command(ckconfig.Suite[struct{}, struct{}]{
|
||||
Scopes: []ckconfig.Scope[struct{}, struct{}]{application, framework},
|
||||
})
|
||||
definitions := ckprompt.FromEngine(loadEngine)
|
||||
declarations := []commandkit.Declaration{
|
||||
commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Summary: "Run a stored prompt.", Run: commandNotImplemented}),
|
||||
commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "serve", Summary: "Serve the HTTP API.", Run: commandNotImplemented}),
|
||||
configuration,
|
||||
commandkit.Completion("scriptorium"),
|
||||
}
|
||||
declarations = append(declarations, ckprompt.Declarations(definitions)...)
|
||||
return commandkit.New(identity, declarations...)
|
||||
}
|
||||
|
||||
// Application acquisition and effects are filled in independently of command
|
||||
// declaration construction; metadata operations must never reach these callbacks.
|
||||
func loadApplication(context.Context, ckconfig.Selection, struct{}, struct{}) (*configkit.Result[appconfig.Document], error) {
|
||||
return nil, errors.New("application configuration acquisition is not implemented")
|
||||
}
|
||||
|
||||
func loadEngine(context.Context, ckconfig.Selection, struct{}, struct{}) (*promptkit.Engine, error) {
|
||||
return nil, errors.New("engine acquisition is not implemented")
|
||||
}
|
||||
|
||||
func commandNotImplemented(commandkit.Invocation, struct{}) commandkit.Outcome {
|
||||
return commandkit.Failure("Application command is not implemented.")
|
||||
}
|
||||
31
internal/adapter/cli/app_test.go
Normal file
31
internal/adapter/cli/app_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func TestNativeMetadataWithoutConfiguration(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
for _, args := range [][]string{
|
||||
{"--help"}, {"version"}, {"run", "--help"}, {"serve", "--help"},
|
||||
{"config", "scopes"}, {"config", "show", "--help"},
|
||||
{"promptkit", "--help"}, {"promptkit", "list", "prompts", "--help"},
|
||||
{"completion", "bash"}, {"completion", "zsh"}, {"completion", "fish"},
|
||||
} {
|
||||
t.Run(strings.Join(args, " "), func(t *testing.T) {
|
||||
var out, diagnostics bytes.Buffer
|
||||
outcome := Execute(context.Background(), commandkit.Identity{Name: "scriptorium", Version: "test-version", Revision: "test-revision"}, args, &out, &diagnostics)
|
||||
if outcome.Status != 0 || out.Len() == 0 {
|
||||
t.Fatalf("outcome=%#v stdout=%q stderr=%q", outcome, out.String(), diagnostics.String())
|
||||
}
|
||||
if args[0] == "version" && !strings.Contains(out.String(), "test-version") {
|
||||
t.Fatalf("version identity missing: %q", out.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
|
||||
executionreport "gitea.maximumdirect.net/eric/scriptorium/internal/report"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
@@ -112,27 +113,7 @@ func (l *listFlag) Set(v string) error {
|
||||
}
|
||||
|
||||
func Run(args []string, stdout, stderr io.Writer) int {
|
||||
if len(args) == 0 {
|
||||
printUsage(stderr)
|
||||
return ExitRuntimeError
|
||||
}
|
||||
|
||||
switch args[0] {
|
||||
case "run":
|
||||
return runCommand(args[1:], stdout, stderr)
|
||||
case "render":
|
||||
return renderCommand(args[1:], stdout, stderr)
|
||||
case "serve":
|
||||
return serveCommand(args[1:], stderr)
|
||||
case "inspect":
|
||||
return inspectCommand(args[1:], stdout, stderr)
|
||||
case "config":
|
||||
return configCommand(args[1:], stdout, stderr)
|
||||
default:
|
||||
fmt.Fprintf(stderr, "unknown command %q\n", args[0])
|
||||
printUsage(stderr)
|
||||
return ExitRuntimeError
|
||||
}
|
||||
return Execute(context.Background(), commandkit.Identity{Name: "scriptorium", Version: "dev"}, args, stdout, stderr).Status
|
||||
}
|
||||
|
||||
func runCommand(args []string, stdout, stderr io.Writer) int {
|
||||
|
||||
Reference in New Issue
Block a user