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 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 // is executed. Requires an app.Lifecycle to be present in the context when the
// command is executed. // command is executed.
func NewCommand(cmdcfg *cli.Command, depfn DepFn) *cli.Command { func NewCommand(cmdcfg *cli.Command, depfn ...DepFn) *cli.Command {
if depfn == nil { if len(depfn) == 0 {
return cmdcfg 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") return ctx, errors.New("lifecycle not found in context, cannot run command with dependencies")
} }
deps, err := depfn(lifecycle, cmd) deps := make([]*app.Module, 0)
if err != nil { for _, fn := range depfn {
return ctx, err 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 { if len(deps) > 0 {
return ctx, err if err := lifecycle.Require(deps...); err != nil {
return ctx, err
}
} }
return ctx, nil 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. // NewRootCommand creates a new root CLI command with the specified configuration.
// Adds verbose flag and override Before and After methods to handle dependencies. // Adds verbose flag and override Before and After methods to handle dependencies.
// See NewCommand for more details. // 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{ 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) return NewCommand(cmdcfg, 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.