99 lines
4.2 KiB
Go
99 lines
4.2 KiB
Go
package commandkit
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"go.maximumdirect.net/commandkit/internal/commandmeta"
|
|
)
|
|
|
|
// InvocationFeature is a captured, reusable invocation-wide capability.
|
|
// Attach it to an installed command with WithInvocationFeature.
|
|
type InvocationFeature struct{ record *invocationFeatureRecord }
|
|
|
|
// InvocationFeatureSpec declares invocation flags and a report producer.
|
|
// NewInput creates fresh input for each invocation; nil uses the zero value.
|
|
// Start receives the current invocation and may return a replacement context
|
|
// and a function producing a report at invocation finalization. Start is required.
|
|
// Features start in installation traversal order after command input construction
|
|
// and successful CheckInput; each receives the context returned by its predecessor.
|
|
// A nil context leaves it unchanged and a nil report function produces no report.
|
|
// Report functions run once after deferred messages and before terminal explanations,
|
|
// even after a returned failure or cancellation. Reports render as text on
|
|
// diagnostics, with quiet suppressing delivery. Generated help, version and
|
|
// completion, and preflight failures do not start features.
|
|
type InvocationFeatureSpec[I any] struct {
|
|
NewInput func() I
|
|
Flags []Flag[I]
|
|
Start func(Invocation, I) (context.Context, func() Report)
|
|
}
|
|
|
|
type invocationFeaturePayload interface {
|
|
bridgeFlags() []commandmeta.Flag
|
|
requiredError([][]string) error
|
|
prepare([][]string, [][]string, []commandmeta.FlagOccurrence, bool) (func(Invocation) (context.Context, func() Report), error)
|
|
}
|
|
|
|
type typedInvocationFeature[I any] struct{ spec InvocationFeatureSpec[I] }
|
|
|
|
func (f typedInvocationFeature[I]) bridgeFlags() []commandmeta.Flag {
|
|
return (typedCommand[I]{spec: CommandSpec[I]{Flags: f.spec.Flags}}).bridgeFlags()
|
|
}
|
|
|
|
func (f typedInvocationFeature[I]) requiredError(values [][]string) error {
|
|
for i, flag := range f.spec.Flags {
|
|
if flag.binding.meta.Required && !flag.binding.deferredRequired && len(values[i]) == 0 {
|
|
return fmt.Errorf("required flag --%s was not supplied", flag.binding.meta.Name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f typedInvocationFeature[I]) prepare(values, spellings [][]string, occurrences []commandmeta.FlagOccurrence, required bool) (func(Invocation) (context.Context, func() Report), error) {
|
|
input, err := prepareInputWithOccurrences(CommandSpec[I]{NewInput: f.spec.NewInput, Flags: f.spec.Flags}, values, spellings, occurrences, nil, required)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return func(inv Invocation) (context.Context, func() Report) { return f.spec.Start(inv, input.build()) }, nil
|
|
}
|
|
|
|
type invocationFeatureRecord struct {
|
|
payload invocationFeaturePayload
|
|
issues []error
|
|
}
|
|
|
|
// NewInvocationFeature captures flag declarations and callbacks without invoking
|
|
// them. Construction errors are reported when an attached command is installed.
|
|
func NewInvocationFeature[I any](spec InvocationFeatureSpec[I]) InvocationFeature {
|
|
spec.Flags = append([]Flag[I](nil), spec.Flags...)
|
|
record := &invocationFeatureRecord{payload: typedInvocationFeature[I]{spec: spec}}
|
|
if err := validateBindings(spec.Flags, nil); err != nil {
|
|
record.issues = append(record.issues, err)
|
|
}
|
|
for _, flag := range spec.Flags {
|
|
if commandmeta.IsReportingFlag(flag.binding.meta.Name) {
|
|
record.issues = append(record.issues, fmt.Errorf("feature flag name %q is reserved for reporting", flag.binding.meta.Name))
|
|
}
|
|
}
|
|
if spec.Start == nil {
|
|
record.issues = append(record.issues, fmt.Errorf("invocation feature requires Start"))
|
|
}
|
|
return InvocationFeature{record: record}
|
|
}
|
|
|
|
// WithInvocationFeature returns a copy of command with feature attached. When
|
|
// installed, the feature applies to the application, not only this command.
|
|
// Reusing the same feature in one application installs it once. An absent
|
|
// command contributes no feature, but attached errors are still reported.
|
|
func WithInvocationFeature(command Command, feature InvocationFeature) Command {
|
|
var record commandRecord
|
|
if command.record == nil {
|
|
record.invalid = true
|
|
record.issues = append(record.issues, fmt.Errorf("invalid zero command declaration"))
|
|
} else {
|
|
record = *command.record
|
|
}
|
|
record.features = append(append([]InvocationFeature(nil), record.features...), feature)
|
|
return Command{record: &record}
|
|
}
|