From 9e0eec84f7f6b24a8113a557ef7e3372472bb4bc Mon Sep 17 00:00:00 2001 From: Grant Spence Date: Thu, 25 Jul 2024 05:10:08 -0400 Subject: [PATCH] Return exit code 1 on failure and silence redundant cobra errors (#86) * Return Exit Code 1 on Failure Previously, the code returned exit code 0 even when the template generation failed. Now, if an error occurs, the program will return exit code 1. * Silence Errors from Cobra Execution Previously, Cobra was printing non-formatted errors. However, we already log these errors via zap. This fix silences Cobra from printing redundant logs. --- main.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index b63410a..caaf38c 100644 --- a/main.go +++ b/main.go @@ -34,11 +34,12 @@ var args = config.Flags{} func main() { cmd := cobra.Command{ - Use: "crd-ref-docs", - Short: "Generate CRD reference documentation", - SilenceUsage: true, - Version: version(), - RunE: doRun, + Use: "crd-ref-docs", + Short: "Generate CRD reference documentation", + SilenceUsage: true, + SilenceErrors: true, + Version: version(), + RunE: doRun, } cmd.SetVersionTemplate("{{ .Version }}\n") @@ -52,7 +53,9 @@ func main() { cmd.Flags().StringVar(&args.OutputMode, "output-mode", "single", "Output mode to generate a single file or one file per group ('group' or 'single')") cmd.Flags().IntVar(&args.MaxDepth, "max-depth", 10, "Maximum recursion level for type discovery") - cmd.Execute() + if err := cmd.Execute(); err != nil { + os.Exit(1) + } } func doRun(_ *cobra.Command, _ []string) error {