make depfn optional
This commit is contained in:
29
appcli.go
29
appcli.go
@@ -28,8 +28,8 @@ func DepList(deps ...*app.Module) DepFn {
|
||||
// is overriden to ensure that all dependencies are satisfied before the command
|
||||
// is executed. Requires an app.Lifecycle to be present in the context when the
|
||||
// command is executed.
|
||||
func NewCommand(cmdcfg *cli.Command, depfn DepFn) *cli.Command {
|
||||
if depfn == nil {
|
||||
func NewCommand(cmdcfg *cli.Command, depfn ...DepFn) *cli.Command {
|
||||
if len(depfn) == 0 {
|
||||
return cmdcfg
|
||||
}
|
||||
|
||||
@@ -49,14 +49,25 @@ func NewCommand(cmdcfg *cli.Command, depfn DepFn) *cli.Command {
|
||||
return ctx, errors.New("lifecycle not found in context, cannot run command with dependencies")
|
||||
}
|
||||
|
||||
deps, err := depfn(lifecycle, cmd)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
deps := make([]*app.Module, 0)
|
||||
for _, fn := range depfn {
|
||||
if fn == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
modules, err := fn(lifecycle, cmd)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
deps = append(deps, modules...)
|
||||
}
|
||||
|
||||
if err := lifecycle.Require(deps...); err != nil {
|
||||
return ctx, err
|
||||
if len(deps) > 0 {
|
||||
if err := lifecycle.Require(deps...); err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
@@ -66,13 +77,13 @@ func NewCommand(cmdcfg *cli.Command, depfn DepFn) *cli.Command {
|
||||
// NewRootCommand creates a new root CLI command with the specified configuration.
|
||||
// Adds verbose flag and override Before and After methods to handle dependencies.
|
||||
// See NewCommand for more details.
|
||||
func NewRootCommand(cmdcfg *cli.Command, depfn DepFn) *cli.Command {
|
||||
func NewRootCommand(cmdcfg *cli.Command, depfn ...DepFn) *cli.Command {
|
||||
cmdcfg.Flags = append(cmdcfg.Flags, &cli.BoolFlag{
|
||||
Name: "verbose",
|
||||
Aliases: []string{"v"},
|
||||
Usage: "Enable verbose output",
|
||||
})
|
||||
return NewCommand(cmdcfg, depfn)
|
||||
return NewCommand(cmdcfg, depfn...)
|
||||
}
|
||||
|
||||
// VerboseFromCommand checks if the verbose flag is set in the command context.
|
||||
|
||||
Reference in New Issue
Block a user