don't take over lifecycle with NewRootCommand
This commit is contained in:
66
appcli.go
66
appcli.go
@@ -63,74 +63,16 @@ func NewCommand(cmdcfg *cli.Command, depfn DepFn) *cli.Command {
|
|||||||
return cmdcfg
|
return cmdcfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// RootCommandOpts defines the options for creating a root command.
|
|
||||||
type RootCommandOpts struct {
|
|
||||||
// Command is the base command configuration.
|
|
||||||
Command *cli.Command
|
|
||||||
|
|
||||||
// DepFn is a function that returns the modules that this command depends
|
|
||||||
// on. Note: All root dependencies will be inherited by all subcommands.
|
|
||||||
DepFn DepFn
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRootCommand creates a new root CLI command with the specified configuration.
|
// NewRootCommand creates a new root CLI command with the specified configuration.
|
||||||
// Override Before and After to take over app.Lifecycle setup and teardown. Adds
|
// Adds verbose flag and override Before and After methods to handle dependencies.
|
||||||
// a verbose flag. See NewCommand for more details on how dependencies are handled.
|
// See NewCommand for more details.
|
||||||
// Requires an app.Lifecycle to be present in the context when the command is executed.
|
func NewRootCommand(cmdcfg *cli.Command, depfn DepFn) *cli.Command {
|
||||||
func NewRootCommand(rootcfg *RootCommandOpts) *cli.Command {
|
|
||||||
cmdcfg := rootcfg.Command
|
|
||||||
|
|
||||||
cmdcfg.Flags = append(cmdcfg.Flags, &cli.BoolFlag{
|
cmdcfg.Flags = append(cmdcfg.Flags, &cli.BoolFlag{
|
||||||
Name: "verbose",
|
Name: "verbose",
|
||||||
Aliases: []string{"v"},
|
Aliases: []string{"v"},
|
||||||
Usage: "Enable verbose output",
|
Usage: "Enable verbose output",
|
||||||
})
|
})
|
||||||
|
return NewCommand(cmdcfg, depfn)
|
||||||
// Override the before method to handle logger and lifecycle
|
|
||||||
originalBefore := cmdcfg.Before
|
|
||||||
cmdcfg.Before = func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
|
||||||
if originalBefore != nil {
|
|
||||||
var err error
|
|
||||||
ctx, err = originalBefore(ctx, cmd)
|
|
||||||
if err != nil {
|
|
||||||
return ctx, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lifecycle := app.LifecycleFromContext(ctx)
|
|
||||||
if lifecycle == nil {
|
|
||||||
return ctx, errors.New("lifecycle not found in context, run root command")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := lifecycle.Setup(); err != nil {
|
|
||||||
return ctx, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Override the After method to handle cleanup if needed
|
|
||||||
originalAfter := cmdcfg.After
|
|
||||||
cmdcfg.After = func(ctx context.Context, cmd *cli.Command) error {
|
|
||||||
if originalAfter != nil {
|
|
||||||
if err := originalAfter(ctx, cmd); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lifecycle := app.LifecycleFromContext(ctx)
|
|
||||||
if lifecycle == nil {
|
|
||||||
return errors.New("lifecycle not found in context, cannot clean up root command")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := lifecycle.Teardown(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewCommand(cmdcfg, rootcfg.DepFn)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerboseFromCommand checks if the verbose flag is set in the command context.
|
// VerboseFromCommand checks if the verbose flag is set in the command context.
|
||||||
|
|||||||
Reference in New Issue
Block a user