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>
1.4 KiB
1.4 KiB
appcli
Bridge between urfave/cli/v3 and app lifecycles.
Install
go get gitea.auvem.com/go-toolkit/appcli
Example
func main() {
lc := app.NewLifecycle()
defer lc.Teardown()
root := appcli.NewRootCommand(&cli.Command{
Name: "myapp",
Usage: "My application",
Commands: []*cli.Command{
appcli.NewCommand(&cli.Command{
Name: "serve",
Usage: "Run the server",
Action: func(ctx context.Context, c *cli.Command) error {
// modules already loaded via DepFn
return nil
},
}, func(l *app.Lifecycle, c *cli.Command) ([]*app.Module, error) {
return []*app.Module{myModule}, nil
}),
},
})
if err := appcli.Run(context.Background(), lc, root, os.Args); err != nil {
log.Fatal(err)
}
}
NewRootCommand vs NewCommand
NewRootCommandadds global--verbose/-v(unless already defined)- Both wrap
Beforeto calllifecycle.Requirewith modules fromDepFn - Wrapped commands are copied; the original
*cli.Commandis not mutated
DepFn
The *cli.Command argument is the command whose Before hook is running. For nested commands, parent hooks receive the parent command; use VerboseFromCommand to read root flags from subcommands.