-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (71 loc) · 1.67 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
package main
import (
"fmt"
"log/slog"
"os"
"strings"
"github.com/coreos/go-semver/semver"
flag "github.com/spf13/pflag"
)
func main() {
// parse the flags via pflags
input := flag.StringP("input", "i", "", "input version")
bump := flag.StringP("bump", "b", "", "bump version")
flag.Parse()
if input == nil || *input == "" {
slog.Error("Input version is required")
os.Exit(1)
}
if bump == nil || *bump == "" {
slog.Error("Bump type is required")
os.Exit(1)
}
validBumps := map[string]bool{
"major": true,
"minor": true,
"patch": true,
}
if !validBumps[*bump] {
slog.Error("Invalid bump type", "bump", *bump)
os.Exit(1)
}
addPrefix := false
if strings.HasPrefix(*input, "v") {
*input = (*input)[1:]
addPrefix = true
}
parsed, err := semver.NewVersion(*input)
if err != nil {
slog.Error("Invalid version", "version", *input, "error", err.Error())
os.Exit(1)
}
switch *bump {
case "major":
parsed.Major += 1
parsed.Minor = 0
parsed.Patch = 0
case "minor":
parsed.Minor += 1
parsed.Patch = 0
case "patch":
parsed.Patch += 1
}
updatedVersion := parsed.String()
if addPrefix {
updatedVersion = "v" + parsed.String()
}
fmt.Printf(updatedVersion + "\n")
githubOutput := os.Getenv("GITHUB_OUTPUT")
if githubOutput != "" {
f, err := os.OpenFile(githubOutput, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
slog.Error("Failed to open file", "file", githubOutput, "error", err.Error())
os.Exit(1)
}
defer f.Close()
if _, err = f.WriteString(fmt.Sprintf("version=%s", updatedVersion)); err != nil {
slog.Error("Failed to write to file", "file", githubOutput, "error", err.Error())
os.Exit(1)
}
}
}