forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_helm_version.go
99 lines (88 loc) · 2.28 KB
/
step_helm_version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// StepHelmVersionOptions contains the command line flags
type StepHelmVersionOptions struct {
StepHelmOptions
Version string
}
var (
StepHelmVersionLong = templates.LongDesc(`
Updates version of the helm Chart.yaml in the given directory
`)
StepHelmVersionExample = templates.Examples(`
# updates the current helm Chart.yaml to the latest build number version
jx step helm version
`)
)
func NewCmdStepHelmVersion(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepHelmVersionOptions{
StepHelmOptions: StepHelmOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "version",
Short: "Updates the chart version in the given directory",
Aliases: []string{""},
Long: StepHelmVersionLong,
Example: StepHelmVersionExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addStepHelmFlags(cmd)
cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The version to update. If none specified it defaults to $BUILD_NUMBER")
return cmd
}
func (o *StepHelmVersionOptions) Run() error {
version := o.Version
if version == "" {
version = o.getBuildNumber()
}
if version == "" {
return fmt.Errorf("No version specified and could not detect the build number via $BUILD_NUMBER")
}
var err error
dir := o.Dir
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return err
}
}
chartFile := filepath.Join(dir, "Chart.yaml")
exists, err := util.FileExists(chartFile)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("No chart exists at %s", chartFile)
}
err = helm.SetChartVersion(chartFile, version)
if err != nil {
return err
}
log.Infof("Modified file %s to set the chart to version %s\n", util.ColorInfo(chartFile), util.ColorInfo(version))
return nil
}