From 56da4b093ff86e90d407182730ffe98a39cc9094 Mon Sep 17 00:00:00 2001 From: Karl Isenberg Date: Tue, 14 Nov 2023 16:26:29 -0800 Subject: [PATCH] feat: Add klog flags to nomos cli (#1026) --- cmd/nomos/bugreport/bugreport.go | 8 +++----- cmd/nomos/nomos.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/nomos/bugreport/bugreport.go b/cmd/nomos/bugreport/bugreport.go index 7e78f91c9a..54333220bc 100644 --- a/cmd/nomos/bugreport/bugreport.go +++ b/cmd/nomos/bugreport/bugreport.go @@ -15,7 +15,6 @@ package bugreport import ( - "flag" "fmt" "github.com/pkg/errors" @@ -42,10 +41,9 @@ var Cmd = &cobra.Command{ // Don't show usage on error, as argument validation passed. cmd.SilenceUsage = true - // hack to set the hidden variable in klog to also print info statements - // cobra does not expose core golang-style flags - if err := flag.CommandLine.Parse([]string{"--stderrthreshold=0"}); err != nil { - klog.Errorf("could not increase logging verbosity: %v", err) + // Send all logs to STDERR. + if err := cmd.InheritedFlags().Lookup("stderrthreshold").Value.Set("0"); err != nil { + klog.Errorf("failed to increase logging STDERR threshold: %v", err) } cfg, err := restconfig.NewRestConfig(flags.ClientTimeout) diff --git a/cmd/nomos/nomos.go b/cmd/nomos/nomos.go index 9773640960..d97b121a26 100644 --- a/cmd/nomos/nomos.go +++ b/cmd/nomos/nomos.go @@ -51,8 +51,15 @@ func init() { } func main() { - klog.InitFlags(nil) - flag.Parse() + // Use the default flag set, because some libs register flags with init. + fs := flag.CommandLine + + // Register klog flags + klog.InitFlags(fs) + + // Cobra uses the pflag lib, instead of the go flag lib. + // So re-register all go flags as global (aka persistent) pflags. + rootCmd.PersistentFlags().AddGoFlagSet(fs) if err := rootCmd.Execute(); err != nil { os.Exit(1)