Add PromptKit configuration scope
This commit is contained in:
54
promptkit_scope.go
Normal file
54
promptkit_scope.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package commandkit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
// NewPromptKitScope adapts PromptKit's captured configuration inspection to
|
||||
// CommandKit's configuration commands.
|
||||
func NewPromptKitScope[C any](
|
||||
load func(context.Context, Selection, C) (*promptkit.Engine, error),
|
||||
) (ConfigScope[C], error) {
|
||||
if load == nil {
|
||||
return ConfigScope[C]{}, fmt.Errorf("PromptKit configuration loader is nil")
|
||||
}
|
||||
|
||||
return ConfigScope[C]{
|
||||
Name: "promptkit",
|
||||
Summary: "PromptKit engine configuration.",
|
||||
Operations: []ConfigOperation{ConfigValidate, ConfigShow, ConfigSources, ConfigExplain},
|
||||
Acquire: func(ctx context.Context, request ConfigRequest, appContext C) (Acquired[ConfigView], error) {
|
||||
engine, err := load(ctx, request.Selection, appContext)
|
||||
if err != nil {
|
||||
var loadErr *configkit.LoadError
|
||||
if errors.As(err, &loadErr) {
|
||||
return Acquired[ConfigView]{Diagnostics: ConfigKitDiagnostics("promptkit", loadErr.Sources(), loadErr.Diagnostics())}, err
|
||||
}
|
||||
return Acquired[ConfigView]{}, err
|
||||
}
|
||||
if engine == nil {
|
||||
return Acquired[ConfigView]{}, fmt.Errorf("PromptKit configuration loader returned a nil engine")
|
||||
}
|
||||
|
||||
inspection, err := engine.InspectConfiguration()
|
||||
if err != nil {
|
||||
return Acquired[ConfigView]{}, err
|
||||
}
|
||||
sources := inspection.Sources()
|
||||
diagnostics := ConfigKitDiagnostics("promptkit", sources, inspection.Diagnostics())
|
||||
return Acquired[ConfigView]{
|
||||
Value: ConfigView{
|
||||
Effective: inspection,
|
||||
Sources: inspection,
|
||||
Explanation: inspection,
|
||||
},
|
||||
Diagnostics: diagnostics,
|
||||
}, nil
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user