From a1e0a94cdc66f5f37924d4b1a2e2583d37e598a2 Mon Sep 17 00:00:00 2001 From: Elijah Duffy Date: Mon, 9 Jun 2025 13:03:18 -0700 Subject: [PATCH] make depfn optional --- appcli.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/appcli.go b/appcli.go index 36af343..d676465 100644 --- a/appcli.go +++ b/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.