Skip to content

Commit

Permalink
allow setting the prerelease and metadata version
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi BEN ABDALLAH committed Feb 28, 2018
1 parent 856bcbd commit 4c217e5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ release:
builds:
- binary: local-chart-version

ldflags: -s -w -extldflags "-static" -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
ldflags: -s -w -extldflags "-static" -X main.Version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}

env:
- CGO_ENABLED=0
Expand Down
72 changes: 28 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package main

import (
"io"
"path/filepath"

"k8s.io/helm/pkg/proto/hapi/chart"

"github.com/spf13/cobra"
"k8s.io/helm/pkg/chartutil"

"fmt"
"io"
"os"

"github.com/Masterminds/semver"
"github.com/mbenabda/helm-local-chart-version/pkg/chartfile"
"github.com/mbenabda/helm-local-chart-version/pkg/version"
"github.com/spf13/cobra"
)

// Version identifier populated via the CI/CD process.
Expand All @@ -24,9 +19,11 @@ type getVersionCommand struct {
}

type setVersionCommand struct {
chart string
version string
out io.Writer
chart string
version string
prerelease string
metadata string
out io.Writer
}

type bumpVersionCommand struct {
Expand All @@ -36,66 +33,52 @@ type bumpVersionCommand struct {
}

func (c *getVersionCommand) run() error {
chart, err := chartutil.Load(c.chart)
chart, err := chartfile.Load(c.chart)
if err != nil {
return err
}

fmt.Fprint(c.out, chart.Metadata.Version)
fmt.Fprint(c.out, chart.Version)

return nil
}

func (c *setVersionCommand) run() error {
chart, err := chartutil.Load(c.chart)
chart, err := chartfile.Load(c.chart)
if err != nil {
return err
}

chart.Metadata.Version = c.version

return writeChartFile(chart, c.chart)
}

func writeChartFile(c *chart.Chart, dest string) error {
return chartutil.SaveChartfile(filepath.Join(dest, "Chart.yaml"), c.Metadata)
}

func incrementVersion(version string, segment string) (string, error) {
v1, err := semver.NewVersion(version)
if err != nil {
return "", err
var baseVersion string
if c.version != "" {
baseVersion = c.version
} else {
baseVersion = chart.Version
}

var v2 semver.Version
switch segment {
case "patch":
v2 = v1.IncPatch()
case "minor":
v2 = v1.IncMinor()
case "major":
v2 = v1.IncMajor()
default:
return "", fmt.Errorf("Unknown version segment %s", segment)
finalVersion, err := version.Assemble(baseVersion, c.prerelease, c.metadata)
if err != nil {
return err
}

return v2.String(), nil
chart.Version = finalVersion
return chartfile.Save(chart, c.chart)
}

func (c *bumpVersionCommand) run() error {
chart, err := chartutil.Load(c.chart)
chart, err := chartfile.Load(c.chart)
if err != nil {
return err
}

incrementedVersion, err := incrementVersion(chart.Metadata.Version, c.segment)
incrementedVersion, err := version.Increment(chart.Version, c.segment)
if err != nil {
return err
}

chart.Metadata.Version = incrementedVersion
chart.Version = incrementedVersion

return writeChartFile(chart, c.chart)
return chartfile.Save(chart, c.chart)
}

func newGetVersionCommand(out io.Writer) *cobra.Command {
Expand Down Expand Up @@ -131,9 +114,10 @@ func newSetVersionCommand(out io.Writer) *cobra.Command {
f := cmd.Flags()
f.StringVarP(&sc.chart, "chart", "c", "", "Path to a local chart's root directory")
f.StringVarP(&sc.version, "version", "v", "", "New version of the chart")
f.StringVarP(&sc.prerelease, "prerelease", "p", "", "")
f.StringVarP(&sc.metadata, "metadata", "m", "", "")

cmd.MarkFlagRequired("chart")
cmd.MarkFlagRequired("version")

return cmd
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/chartfile/chartfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package chartfile

import (
"path/filepath"

"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/proto/hapi/chart"
)

// Save a chart's manifest file
func Save(cm *chart.Metadata, chartDir string) error {
return chartutil.SaveChartfile(Path(chartDir), cm)
}

// Path of a chart's manifest file
func Path(chartDir string) string {
return filepath.Join(chartDir, "Chart.yaml")
}

// Load a chart's manifest file
func Load(chartDir string) (*chart.Metadata, error) {
return chartutil.LoadChartfile(Path(chartDir))
}
55 changes: 55 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package version

import (
"fmt"

"github.com/Masterminds/semver"
)

// Increment a version's semver segment
func Increment(version string, segment string) (string, error) {
v1, err := semver.NewVersion(version)
if err != nil {
return "", err
}

var v2 semver.Version
switch segment {
case "patch":
v2 = v1.IncPatch()
case "minor":
v2 = v1.IncMinor()
case "major":
v2 = v1.IncMajor()
default:
return "", fmt.Errorf("Unknown version segment %s", segment)
}

return v2.String(), nil
}

// Assemble a semver version from its parts
func Assemble(version string, prerelease string, metadata string) (string, error) {
v, err := semver.NewVersion(version)
if err != nil {
return "", err
}

if prerelease != "" {
withPrerelease, err := v.SetPrerelease(prerelease)
if err != nil {
return "", err
}
v = &withPrerelease
}

if metadata != "" {
withMetadata, err := v.SetMetadata(metadata)
if err != nil {
return "", err
}
v = &withMetadata
}

return v.String(), nil
}
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "local-chart-version"
version: "0.0.2"
version: "0.0.3"
usage: "local-chart-version [subcommand] LOCAL_CHART_DIRECTORY [flags]"
description: |-
Helm plugin for setting/bumping the version number of a local chart
Expand Down

0 comments on commit 4c217e5

Please sign in to comment.