make depfn optional

This commit is contained in:
Elijah Duffy
2025-06-09 13:03:18 -07:00
parent 1d648a842b
commit a1e0a94cdc

View File

@@ -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)
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 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.