Use native configuration views and shared prompt loading

This commit is contained in:
2026-11-23 03:28:30 +00:00
parent b6693fde73
commit 5e29399705
6 changed files with 49 additions and 130 deletions

View File

@@ -1,59 +0,0 @@
package cli
import (
"errors"
"fmt"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
)
type applicationAcquisition struct {
sources []configkit.SourceInfo
records []configkit.Diagnostic
err error
}
// acquisitionState holds only presentation evidence for this invocation, never
// configuration or engines. Each reached loader still performs a fresh load.
type acquisitionState struct{ applications []applicationAcquisition }
// Native PromptKit adapters interpret a ConfigKit LoadError as framework
// evidence. Transport application acquisition failure separately so it cannot
// become a finding in the framework view. The callback decorator restores the
// original typed cause before the native command classifies the outcome.
var errApplicationAcquisition = errors.New("application configuration acquisition failed")
func (state *acquisitionState) mark() int {
if state == nil {
return 0
}
return len(state.applications)
}
func attachApplicationEvidence[T any](acquired commandkit.Acquired[T], err error, state *acquisitionState, start int, comparison bool) (commandkit.Acquired[T], error) {
if state == nil {
return acquired, err
}
for index, application := range state.applications[start:] {
context := "Application configuration"
if comparison {
side := "left"
if index > 0 {
side = "right"
}
context += " (" + side + ")"
}
diagnostics, explanation := ckconfig.AdaptDiagnostics(context+":\n", application.sources, application.records)
acquired.Diagnostics = append(acquired.Diagnostics, diagnostics...)
if acquired.Explanation.Message == "" {
acquired.Explanation.Message = explanation.Message
}
acquired.Explanation.Details = append(acquired.Explanation.Details, explanation.Details...)
if application.err != nil && errors.Is(err, errApplicationAcquisition) {
err = fmt.Errorf("%s: %w", context, application.err)
}
}
return acquired, err
}

View File

@@ -117,3 +117,33 @@ func TestServerBootstrapsBeforeListening(t *testing.T) {
t.Fatalf("bootstrap repeated or leaked: response=%q stdout=%q stderr=%q", body, out.String(), diagnostics.String())
}
}
func TestNativeScopesAndHelpUseSharedLoading(t *testing.T) {
const name = "SCRIPTORIUM_TEST_SCOPE_CREDENTIAL"
t.Setenv(name, "")
root := t.TempDir()
if err := os.Mkdir(filepath.Join(root, "secrets"), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "secrets", name), []byte("dummy"), 0600); err != nil {
t.Fatal(err)
}
path := filepath.Join(root, "app.yml")
if err := os.WriteFile(path, []byte("config: {secrets_dir: secrets, secrets_overwrite: true}\n"), 0600); err != nil {
t.Fatal(err)
}
for _, args := range [][]string{{"config", "scopes"}, {"config", "explain", "--config=" + path}, {"--help"}, {"completion", "bash"}, {"completion", "zsh"}, {"completion", "fish"}} {
var out, diagnostic bytes.Buffer
outcome := Execute(context.Background(), commandkit.Identity{Name: "scriptorium"}, args, &out, &diagnostic)
if outcome.Status != 0 || os.Getenv(name) != "" || strings.Contains(diagnostic.String(), name) {
t.Fatalf("%v: %+v %q", args, outcome, diagnostic.String())
}
}
for _, scope := range []string{"app", "promptkit"} {
var out, diagnostic bytes.Buffer
outcome := Execute(context.Background(), commandkit.Identity{Name: "scriptorium"}, []string{"config", "show", "--scope=" + scope, "--config=" + path, `--set=/promptkit/custom="selected"`}, &out, &diagnostic)
if outcome.Status != 0 || !strings.Contains(out.String(), "selected") || os.Getenv(name) != "dummy" {
t.Fatalf("%s: %+v %q %q", scope, outcome, out.String(), diagnostic.String())
}
}
}

View File

