Skip to content

Commit

Permalink
fix(helm): plugins positional arguments not passed properly
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 7, 2022
1 parent b2cec4b commit 98f9003
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
38 changes: 31 additions & 7 deletions cmd/werf/helm/helm.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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.

Expand Down Expand Up @@ -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() {
Expand All @@ -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()))
}

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/main.go
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -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=
Expand Down

0 comments on commit 98f9003

Please sign in to comment.