-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepare_release.go
173 lines (147 loc) · 4.05 KB
/
prepare_release.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2018-2023 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var (
author = flag.String("author", "", "the author that creates the release")
email = flag.String("email", "", "the email of the authot that creates the release")
versionTag = flag.String("version-tag", "", "the tag of the version")
version = flag.String("version", "0", "version to tag")
revaVersion = flag.String("reva-version", "", "the reva version and commt")
)
const (
specFile = "cernbox-revad.spec"
prodBranch = "cernbox"
)
func init() {
flag.Parse()
if *author == "" || *email == "" || *revaVersion == "" {
fmt.Fprintln(os.Stderr, "fill the author, email, and revaVersion flags")
os.Exit(1)
}
}
func main() {
err := releaseNewVersion(*author, *email, *revaVersion)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func releaseNewVersion(author, email, revaVersion string) error {
specContent, err := readSpecFile()
if err != nil {
return fmt.Errorf("error reading spec content: %w", err)
}
versionStr := getNextVersion(specContent)
if *versionTag != "" && *versionTag != prodBranch {
versionStr += "." + *versionTag
}
// update the version in the spec file
for i, line := range specContent {
if strings.HasPrefix(line, "Version:") {
specContent[i] = "Version: " + versionStr
}
}
// add the changelog
changelogHeader := -1
for i, line := range specContent {
if strings.HasPrefix(line, "%changelog") {
changelogHeader = i
}
}
if changelogHeader == -1 {
return errors.New("changelog header not found in spec file")
}
var newChangelog []string
today := time.Now().Format("Mon Jan 02 2006")
newChangelog = append(newChangelog, fmt.Sprintf("* %s %s <%s> %s", today, author, email, versionStr))
newChangelog = append(newChangelog, fmt.Sprintf("- v%s, based on %s", versionStr, revaVersion))
var newSpec []string
newSpec = append(newSpec, specContent[:changelogHeader+1]...)
newSpec = append(newSpec, newChangelog...)
newSpec = append(newSpec, specContent[changelogHeader+1:]...)
err = writeSpecFile(newSpec)
if err != nil {
return fmt.Errorf("error updating spec file: %w", err)
}
return nil
}
func readSpecFile() ([]string, error) {
f, err := os.Open(specFile)
if err != nil {
return nil, err
}
defer f.Close()
scan := bufio.NewScanner(f)
var spec []string
for scan.Scan() {
spec = append(spec, scan.Text())
}
return spec, nil
}
func writeSpecFile(spec []string) error {
f, err := os.OpenFile(specFile, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
for _, line := range spec {
f.WriteString(line + "\n")
}
return nil
}
func getNextVersion(spec []string) string {
if *version != "0" {
return *version
}
for _, line := range spec {
if strings.HasPrefix(line, "Version:") {
v := strings.TrimPrefix(line, "Version: ")
split := strings.Split(v, ".")
ver, err := strconv.ParseInt(split[2], 10, 64)
if err != nil {
return "invalid"
}
return fmt.Sprintf("%s.%s.%d", split[0], split[1], int(ver) + 1)
}
}
panic("cannot find a version")
}
func run(cmd *exec.Cmd) {
var b bytes.Buffer
mw := io.MultiWriter(os.Stdout, &b)
cmd.Stdout = mw
cmd.Stderr = mw
err := cmd.Run()
if err != nil {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
}