@@ -1,41 +1,18 @@
package cli
import (
"context"
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/ckprompt"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
)
func configurationCommand() commandkit.Command {
application := ckconfig.FromConfigKit("app", "Scriptorium application configuration.",
func(ctx context.Context, selection ckconfig.Selection, input applicationInput, _ *acquisitionState) (*configkit.Result[appconfig.Document], error) {
return loadApplication(ctx, selection, input, struct{}{})
})
application := ckconfig.FromConfigKit("app", "Scriptorium application configuration.", loadApplication)
framework := ckconfig.FromConfigKitSubtree("promptkit", "PromptKit configuration.", configkit.Keys("promptkit"), loadApplication)
application.Features.Overrides = true
framework := frameworkConfigurationScope()
return ckconfig.Command(ckconfig.Suite[applicationInput, *acquisitionState]{
framework.Features.Overrides = true
return ckconfig.Command(ckconfig.Suite[applicationInput, struct{}]{
NewInput: func() applicationInput { return applicationInput{} },
NewState: func() *acquisitionState { return &acquisitionState{} },
Scopes: []ckconfig.Scope[applicationInput, *acquisitionState]{application, framework},
Scopes: []ckconfig.Scope[applicationInput, struct{}]{application, framework},
})
}
func frameworkConfigurationScope() ckconfig.Scope[applicationInput, *acquisitionState] {
scope := ckprompt.ConfigScope(loadEngine)
acquire, compare := scope.Acquire, scope.Compare
scope.Acquire = func(ctx context.Context, request ckconfig.Request, input applicationInput, state *acquisitionState) (commandkit.Acquired[ckconfig.View], error) {
start := state.mark()
acquired, err := acquire(ctx, request, input, state)
return attachApplicationEvidence(acquired, err, state, start, false)
}
scope.Compare = func(ctx context.Context, request ckconfig.ComparisonRequest[applicationInput], state *acquisitionState) (commandkit.Acquired[[]configkit.Difference], error) {
start := state.mark()
acquired, err := compare(ctx, request, state)
return attachApplicationEvidence(acquired, err, state, start, true)
}
return scope
}

View File

@@ -1,40 +1,15 @@
package cli
import (
"context"
"go.maximumdirect.net/ckprompt"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/promptkit"
)
func promptSuite() ckprompt.Suite[applicationInput, *acquisitionState] {
func promptSuite() ckprompt.Suite[applicationInput, struct{}] {
suite := ckprompt.FromEngine(loadEngine)
suite.NewInput = func() applicationInput { return applicationInput{} }
suite.NewState = func() *acquisitionState { return &acquisitionState{} }
suite = ckprompt.WithRequestValidation(suite, loadEngine, nil)
suite = ckprompt.WithRequestPreparation(suite, loadEngine, nil)
suite = ckprompt.WithSmokeTesting(suite, loadEngine, nil)
suite = ckprompt.WithNamespaces(suite, promptkit.Namespaces{})
suite.ListPrompts = withApplicationEvidence(suite.ListPrompts)
suite.ListProfiles = withApplicationEvidence(suite.ListProfiles)
suite.ListBackends = withApplicationEvidence(suite.ListBackends)
suite.ListNamespaces = withApplicationEvidence(suite.ListNamespaces)
suite.ListCollections = withApplicationEvidence(suite.ListCollections)
suite.InspectPrompt = withApplicationEvidence(suite.InspectPrompt)
suite.InspectProfile = withApplicationEvidence(suite.InspectProfile)
suite.InspectBackend = withApplicationEvidence(suite.InspectBackend)
suite.InspectRequirements = withApplicationEvidence(suite.InspectRequirements)
suite.ValidateRequest = withApplicationEvidence(suite.ValidateRequest)
suite.PrepareRequest = withApplicationEvidence(suite.PrepareRequest)
suite.SmokeTest = withApplicationEvidence(suite.SmokeTest)
return suite
}
func withApplicationEvidence[R, V any](callback func(context.Context, R, applicationInput, *acquisitionState) (commandkit.Acquired[V], error)) func(context.Context, R, applicationInput, *acquisitionState) (commandkit.Acquired[V], error) {
return func(ctx context.Context, request R, input applicationInput, state *acquisitionState) (commandkit.Acquired[V], error) {
start := state.mark()
acquired, err := callback(ctx, request, input, state)
return attachApplicationEvidence(acquired, err, state, start, false)
}
return ckprompt.WithNamespaces(suite, promptkit.Namespaces{})
}

View File

@@ -2,7 +2,6 @@ package cli
import (
"context"
"errors"
"strconv"
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
@@ -91,22 +90,8 @@ func loadApplication(ctx context.Context, selection ckconfig.Selection, input ap
return result, nil
}
func loadEngine(ctx context.Context, selection ckconfig.Selection, input applicationInput, state *acquisitionState) (*promptkit.Engine, error) {
func loadEngine(ctx context.Context, selection ckconfig.Selection, input applicationInput, _ struct{}) (*promptkit.Engine, error) {
result, err := loadApplication(ctx, selection, input, struct{}{})
if state != nil {
evidence := applicationAcquisition{err: err}
if result != nil {
evidence.sources, evidence.records = result.Sources(), result.Diagnostics()
}
var native *configkit.LoadError
if errors.As(err, &native) && native != nil {
evidence.sources, evidence.records = native.Sources(), native.Diagnostics()
}
state.applications = append(state.applications, evidence)
if err != nil {
return nil, errApplicationAcquisition
}
}
if err != nil {
return nil, err
}