-
Notifications
You must be signed in to change notification settings - Fork 18
/
release_generate.go
112 lines (101 loc) · 2.1 KB
/
release_generate.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"os"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/mandelsoft/filepath/pkg/filepath"
)
func Error(err error) {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
func main() {
args := os.Args[1:]
nodev := false
for len(args) > 0 {
if !strings.HasPrefix(args[0], "-") {
break
}
switch args[0] {
case "--no-dev":
nodev = true
default:
Error(fmt.Errorf("ilvalid option %q", args[0]))
}
args = args[1:]
}
if len(args) == 0 {
Error(fmt.Errorf("missing argument"))
}
cmd := args[0]
pre := ""
if len(args) > 1 {
pre = args[1]
}
vers_file := "VERSION"
var data []byte
dir := "."
verpath := ""
err := os.ErrClosed
for err != nil {
dir, err = filepath.Abs(dir)
if err == nil {
if !filepath.IsRoot(dir) {
verpath = filepath.Join(dir, vers_file)
data, err = os.ReadFile(verpath)
} else {
err = fmt.Errorf("no %q file found", vers_file)
}
}
dir += "/.."
}
if err != nil {
Error(fmt.Errorf("cannot read version file %q: %w", vers_file, err))
}
raw := strings.TrimSpace(string(data))
v := raw
if i := strings.Index(raw, "-"); i >= 0 {
v = raw[:i]
found := raw[i+1:]
if pre == "" {
pre = found
}
}
if nodev && pre == "dev" {
Error(fmt.Errorf("dev release not possible"))
}
nonpre := semver.MustParse(v)
if pre != "" {
_ = semver.MustParse(v + "-" + pre)
}
v = strings.TrimPrefix(v, "v")
//nolint:forbidigo // Logger not needed for this command.
switch cmd {
case "print-version":
fmt.Print(v)
case "print-rc-version":
if pre == "" {
fmt.Print(v)
} else {
fmt.Printf("%s-%s", v, pre)
}
case "bump-version":
var next string
if nonpre.Patch() > 0 {
next = nonpre.IncPatch().String()
} else {
next = nonpre.IncMinor().String()
}
next += "-dev"
fmt.Printf("%s", next)
case "bump-patch":
next := nonpre.IncPatch().String() + "-dev"
fmt.Printf("%s", next)
default:
Error(fmt.Errorf("invalid command %q", cmd))
}
}