From 9791767baa9d3e4d460c25e921f06172b2cec53f Mon Sep 17 00:00:00 2001 From: Chris Berry Date: Fri, 21 Feb 2025 16:16:26 +0000 Subject: [PATCH] Refactor based on review comment Signed-off-by: Chris Berry --- cmd/helm/helm.go | 7 ++++++- cmd/helm/list.go | 2 +- pkg/action/action.go | 19 ++++++++++--------- pkg/action/action_test.go | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index aa981740f..3d5c17030 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -57,6 +57,11 @@ func warning(format string, v ...interface{}) { fmt.Fprintf(os.Stderr, format, v...) } +// hookOutputWriter provides the writer for writing hook logs. +func hookOutputWriter(_, _, _ string) io.Writer { + return log.Writer() +} + func main() { // Setting the name of the app for managedFields in the Kubernetes client. // It is set here to the full name of "helm" so that renaming of helm to @@ -74,7 +79,7 @@ func main() { // run when each command's execute method is called cobra.OnInitialize(func() { helmDriver := os.Getenv("HELM_DRIVER") - if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), helmDriver, debug); err != nil { + if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), helmDriver, debug, hookOutputWriter); err != nil { log.Fatal(err) } if helmDriver == "memory" { diff --git a/cmd/helm/list.go b/cmd/helm/list.go index 67da22cdf..f9bf336d4 100644 --- a/cmd/helm/list.go +++ b/cmd/helm/list.go @@ -71,7 +71,7 @@ func newListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { ValidArgsFunction: noMoreArgsCompFunc, RunE: func(cmd *cobra.Command, _ []string) error { if client.AllNamespaces { - if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil { + if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug, nil); err != nil { return err } } diff --git a/pkg/action/action.go b/pkg/action/action.go index 0d81a63cb..dfb263269 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "io" - "log" "os" "path" "path/filepath" @@ -98,7 +97,7 @@ type Configuration struct { Log func(string, ...interface{}) - // HookOutputFunc Called with container name and returns and expects writer that will receive the log output + // HookOutputFunc called with container name and returns and expects writer that will receive the log output. HookOutputFunc func(namespace, pod, container string) io.Writer } @@ -247,6 +246,9 @@ type RESTClientGetter interface { // DebugLog sets the logger that writes debug strings type DebugLog func(format string, v ...interface{}) +// HookOutputFunc returns the io.Writer for outputting hook logs. +type HookOutputFunc func(namespace, pod, container string) io.Writer + // capabilities builds a Capabilities from discovery information. func (cfg *Configuration) getCapabilities() (*chartutil.Capabilities, error) { if cfg.Capabilities != nil { @@ -375,7 +377,7 @@ func (cfg *Configuration) recordRelease(r *release.Release) { } // Init initializes the action configuration -func (cfg *Configuration) Init(getter genericclioptions.RESTClientGetter, namespace, helmDriver string, log DebugLog) error { +func (cfg *Configuration) Init(getter genericclioptions.RESTClientGetter, namespace, helmDriver string, log DebugLog, outputFunc HookOutputFunc) error { kc := kube.New(getter) kc.Log = log @@ -427,12 +429,11 @@ func (cfg *Configuration) Init(getter genericclioptions.RESTClientGetter, namesp cfg.KubeClient = kc cfg.Releases = store cfg.Log = log - cfg.HookOutputFunc = defaultHookOutputWriter + if outputFunc != nil { + cfg.HookOutputFunc = outputFunc + } else { + cfg.HookOutputFunc = func(_, _, _ string) io.Writer { return io.Discard } + } return nil } - -// defaultHookOutputWriter will write the Hook logs to log.Writer(). -func defaultHookOutputWriter(_, _, _ string) io.Writer { - return log.Writer() -} diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 47cff6ec1..2d1516edb 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -334,7 +334,7 @@ func TestConfiguration_Init(t *testing.T) { t.Run(tt.name, func(t *testing.T) { cfg := &Configuration{} - actualErr := cfg.Init(nil, "default", tt.helmDriver, nil) + actualErr := cfg.Init(nil, "default", tt.helmDriver, nil, nil) if tt.expectErr { assert.Error(t, actualErr) assert.Contains(t, actualErr.Error(), tt.errMsg)