-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathversioner.go
More file actions
39 lines (36 loc) · 820 Bytes
/
versioner.go
File metadata and controls
39 lines (36 loc) · 820 Bytes
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
package main
import (
"fmt"
"os"
"path"
"regexp"
"runtime"
"text/template"
)
func getTemplateFilePath() string {
_, filename, _, _ := runtime.Caller(0) // nolint:dogsled
dir := path.Dir(filename)
return path.Join(dir, "version.go.tmpl")
}
func main() {
tag := os.Args[1] // The first argument is the tag
// If the tag is not a valid semantic version, then it is not a release tag
re := regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
if !re.MatchString(tag) {
panic(fmt.Errorf("tag %s is not a valid semantic version", tag))
}
tmpl, err := template.ParseFiles(getTemplateFilePath())
if err != nil {
panic(err)
}
f, err := os.Create("version.go")
if err != nil {
panic(err)
}
defer f.Close()
if err := tmpl.Execute(f, map[string]string{
"Version": string(tag),
}); err != nil {
panic(err)
}
}