From d41162078a3b8ce8cb222b68aea89a07df5b375a Mon Sep 17 00:00:00 2001 From: Arik Kfir Date: Thu, 16 May 2024 22:34:39 +0300 Subject: [PATCH] fix(presubcommandrun): fix panic when parent-hook is not specified This fix prevents a panic when a command spec does not provide a value for the `PreSubCommandRun` hook. --- command.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/command.go b/command.go index 9a7d23b..6a6674a 100644 --- a/command.go +++ b/command.go @@ -444,9 +444,11 @@ func Execute(ctx context.Context, w io.Writer, root *Command, args []string, env return 1 } - if err := current.PreSubCommandRun(ctx, current.Config, current); err != nil { - _, _ = fmt.Fprintln(w, err.Error()) - return 1 + if current.PreSubCommandRun != nil { + if err := current.PreSubCommandRun(ctx, current.Config, current); err != nil { + _, _ = fmt.Fprintln(w, err.Error()) + return 1 + } } }