From f0cf9c28f091767d134070cdf230a65bc3d8f618 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Tue, 1 Jul 2025 15:04:30 -0400 Subject: [PATCH] Move logging setup to be configurable Signed-off-by: Matt Farina --- cmd/helm/helm.go | 2 +- pkg/cmd/helpers_test.go | 2 +- pkg/cmd/root.go | 35 +++++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 0e912cda4..05e7e7ba2 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -34,7 +34,7 @@ func main() { // manager as picked up by the automated name detection. kube.ManagedFieldsManager = "helm" - cmd, err := helmcmd.NewRootCmd(os.Stdout, os.Args[1:]) + cmd, err := helmcmd.NewRootCmd(os.Stdout, os.Args[1:], helmcmd.SetupLogging) if err != nil { slog.Warn("command failed", slog.Any("error", err)) os.Exit(1) diff --git a/pkg/cmd/helpers_test.go b/pkg/cmd/helpers_test.go index 5d71fecad..8c06db4ae 100644 --- a/pkg/cmd/helpers_test.go +++ b/pkg/cmd/helpers_test.go @@ -94,7 +94,7 @@ func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string) Capabilities: chartutil.DefaultCapabilities, } - root, err := newRootCmdWithConfig(actionConfig, buf, args) + root, err := newRootCmdWithConfig(actionConfig, buf, args, SetupLogging) if err != nil { return nil, "", err } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index ee22533f0..4eb5da494 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -98,9 +98,9 @@ By default, the default directories depend on the Operating System. The defaults var settings = cli.New() -func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) { +func NewRootCmd(out io.Writer, args []string, logSetup func(bool)) (*cobra.Command, error) { actionConfig := new(action.Configuration) - cmd, err := newRootCmdWithConfig(actionConfig, out, args) + cmd, err := newRootCmdWithConfig(actionConfig, out, args, logSetup) if err != nil { return nil, err } @@ -117,7 +117,19 @@ func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) { return cmd, nil } -func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, args []string) (*cobra.Command, error) { +// SetupLogging sets up Helm logging used by the Helm client. +// This function is passed to the NewRootCmd function to enable logging. Any other +// application that uses the NewRootCmd function to setup all the Helm commands may +// use this function to setup logging or their own. Using a custom logging setup function +// enables applications using Helm commands to integrate with their existing logging +// system. +// The debug argument is the value if Helm is set for debugging (i.e. --debug flag) +func SetupLogging(debug bool) { + logger := logging.NewLogger(func() bool { return debug }) + slog.SetDefault(logger) +} + +func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, args []string, logSetup func(bool)) (*cobra.Command, error) { cmd := &cobra.Command{ Use: "helm", Short: "The Helm package manager for Kubernetes.", @@ -140,8 +152,14 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg settings.AddFlags(flags) addKlogFlags(flags) - logger := logging.NewLogger(func() bool { return settings.Debug }) - slog.SetDefault(logger) + // We can safely ignore any errors that flags.Parse encounters since + // those errors will be caught later during the call to cmd.Execution. + // This call is required to gather configuration information prior to + // execution. + flags.ParseErrorsWhitelist.UnknownFlags = true + flags.Parse(args) + + logSetup(settings.Debug) // Setup shell completion for the namespace flag err := cmd.RegisterFlagCompletionFunc("namespace", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { @@ -190,13 +208,6 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg log.Fatal(err) } - // We can safely ignore any errors that flags.Parse encounters since - // those errors will be caught later during the call to cmd.Execution. - // This call is required to gather configuration information prior to - // execution. - flags.ParseErrorsWhitelist.UnknownFlags = true - flags.Parse(args) - registryClient, err := newDefaultRegistryClient(false, "", "") if err != nil { return nil, err