/
bump.go
240 lines (208 loc) · 5.53 KB
/
bump.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"fmt"
"strings"
"github.com/coreos/go-semver/semver"
bumper "github.com/giantswarm/semver-bump/bump"
"github.com/giantswarm/semver-bump/storage"
"github.com/urfave/cli"
)
// VType represents the version type
type VType int
//
const (
Patch VType = iota
Minor
Major
)
var (
// InitialVersion - inital fn version.
InitialVersion = "0.0.1"
)
// BumpCommand command to build function version.
func BumpCommand() cli.Command {
cmd := bumpcmd{}
flags := append([]cli.Flag{}, cmd.flags()...)
return cli.Command{
Name: "bump",
Usage: "\tBump function version",
Aliases: []string{"bm"},
Category: "DEVELOPMENT COMMANDS",
Description: "This command bumps the version of the func.yaml.",
Flags: flags,
Action: cmd.bump,
}
}
type bumpcmd struct {
major bool
minor bool
}
func (b *bumpcmd) flags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "major",
Usage: "bumps major version",
Destination: &b.major,
},
cli.BoolFlag{
Name: "minor",
Usage: "bumps minor version",
Destination: &b.minor,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "verbose mode",
Destination: &CommandVerbose,
},
cli.StringFlag{
Name: "working-dir,w",
Usage: "Specify the working directory to bump a function, must be the full path.",
},
}
}
// bump will take the found valid function and bump its version
func (b *bumpcmd) bump(c *cli.Context) error {
var t VType
var dir string
if b.major {
t = Major
} else if b.minor {
t = Minor
} else {
t = Patch
}
dir = GetDir(c)
ff, err := ReadInFuncFile()
version := GetFuncYamlVersion(ff)
if version == LatestYamlVersion {
_, err = bumpItWdV20180708(dir, t)
} else {
_, err = bumpItWd(dir, t)
}
return err
}
func bumpItWd(wd string, vtype VType) (*FuncFile, error) {
fn, err := FindFuncfile(wd)
if err != nil {
return nil, err
}
return BumpIt(fn, vtype)
}
// BumpIt returns updated funcfile
func BumpIt(fpath string, vtype VType) (*FuncFile, error) {
// fmt.Println("Bumping version in func file at: ", fpath)
funcfile, err := ParseFuncfile(fpath)
if err != nil {
return nil, err
}
funcfile, err = bumpVersion(funcfile, vtype)
if err != nil {
return nil, err
}
if err := storeFuncfile(fpath, funcfile); err != nil {
return nil, err
}
fmt.Println("Bumped to version", funcfile.Version)
return funcfile, nil
}
func bumpVersion(funcfile *FuncFile, t VType) (*FuncFile, error) {
funcfile.Name = cleanImageName(funcfile.Name)
if funcfile.Version == "" {
funcfile.Version = InitialVersion
return funcfile, nil
}
s, err := storage.NewVersionStorage("local", funcfile.Version)
if err != nil {
return nil, err
}
version := bumper.NewSemverBumper(s, "")
var newver *semver.Version
if t == Major {
newver, err = version.BumpMajorVersion("", "")
} else if t == Minor {
newver, err = version.BumpMinorVersion("", "")
} else {
newver, err = version.BumpPatchVersion("", "")
}
if err != nil {
return nil, err
}
funcfile.Version = newver.String()
return funcfile, nil
}
// cleanImageName is intended to remove any trailing tag from the image name
// since the version field conveys this information. More cleanup could be done
// here in future if necessary.
func cleanImageName(name string) string {
slashParts := strings.Split(name, "/")
l := len(slashParts) - 1
if i := strings.Index(slashParts[l], ":"); i > -1 {
slashParts[l] = slashParts[l][:i]
}
return strings.Join(slashParts, "/")
}
// --------- FuncFileV20180708 -------------
func bumpItWdV20180708(wd string, vtype VType) (*FuncFileV20180708, error) {
fn, err := FindFuncfile(wd)
if err != nil {
return nil, err
}
return BumpItV20180708(fn, vtype)
}
// BumpIt returns updated funcfile
func BumpItV20180708(fpath string, vtype VType) (*FuncFileV20180708, error) {
// fmt.Println("Bumping version in func file at: ", fpath)
funcfile, err := ParseFuncFileV20180708(fpath)
if err != nil {
return nil, err
}
funcfile, err = bumpVersionV20180708(funcfile, vtype)
if err != nil {
return nil, err
}
if err := storeFuncFileV20180708(fpath, funcfile); err != nil {
return nil, err
}
fmt.Println("Bumped to version", funcfile.Version)
return funcfile, nil
}
func bumpVersionV20180708(funcfile *FuncFileV20180708, t VType) (*FuncFileV20180708, error) {
funcfile.Name = cleanImageName(funcfile.Name)
if funcfile.Version == "" {
funcfile.Version = InitialVersion
return funcfile, nil
}
s, err := storage.NewVersionStorage("local", funcfile.Version)
if err != nil {
return nil, err
}
version := bumper.NewSemverBumper(s, "")
var newver *semver.Version
if t == Major {
newver, err = version.BumpMajorVersion("", "")
} else if t == Minor {
newver, err = version.BumpMinorVersion("", "")
} else {
newver, err = version.BumpPatchVersion("", "")
}
if err != nil {
return nil, err
}
funcfile.Version = newver.String()
return funcfile, nil
}