From eeb7e814b51fe9c1270019f4182d76b88b18a9d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Tue, 5 Sep 2023 14:19:04 +0200 Subject: [PATCH] fix: cli exit cleanly (#8267) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- cmd/cli/kubectl-kyverno/main.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/cli/kubectl-kyverno/main.go b/cmd/cli/kubectl-kyverno/main.go index 53a317825f..ea7033794b 100644 --- a/cmd/cli/kubectl-kyverno/main.go +++ b/cmd/cli/kubectl-kyverno/main.go @@ -11,18 +11,28 @@ import ( ) func main() { - cmd := commands.RootCommand() - configureLogs(cmd) - if err := cmd.Execute(); err != nil { + if err := run(); err != nil { + fmt.Println("Error:", err) os.Exit(1) } } -func configureLogs(cli *cobra.Command) { +func run() error { + cmd := commands.RootCommand() + if err := configureLogs(cmd); err != nil { + return fmt.Errorf("Failed to setup logging (%w)", err) + } + if err := cmd.Execute(); err != nil { + return fmt.Errorf("Failed to execute command (%w)", err) + } + return nil +} + +func configureLogs(cli *cobra.Command) error { logging.InitFlags(nil) if err := logging.Setup(logging.TextFormat, 0); err != nil { - fmt.Println("failed to setup logging", err) - os.Exit(1) + return err } cli.PersistentFlags().AddGoFlagSet(flag.CommandLine) + return nil }