Files
scriptorium/internal/adapter/cli/app.go

55 lines
2.2 KiB
Go
Raw Normal View History

package cli
import (
"context"
"io"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/ckprompt"
"go.maximumdirect.net/commandkit"
)
// 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[applicationInput, struct{}]{
NewInput: func() applicationInput { return applicationInput{} },
Scopes: []ckconfig.Scope[applicationInput, 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[serveInput]{Name: "serve", Summary: "Serve the HTTP API.", Flags: serveFlags(), Run: func(commandkit.Invocation, serveInput) commandkit.Outcome {
return commandkit.Failure("Server startup is not implemented.")
}}),
configuration,
commandkit.Completion("scriptorium"),
}
declarations = append(declarations, ckprompt.Declarations(definitions)...)
return commandkit.New(identity, declarations...)
}
type serveInput struct {
Selection ckconfig.Selection
Application applicationInput
}
func serveFlags() []commandkit.Flag[serveInput] {
flags := configurationSelectors(func(input *serveInput) *ckconfig.Selection { return &input.Selection })
return append(flags, serverSelectors(func(input *serveInput) *applicationInput { return &input.Application })...)
}
func commandNotImplemented(commandkit.Invocation, struct{}) commandkit.Outcome {
return commandkit.Failure("Application command is not implemented.")
}