forked from go-acme/lego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.go
223 lines (190 loc) · 4.37 KB
/
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"os"
"regexp"
"strconv"
"strings"
"text/template"
"github.com/urfave/cli/v2"
)
const sourceFile = "./acme/api/internal/sender/useragent.go"
const uaTemplate = `package sender
// CODE GENERATED AUTOMATICALLY
// THIS FILE MUST NOT BE EDITED BY HAND
const (
// ourUserAgent is the User-Agent of this underlying library package.
ourUserAgent = "xenolf-acme/{{ .version }}"
// ourUserAgentComment is part of the UA comment linked to the version status of this underlying library package.
// values: detach|release
// NOTE: Update this with each tagged release.
ourUserAgentComment = "{{ .comment }}"
)
`
func main() {
app := cli.NewApp()
app.Name = "lego-releaser"
app.Usage = "Lego releaser"
app.HelpName = "releaser"
app.Commands = []*cli.Command{
{
Name: "release",
Usage: "Update file for a release",
Action: release,
Before: func(ctx *cli.Context) error {
mode := ctx.String("mode")
switch mode {
case "patch", "minor", "major":
return nil
default:
return fmt.Errorf("invalid mode: %s", mode)
}
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mode",
Aliases: []string{"m"},
Value: "patch",
Usage: "The release mode: patch|minor|major",
},
},
},
{
Name: "detach",
Usage: "Update file post release",
Action: detach,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func release(ctx *cli.Context) error {
mode := ctx.String("mode")
// Read file
data, err := readUserAgentFile(sourceFile)
if err != nil {
return err
}
// Bump version
newVersion, err := bumpVersion(data["ourUserAgent"], mode)
if err != nil {
return err
}
// Write file
comment := "release" // detach|release
return writeUserAgentFile(sourceFile, newVersion, comment)
}
func detach(_ *cli.Context) error {
// Read file
data, err := readUserAgentFile(sourceFile)
if err != nil {
return err
}
// Write file
version := strings.TrimPrefix(data["ourUserAgent"], "xenolf-acme/")
comment := "detach"
return writeUserAgentFile(sourceFile, version, comment)
}
type visitor struct {
data map[string]string
}
func (v visitor) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
switch d := n.(type) {
case *ast.GenDecl:
if d.Tok == token.CONST {
for _, spec := range d.Specs {
valueSpec, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
if len(valueSpec.Names) != 1 || len(valueSpec.Values) != 1 {
continue
}
va, ok := valueSpec.Values[0].(*ast.BasicLit)
if !ok {
continue
}
if va.Kind != token.STRING {
continue
}
s, err := strconv.Unquote(va.Value)
if err != nil {
continue
}
v.data[valueSpec.Names[0].String()] = s
}
}
default:
// noop
}
return v
}
func readUserAgentFile(filename string) (map[string]string, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
if err != nil {
return nil, err
}
v := visitor{data: make(map[string]string)}
ast.Walk(v, file)
return v.data, nil
}
func writeUserAgentFile(filename, version, comment string) error {
tmpl, err := template.New("ua").Parse(uaTemplate)
if err != nil {
return err
}
b := &bytes.Buffer{}
err = tmpl.Execute(b, map[string]string{
"version": version,
"comment": comment,
})
if err != nil {
return err
}
source, err := format.Source(b.Bytes())
if err != nil {
return err
}
return os.WriteFile(filename, source, 0o644)
}
func bumpVersion(userAgent, mode string) (string, error) {
prevVersion := strings.TrimPrefix(userAgent, "xenolf-acme/")
allString := regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)`).FindStringSubmatch(prevVersion)
if len(allString) != 4 {
return "", fmt.Errorf("invalid version format: %s", prevVersion)
}
switch mode {
case "patch":
patch, err := strconv.Atoi(allString[3])
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%s.%d", allString[1], allString[2], patch+1), nil
case "minor":
minor, err := strconv.Atoi(allString[2])
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%d.0", allString[1], minor+1), nil
case "major":
major, err := strconv.Atoi(allString[1])
if err != nil {
return "", err
}
return fmt.Sprintf("%d.0.0", major+1), nil
default:
return "", fmt.Errorf("invalid mode: %s", mode)
}
}