Skip to content

Commit f583d53

Browse files
committed
Chore(UV): Check level validity in a smoother way
1 parent 659d8c4 commit f583d53

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

main.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ func stageChanges() {
167167
// It only changes version number in package.json by default.
168168
// `files` is a comma seperated string of other files to change version in.
169169
func updateVersionNumbers(level string, files string) {
170+
// Make a map of levels to their respective numbers of the version number
171+
var table = map[string]int{
172+
"major": 0,
173+
"minor": 1,
174+
"patch": 2,
175+
}
176+
177+
// Check if level is valid
178+
var err error = fmt.Errorf("Invalid level. Use one of the following: major, minor, patch.")
179+
for key := range table {
180+
if key == level {
181+
err = nil
182+
}
183+
}
184+
check(err)
185+
186+
// Get the index of the version number to increment
187+
index := table[level]
188+
170189
// Read the package.json file
171190
packageJSONBytes, err := os.ReadFile("package.json")
172191
check(err)
@@ -180,14 +199,6 @@ func updateVersionNumbers(level string, files string) {
180199
newVersion := strings.Split(packageJSONMap["version"].(string), ".")
181200
var num int
182201

183-
// Get the index of the version number to increment
184-
var table = map[string]int{
185-
"major": 0,
186-
"minor": 1,
187-
"patch": 2,
188-
}
189-
index := table[level]
190-
191202
// Update the version number
192203
num, err = strconv.Atoi(newVersion[index])
193204
check(err)
@@ -290,12 +301,6 @@ func main() {
290301
},
291302
Action: func(c *cli.Context) (err error) {
292303
level := c.Args().First()
293-
// Check if level is valid
294-
if level != "major" && level != "minor" && level != "patch" {
295-
fmt.Println("Invalid level. Use one of the following: major, minor, patch.")
296-
return
297-
}
298-
299304
updateVersionNumbers(level, files)
300305
return
301306
},

0 commit comments

Comments
 (0)