-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncs-pkg-bumper.go
142 lines (121 loc) · 3.1 KB
/
ncs-pkg-bumper.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
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"github.com/beevik/etree"
)
type bumpType = string
const (
major bumpType = "major"
minor bumpType = "minor"
patch bumpType = "patch"
)
func bumpVersion(source string, bump bumpType) string {
var versionParts = strings.Split(source, ".")
var partsCount = len(versionParts)
if partsCount < 3 {
for i := partsCount; i < 3; i++ {
versionParts = append(versionParts, "0")
}
}
if partsCount > 3 {
versionParts = versionParts[:3]
}
var intVersionParts []int32
for _, part := range versionParts {
var value, err = strconv.ParseInt(part, 10, 32)
if err != nil {
fmt.Println(err.Error())
fmt.Println(errors.New("could not parse the version! Not all version parts are numeric!"))
os.Exit(1)
}
intVersionParts = append(intVersionParts, int32(value))
}
switch bump {
case major:
return fmt.Sprintf("%d.0.0", intVersionParts[0]+1)
case minor:
return fmt.Sprintf("%d.%d.0", intVersionParts[0], intVersionParts[1]+1)
case patch:
return fmt.Sprintf("%d.%d.%d", intVersionParts[0], intVersionParts[1], intVersionParts[2]+1)
}
panic("Unknown bumpType")
}
var (
filePath string
bumpMode string
)
func init() {
flag.StringVar(&filePath, "p", "", "Path to file. Defaults to './package-meta-data.xml'")
flag.StringVar(&bumpMode, "m", "", "Mode of bump one of: ['major', 'minor', 'patch']")
flag.Parse()
}
func main() {
// validate args
if filePath == "" {
filePath = "./package-meta-data.xml"
} else {
pathInfo, err := os.Stat(filePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if pathInfo.IsDir() {
filePath = path.Join(filePath, "package-meta-data.xml")
}
}
if bumpMode == "" {
fmt.Println(errors.New("'-m' arg is missing"))
os.Exit(1)
}
if bumpMode != major && bumpMode != minor && bumpMode != patch {
fmt.Println(errors.New("'-m' flag has an unknown value"))
os.Exit(1)
}
// Open our xmlFile
xmlFile, err := os.Open(filePath)
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// read our opened xmlFile as a byte array.
byteValue, _ := io.ReadAll(xmlFile)
// close file
xmlFile.Close()
// create document tree from file
xmlDoc := etree.NewDocument()
if err := xmlDoc.ReadFromBytes(byteValue); err != nil {
panic(err)
}
// get package-name
pkgName := xmlDoc.FindElement("/ncs-package/name")
if pkgName == nil {
fmt.Println(errors.New("could not grab 'name' tag from meta-data.xml"))
os.Exit(1)
}
// get version tag
pkgVersion := xmlDoc.FindElement("/ncs-package/package-version")
if pkgVersion == nil {
fmt.Println(errors.New("could not grab 'package-version' tag from meta-data.xml"))
os.Exit(1)
}
// log package name
fmt.Printf("--- Package: %s ---\n", pkgName.Text())
// get version string
currentVersion := pkgVersion.Text()
// modify version
newVersion := bumpVersion(currentVersion, bumpMode)
// log versions
fmt.Printf("Current version: %s\n", currentVersion)
pkgVersion.SetText(newVersion)
fmt.Printf("New version: %s\n", newVersion)
// save to file
xmlDoc.WriteToFile(filePath)
}