From 022d4db6af8d6d0307164be41a0c1cb68ea12ec0 Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Mon, 3 Mar 2025 18:43:03 +0000 Subject: [PATCH] move logic from cmd/helm to pkg/cmd Signed-off-by: Austin Abro --- cmd/helm/helm.go | 4 +--- pkg/cmd/helpers_test.go | 2 +- pkg/cmd/root.go | 31 ++++++++++++++++++++----------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 538aa9ed8..da6a5c54e 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -23,7 +23,6 @@ import ( // Import to initialize client auth plugins. _ "k8s.io/client-go/plugin/pkg/client/auth" - "helm.sh/helm/v4/pkg/action" helmcmd "helm.sh/helm/v4/pkg/cmd" "helm.sh/helm/v4/pkg/kube" ) @@ -39,8 +38,7 @@ func main() { // manager as picked up by the automated name detection. kube.ManagedFieldsManager = "helm" - actionConfig := new(action.Configuration) - cmd, err := helmcmd.NewRootCmd(actionConfig, os.Stdout, os.Args[1:]) + cmd, err := helmcmd.NewRootCmd(os.Stdout, os.Args[1:]) if err != nil { helmcmd.Warning("%+v", err) os.Exit(1) diff --git a/pkg/cmd/helpers_test.go b/pkg/cmd/helpers_test.go index 5f9f4e769..effbc1673 100644 --- a/pkg/cmd/helpers_test.go +++ b/pkg/cmd/helpers_test.go @@ -95,7 +95,7 @@ func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string) Log: func(_ string, _ ...interface{}) {}, } - root, err := NewRootCmd(actionConfig, buf, args) + root, err := newRootCmdWithConfig(actionConfig, buf, args) if err != nil { return nil, "", err } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index c035181e3..ea686be7c 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -106,7 +106,26 @@ func Warning(format string, v ...interface{}) { fmt.Fprintf(os.Stderr, "WARNING: "+format+"\n", v...) } -func NewRootCmd(actionConfig *action.Configuration, out io.Writer, args []string) (*cobra.Command, error) { +func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) { + actionConfig := new(action.Configuration) + cmd, err := newRootCmdWithConfig(actionConfig, out, args) + if err != nil { + return nil, err + } + cobra.OnInitialize(func() { + helmDriver := os.Getenv("HELM_DRIVER") + if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), helmDriver, Debug); err != nil { + log.Fatal(err) + } + if helmDriver == "memory" { + loadReleasesInMemory(actionConfig) + } + actionConfig.SetHookOutputFunc(hookOutputWriter) + }) + return cmd, nil +} + +func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, args []string) (*cobra.Command, error) { cmd := &cobra.Command{ Use: "helm", Short: "The Helm package manager for Kubernetes.", @@ -124,16 +143,6 @@ func NewRootCmd(actionConfig *action.Configuration, out io.Writer, args []string }, } - cobra.OnInitialize(func() { - helmDriver := os.Getenv("HELM_DRIVER") - if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), helmDriver, Debug); err != nil { - log.Fatal(err) - } - if helmDriver == "memory" { - loadReleasesInMemory(actionConfig) - } - actionConfig.SetHookOutputFunc(hookOutputWriter) - }) flags := cmd.PersistentFlags() settings.AddFlags(flags)