Skip to content

Commit 056ffa4

Browse files
committed
main: fix --verbose logging
rootCmd.PersistentFlags().GetBool("verbose") always returned false prior this commit because flags are not parse yet. This happens only when rootCmd.Execute() is called. Fix this by using one of the cobra hooks that runs after flags are parsed. Note that the hook is needed because we want to enable verbose logging for all subcommands. Otherwise, we would have to enable it in every single subcommand. I also added a small log message that actually informs users that verbose logging is enabled.
1 parent 13a177b commit 056ffa4

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

cmd/image-builder/main.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -646,14 +646,19 @@ operating systems like Fedora, CentOS and RHEL with easy customizations support.
646646

647647
rootCmd.AddCommand(describeImgCmd)
648648

649-
verbose, err := rootCmd.PersistentFlags().GetBool("verbose")
650-
if err != nil {
651-
return err
652-
}
653-
if verbose {
654-
olog.SetDefault(log.New(os.Stderr, "", 0))
655-
// XXX: add once images has olog support
656-
//images_log.SetDefault(log.New(os.Stderr, "", 0))
649+
// Flags are not parsed yet, so we need to use this hook for enabling verbose logging for all commands
650+
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
651+
verbose, err := cmd.Flags().GetBool("verbose")
652+
if err != nil {
653+
return err
654+
}
655+
if verbose {
656+
olog.SetDefault(log.New(os.Stderr, "", 0))
657+
olog.Print("verbose logging enabled")
658+
// XXX: add once images has olog support
659+
//images_log.SetDefault(log.New(os.Stderr, "", 0))
660+
}
661+
return nil
657662
}
658663

659664
return rootCmd.Execute()

0 commit comments

Comments
 (0)