From d40ed3b5ad603a48f436f3dccbf248e41e49a677 Mon Sep 17 00:00:00 2001 From: Matthew Fisher Date: Wed, 31 Jul 2019 11:28:28 -0700 Subject: [PATCH] fix(rollback): fix revision argument not being handled The revision argument that was mandatory to `helm rollback` was being ignored. The only way to roll back to an older revision was to run `helm rollback RELEASE --version REVISION`. This change respects that argument and removes the `--version` flag, which was redundant. Signed-off-by: Matthew Fisher --- cmd/helm/rollback.go | 24 ++++++++++++------- cmd/helm/rollback_test.go | 9 +++++-- cmd/helm/testdata/output/rollback-no-args.txt | 4 ++-- .../testdata/output/rollback-no-revision.txt | 1 + 4 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 cmd/helm/testdata/output/rollback-no-revision.txt diff --git a/cmd/helm/rollback.go b/cmd/helm/rollback.go index 9c97a4abd..d9af935d7 100644 --- a/cmd/helm/rollback.go +++ b/cmd/helm/rollback.go @@ -19,6 +19,7 @@ package main import ( "fmt" "io" + "strconv" "time" "github.com/spf13/cobra" @@ -31,32 +32,39 @@ const rollbackDesc = ` This command rolls back a release to a previous revision. The first argument of the rollback command is the name of a release, and the -second is a revision (version) number. To see revision numbers, run -'helm history RELEASE'. +second is a revision (version) number. If this argument is omitted, it will +roll back to the previous release. + +To see revision numbers, run 'helm history RELEASE'. ` func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client := action.NewRollback(cfg) cmd := &cobra.Command{ - Use: "rollback [RELEASE] [REVISION]", + Use: "rollback [REVISION]", Short: "roll back a release to a previous revision", Long: rollbackDesc, - Args: require.ExactArgs(2), + Args: require.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - _, err := client.Run(args[0]) - if err != nil { + if len(args) > 1 { + ver, err := strconv.Atoi(args[1]) + if err != nil { + return fmt.Errorf("could not convert revision to a number: %v", err) + } + client.Version = ver + } + + if _, err := client.Run(args[0]); err != nil { return err } fmt.Fprintf(out, "Rollback was a success! Happy Helming!\n") - return nil }, } f := cmd.Flags() - f.IntVar(&client.Version, "version", 0, "revision number to rollback to (default: rollback to previous release)") f.BoolVar(&client.DryRun, "dry-run", false, "simulate a rollback") f.BoolVar(&client.Recreate, "recreate-pods", false, "performs pods restart for the resource if applicable") f.BoolVar(&client.Force, "force", false, "force resource update through delete/recreate if needed") diff --git a/cmd/helm/rollback_test.go b/cmd/helm/rollback_test.go index 6283f6f20..de0f9b0f1 100644 --- a/cmd/helm/rollback_test.go +++ b/cmd/helm/rollback_test.go @@ -55,8 +55,13 @@ func TestRollbackCmd(t *testing.T) { golden: "output/rollback-wait.txt", rels: rels, }, { - name: "rollback a release without revision", - cmd: "rollback funny-honey", + name: "rollback a release without revision", + cmd: "rollback funny-honey", + golden: "output/rollback-no-revision.txt", + rels: rels, + }, { + name: "rollback a release without release name", + cmd: "rollback", golden: "output/rollback-no-args.txt", rels: rels, wantError: true, diff --git a/cmd/helm/testdata/output/rollback-no-args.txt b/cmd/helm/testdata/output/rollback-no-args.txt index 3fde9d219..a1bc30b7a 100644 --- a/cmd/helm/testdata/output/rollback-no-args.txt +++ b/cmd/helm/testdata/output/rollback-no-args.txt @@ -1,3 +1,3 @@ -Error: "helm rollback" requires 2 arguments +Error: "helm rollback" requires at least 1 argument -Usage: helm rollback [RELEASE] [REVISION] [flags] +Usage: helm rollback [REVISION] [flags] diff --git a/cmd/helm/testdata/output/rollback-no-revision.txt b/cmd/helm/testdata/output/rollback-no-revision.txt new file mode 100644 index 000000000..ae3c6f1c4 --- /dev/null +++ b/cmd/helm/testdata/output/rollback-no-revision.txt @@ -0,0 +1 @@ +Rollback was a success! Happy Helming!