-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
197 lines (157 loc) · 4.44 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"fmt"
"io"
"os"
"github.com/mbenabda/helm-local-chart-version/pkg/chartfile"
"github.com/mbenabda/helm-local-chart-version/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
// Version identifier populated via the CI/CD process.
var Version = "HEAD"
type getVersionCommand struct {
chart string
segment string
out io.Writer
}
type setVersionCommand struct {
chart string
version string
prerelease string
updatePrerelease bool
out io.Writer
}
type bumpVersionCommand struct {
chart string
segment string
out io.Writer
}
func (c *getVersionCommand) run() error {
chart, err := chartfile.Open(c.chart)
if err != nil {
return err
}
segment, err := version.Get(chart.Version(), c.segment)
if err != nil {
return err
}
fmt.Fprintf(c.out, segment)
return nil
}
func (cmd *setVersionCommand) run() error {
chart, err := chartfile.Open(cmd.chart)
if err != nil {
return err
}
var baseVersion string
if cmd.version == "" {
baseVersion = chart.Version()
} else {
baseVersion = cmd.version
}
prerelease := cmd.prerelease
if !cmd.updatePrerelease {
pre, err := version.Get(baseVersion, "prerelease")
if err != nil {
return err
}
prerelease = pre
}
finalVersion, err := version.Assemble(baseVersion, prerelease)
if err != nil {
return err
}
return chart.SetVersion(finalVersion)
}
func (c *bumpVersionCommand) run() error {
chart, err := chartfile.Open(c.chart)
if err != nil {
return err
}
incrementedVersion, err := version.Increment(chart.Version(), c.segment)
if err != nil {
return err
}
return chart.SetVersion(incrementedVersion)
}
func newGetVersionCommand(out io.Writer) *cobra.Command {
gv := &getVersionCommand{out: out}
cmd := &cobra.Command{
Use: "get",
Short: "Print a local chart's version number",
RunE: func(cmd *cobra.Command, args []string) error {
return gv.run()
},
}
f := cmd.Flags()
f.StringVarP(&gv.chart, "chart", "c", "", "Path to a local chart's root directory")
f.StringVarP(&gv.segment, "version-segment", "s", "", "Specific segment of the chart's version to get (major|minor|patch|prerelease)")
cmd.MarkFlagRequired("chart")
return cmd
}
func newSetVersionCommand(out io.Writer) *cobra.Command {
sc := &setVersionCommand{out: out}
cmd := &cobra.Command{
Use: "set",
Short: "Modify a local chart's version number in place",
RunE: func(cmd *cobra.Command, args []string) error {
sc.updatePrerelease = cmd.Flags().Lookup("prerelease") != nil
return sc.run()
},
}
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", "", "")
cmd.MarkFlagRequired("chart")
return cmd
}
func newBumpVersionCommand(out io.Writer) *cobra.Command {
bc := &bumpVersionCommand{out: out}
cmd := &cobra.Command{
Use: "bump",
Short: "Increment the desired segment of a local chart's version",
RunE: func(cmd *cobra.Command, args []string) error {
return bc.run()
},
}
f := cmd.Flags()
f.StringVarP(&bc.chart, "chart", "c", "", "Path to a local chart's root directory")
f.StringVarP(&bc.segment, "version-segment", "s", "", "Segment of the chart's version to bump (major|minor|patch)")
cmd.MarkFlagRequired("chart")
cmd.MarkFlagRequired("version-segment")
return cmd
}
func newGenerateDocumentationCommand(out io.Writer, rootCmd *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "generate-documentation",
Short: "Generate the cli documentation",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return doc.GenMarkdownTree(rootCmd, "./docs/")
},
}
}
func main() {
rootCmd := &cobra.Command{
Use: "local-chart-version",
Long: "Modify the version number of a local helm chart",
}
out := rootCmd.OutOrStdout()
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Print the version of the local-chart-version helm plugin",
Long: "All software has versions. This is helm-local-chart-version's",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprint(out, Version)
},
})
rootCmd.AddCommand(newGetVersionCommand(out))
rootCmd.AddCommand(newSetVersionCommand(out))
rootCmd.AddCommand(newBumpVersionCommand(out))
rootCmd.AddCommand(newGenerateDocumentationCommand(out, rootCmd))
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}