From 98f90034ec8fcb059acf57c527d250aa49d99983 Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Mon, 6 Jun 2022 20:10:24 +0300 Subject: [PATCH] fix(helm): plugins positional arguments not passed properly Signed-off-by: Timofey Kirillov --- cmd/werf/helm/helm.go | 38 +++++++++++++++++++++++++++++++------- cmd/werf/main.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/cmd/werf/helm/helm.go b/cmd/werf/helm/helm.go index b07f29cf03..cdf37c6b06 100644 --- a/cmd/werf/helm/helm.go +++ b/cmd/werf/helm/helm.go @@ -32,6 +32,10 @@ import ( var _commonCmdData common.CmdData +func IsHelm3Mode() bool { + return os.Getenv("WERF_HELM3_MODE") == "1" +} + func NewCmd() *cobra.Command { var namespace string actionConfig := new(action.Configuration) @@ -99,7 +103,19 @@ func NewCmd() *cobra.Command { helm_v3.NewRegistryCmd(actionConfig, os.Stdout), ) - helm_v3.LoadPlugins(cmd, os.Stdout) + if IsHelm3Mode() { + helm_v3.LoadPlugins(cmd, os.Stdout) + } else { + func() { + saveArgs := os.Args + os.Args = os.Args[1:] + defer func() { + os.Args = saveArgs + }() + + helm_v3.LoadPlugins(cmd, os.Stdout) + }() + } commandsQueue := []*cobra.Command{cmd} for len(commandsQueue) > 0 { @@ -110,7 +126,11 @@ func NewCmd() *cobra.Command { if cmd.Runnable() { oldRunE := cmd.RunE + cmd.RunE = nil + oldRun := cmd.Run + cmd.Run = nil + cmd.RunE = func(cmd *cobra.Command, args []string) error { // NOTE: Common init block for all runnable commands. @@ -160,13 +180,11 @@ func NewCmd() *cobra.Command { ReleasesHistoryMax: *_commonCmdData.ReleasesHistoryMax, }) - if oldRun != nil { - oldRun(cmd, args) - return nil - } else { + if oldRunE != nil { if err := oldRunE(cmd, args); err != nil { - errValue := reflect.ValueOf(err) - if errValue.Kind() == reflect.Struct { + if helm_v3.IsPluginError(err) { + return err + } else if errValue := reflect.ValueOf(err); errValue.Kind() == reflect.Struct { if !errValue.IsZero() { codeValue := errValue.FieldByName("code") if codeValue.IsValid() && !codeValue.IsZero() { @@ -179,7 +197,13 @@ func NewCmd() *cobra.Command { } return nil + } else if oldRun != nil { + oldRun(cmd, args) + return nil + } else { + panic(fmt.Sprintf("unexpected command %q, please report bug to the https://github.com/werf/werf", cmd.Name())) } + } } } diff --git a/cmd/werf/main.go b/cmd/werf/main.go index 639a8a8ce0..8af4fa53a8 100644 --- a/cmd/werf/main.go +++ b/cmd/werf/main.go @@ -75,7 +75,7 @@ func main() { } func constructRootCmd() *cobra.Command { - if filepath.Base(os.Args[0]) == "helm" || os.Getenv("WERF_HELM3_MODE") == "1" { + if filepath.Base(os.Args[0]) == "helm" || helm.IsHelm3Mode() { return helm.NewCmd() } diff --git a/go.mod b/go.mod index 7554806f96..1ff2725cff 100644 --- a/go.mod +++ b/go.mod @@ -307,6 +307,6 @@ replace k8s.io/helm => github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f replace github.com/deislabs/oras => github.com/werf/third-party-oras v0.9.1-0.20210927171747-6d045506f4c8 -replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220603120405-dcb207cdbe59 +replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220607090441-92d1c219cc69 replace github.com/go-git/go-git/v5 => github.com/ZauberNerd/go-git/v5 v5.4.3-0.20220315170230-29ec1bc1e5db diff --git a/go.sum b/go.sum index 771c6a3088..9db48c85c4 100644 --- a/go.sum +++ b/go.sum @@ -2032,8 +2032,8 @@ github.com/vmware/govmomi v0.20.3/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59b github.com/weppos/publicsuffix-go v0.4.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k= github.com/weppos/publicsuffix-go v0.5.0 h1:rutRtjBJViU/YjcI5d80t4JAVvDltS6bciJg2K1HrLU= github.com/weppos/publicsuffix-go v0.5.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k= -github.com/werf/3p-helm/v3 v3.0.0-20220603120405-dcb207cdbe59 h1:QLZEoS+9ucXjhzK0UR8L7P8oYi7S5KyCxk6clmOIPlI= -github.com/werf/3p-helm/v3 v3.0.0-20220603120405-dcb207cdbe59/go.mod h1:NxtE2KObf2PrzDl6SIamPFPKyAqWi10iWuvKlQn/Yao= +github.com/werf/3p-helm/v3 v3.0.0-20220607090441-92d1c219cc69 h1:Ri3CCX9UOThJ7Ie5MGV0dOgomgsDu+2VTPDa4XP09yw= +github.com/werf/3p-helm/v3 v3.0.0-20220607090441-92d1c219cc69/go.mod h1:NxtE2KObf2PrzDl6SIamPFPKyAqWi10iWuvKlQn/Yao= github.com/werf/copy-recurse v0.2.4 h1:kEyGUKhgS8WdEOjInNQKgk4lqPWzP2AgR27F3dcGsVc= github.com/werf/copy-recurse v0.2.4/go.mod h1:KVHSQ90p19xflWW0B7BJhLBwmSbEtuxIaBnjlUYRPhk= github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f h1:81YscYTF9mmTf0ULOsCmm42YWQp+qWDzWi1HjWniZrg=