Skip to content

Commit

Permalink
feat(1480): add --version flag to package command
Browse files Browse the repository at this point in the history
This adds a `helm package --version=SEMVER` param that allows users to
set a version during a package operation.

Closes #1480
Closes #1699
  • Loading branch information
technosophos committed Dec 17, 2016
1 parent ff9651b commit a8442cb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/helm/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (
"path/filepath"
"syscall"

"github.com/Masterminds/semver"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"

"k8s.io/helm/cmd/helm/helmpath"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/provenance"
"k8s.io/helm/pkg/repo"
)
Expand All @@ -52,6 +54,7 @@ type packageCmd struct {
path string
key string
keyring string
version string
out io.Writer
home helmpath.Home
}
Expand Down Expand Up @@ -93,6 +96,7 @@ func newPackageCmd(client helm.Interface, out io.Writer) *cobra.Command {
f.BoolVar(&pkg.sign, "sign", false, "use a PGP private key to sign this package")
f.StringVar(&pkg.key, "key", "", "name of the key to use when signing. Used if --sign is true")
f.StringVar(&pkg.keyring, "keyring", defaultKeyring(), "location of a public keyring")
f.StringVar(&pkg.version, "version", "", "set the version on the chart to this semver version")

return cmd
}
Expand All @@ -108,6 +112,16 @@ func (p *packageCmd) run(cmd *cobra.Command, args []string) error {
return err
}

// If version is set, modify the version.
if len(p.version) != 0 {
if err := setVersion(ch, p.version); err != nil {
return err
}
if flagDebug {
fmt.Fprintf(p.out, "Setting version to %s", p.version)
}
}

if filepath.Base(path) != ch.Metadata.Name {
return fmt.Errorf("directory name (%s) and Chart.yaml name (%s) must match", filepath.Base(path), ch.Metadata.Name)
}
Expand Down Expand Up @@ -140,6 +154,17 @@ func (p *packageCmd) run(cmd *cobra.Command, args []string) error {
return err
}

func setVersion(ch *chart.Chart, ver string) error {
// Verify that version is a SemVer, and error out if it is not.
if _, err := semver.NewVersion(ver); err != nil {
return err
}

// Set the version field on the chart.
ch.Metadata.Version = ver
return nil
}

func (p *packageCmd) clearsign(filename string) error {
// Load keyring
signer, err := provenance.NewFromKeyring(p.keyring, p.key)
Expand Down
22 changes: 22 additions & 0 deletions cmd/helm/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,30 @@ import (
"github.com/spf13/cobra"

"k8s.io/helm/cmd/helm/helmpath"
"k8s.io/helm/pkg/proto/hapi/chart"
)

func TestSetVersion(t *testing.T) {
c := &chart.Chart{
Metadata: &chart.Metadata{
Name: "prow",
Version: "0.0.1",
},
}
expect := "1.2.3-beta.5"
if err := setVersion(c, expect); err != nil {
t.Fatal(err)
}

if c.Metadata.Version != expect {
t.Errorf("Expected %q, got %q", expect, c.Metadata.Version)
}

if err := setVersion(c, "monkeyface"); err == nil {
t.Error("Expected bogus version to return an error.")
}
}

func TestPackage(t *testing.T) {

tests := []struct {
Expand Down

0 comments on commit a8442cb

Please sign in to comment.