f64f7fa04e
Clone commands before wrapping Before hooks, export ErrLifecycleNotInContext and ErrNoDependencies, add Run helper, split command.go and deps.go. Co-authored-by: Cursor <cursoragent@cursor.com>
38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package appcli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gitea.auvem.com/go-toolkit/app"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
var (
|
|
// ErrLifecycleNotInContext is returned when a command with dependencies runs
|
|
// without a lifecycle in context.
|
|
ErrLifecycleNotInContext = errors.New("lifecycle not found in context, cannot run command with dependencies")
|
|
|
|
// ErrNoDependencies is returned when DepList is called with zero modules.
|
|
ErrNoDependencies = errors.New("no dependencies provided")
|
|
)
|
|
|
|
// DepFn returns modules to load before a command runs. The *cli.Command is the
|
|
// command whose Before hook is executing (may be a parent in nested command trees).
|
|
type DepFn func(*app.Lifecycle, *cli.Command) ([]*app.Module, error)
|
|
|
|
// DepList returns a DepFn that always supplies the given modules.
|
|
func DepList(deps ...*app.Module) DepFn {
|
|
return func(_ *app.Lifecycle, _ *cli.Command) ([]*app.Module, error) {
|
|
if len(deps) == 0 {
|
|
return nil, ErrNoDependencies
|
|
}
|
|
return deps, nil
|
|
}
|
|
}
|
|
|
|
// Run injects lifecycle into ctx and executes cmd with args.
|
|
func Run(ctx context.Context, lc *app.Lifecycle, cmd *cli.Command, args []string) error {
|
|
return cmd.Run(lc.Context(ctx), args)
|
|
}
|