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. 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 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]) 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} }