Skip to content

Commit eb7e0b2

Browse files
committed
feature: support v2023 and v2
Signed-off-by: Valery Piashchynski <[email protected]>
1 parent 2110a71 commit eb7e0b2

18 files changed

+365
-185
lines changed

.github/workflows/linters.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
- name: Set up Go
1414
uses: actions/setup-go@v4 # action page: <https://github.com/actions/setup-go>
1515
with:
16-
go-version: 1.19
16+
go-version: stable
1717

1818
- name: Run linter
1919
uses: golangci/[email protected] # Action page: <https://github.com/golangci/golangci-lint-action>
2020
with:
21-
version: v1.49 # without patch version
21+
version: v1.52 # without patch version
2222
only-new-issues: false # show only new issues if it's a pull request
2323
args: --timeout=10m --build-tags=race

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- name: Set up Go
3737
uses: actions/setup-go@v4
3838
with:
39-
go-version: 1.19
39+
go-version: stable
4040

4141
- name: Check out code
4242
uses: actions/checkout@v3

.golangci.yml

-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ linters-settings:
1818
check-shadowing: true
1919
golint:
2020
min-confidence: 0.1
21-
gocyclo:
22-
min-complexity: 15
2321
godot:
2422
scope: declarations
2523
capital: true
@@ -53,10 +51,8 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
5351
- exportloopref # checks for pointers to enclosing loop variables
5452
- gochecknoglobals # Checks that no globals are present in Go code
5553
- gochecknoinits # Checks that no init functions are present in Go code
56-
- gocognit # Computes and checks the cognitive complexity of functions
5754
- goconst # Finds repeated strings that could be replaced by a constant
5855
- gocritic # The most opinionated Go source code linter
59-
- gocyclo # Computes and checks the cyclomatic complexity of functions
6056
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
6157
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
6258
- revive

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ENV LDFLAGS="-s \
1818
# verbose
1919
RUN set -x
2020
RUN go mod download
21-
RUN go mod tidy -go 1.19
21+
RUN go mod tidy -go 1.20
2222

2323
RUN CGO_ENABLED=0 go build -trimpath -ldflags "$LDFLAGS" -o ./velox ./cmd/vx
2424

builder/builder.go

+69-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package builder
22

33
import (
44
"bytes"
5+
"fmt"
56
"math/rand"
67
"os"
78
"os/exec"
@@ -11,7 +12,9 @@ import (
1112
"strings"
1213
"syscall"
1314

15+
"github.com/hashicorp/go-version"
1416
"github.com/roadrunner-server/velox"
17+
"github.com/roadrunner-server/velox/builder/templates"
1518
"go.uber.org/zap"
1619
)
1720

@@ -48,11 +51,18 @@ func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out string, log
4851
}
4952

5053
// Build builds a RR based on the provided modules info
51-
func (b *Builder) Build() error { //nolint:gocyclo
52-
t := new(Template)
53-
t.Entries = make([]*Entry, len(b.modules))
54+
func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
55+
t := new(templates.Template)
56+
57+
module, err := validateModule(rrModule)
58+
if err != nil {
59+
return err
60+
}
61+
62+
t.ModuleVersion = module
63+
t.Entries = make([]*templates.Entry, len(b.modules))
5464
for i := 0; i < len(b.modules); i++ {
55-
t.Entries[i] = &Entry{
65+
t.Entries[i] = &templates.Entry{
5666
Module: b.modules[i].ModuleName,
5767
Prefix: randStringBytes(5),
5868
Structure: pluginStructureStr,
@@ -62,9 +72,21 @@ func (b *Builder) Build() error { //nolint:gocyclo
6272
}
6373

6474
buf := new(bytes.Buffer)
65-
err := compileTemplate(buf, t)
66-
if err != nil {
67-
return err
75+
76+
// compatibility with the version 2
77+
switch t.ModuleVersion {
78+
case velox.V2023:
79+
err = templates.CompileTemplateV2023(buf, t)
80+
if err != nil {
81+
return err
82+
}
83+
case velox.V2:
84+
err = templates.CompileTemplateV2(buf, t)
85+
if err != nil {
86+
return err
87+
}
88+
default:
89+
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
6890
}
6991

7092
b.log.Debug("[RESULTING TEMPLATE]", zap.String("template", buf.String()))
@@ -109,9 +131,20 @@ func (b *Builder) Build() error { //nolint:gocyclo
109131

110132
buf.Reset()
111133

112-
err = compileGoModTemplate(buf, t)
113-
if err != nil {
114-
return err
134+
// compatibility with the version 2
135+
switch t.ModuleVersion {
136+
case velox.V2023:
137+
err = templates.CompileGoModTemplate2023(buf, t)
138+
if err != nil {
139+
return err
140+
}
141+
case velox.V2:
142+
err = templates.CompileGoModTemplateV2(buf, t)
143+
if err != nil {
144+
return err
145+
}
146+
default:
147+
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
115148
}
116149

117150
_, err = goModFile.Write(buf.Bytes())
@@ -139,7 +172,7 @@ func (b *Builder) Build() error { //nolint:gocyclo
139172
}
140173
}
141174

142-
// upgrade to 1.19
175+
// upgrade to 1.20
143176
err = b.goModTidyCmd()
144177
if err != nil {
145178
return err
@@ -165,6 +198,26 @@ func (b *Builder) Build() error { //nolint:gocyclo
165198
return nil
166199
}
167200

201+
func (b *Builder) Write(d []byte) (int, error) {
202+
b.log.Debug("[STDERR OUTPUT]", zap.ByteString("log", d))
203+
return len(d), nil
204+
}
205+
206+
func validateModule(module string) (string, error) {
207+
if module == "master" {
208+
// default branch
209+
return velox.V2023, nil
210+
}
211+
212+
v, err := version.NewVersion(module)
213+
if err != nil {
214+
return "", err
215+
}
216+
217+
// return major version (v2, v2023, etc)
218+
return fmt.Sprintf("v%d", v.Segments()[0]), nil
219+
}
220+
168221
func randStringBytes(n int) string {
169222
b := make([]byte, n)
170223
for i := range b {
@@ -208,8 +261,8 @@ func (b *Builder) goBuildCmd(out string) error {
208261
}
209262

210263
func (b *Builder) goModTidyCmd() error {
211-
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod tidy -go=1.19"))
212-
cmd := exec.Command("go", "mod", "tidy", "-go=1.19")
264+
b.log.Info("[EXECUTING CMD]", zap.String("cmd", "go mod tidy -go=1.20"))
265+
cmd := exec.Command("go", "mod", "tidy", "-go=1.20")
213266
cmd.Stderr = b
214267
err := cmd.Start()
215268
if err != nil {
@@ -238,14 +291,14 @@ func (b *Builder) goGetMod(repo, hash string) error {
238291
return nil
239292
}
240293

241-
func (b *Builder) getDepsReplace(repl string) []*Entry {
294+
func (b *Builder) getDepsReplace(repl string) []*templates.Entry {
242295
b.log.Info("[REPLACING DEPENDENCIES]", zap.String("dependency", repl))
243296
modFile, err := os.ReadFile(path.Join(repl, goModStr))
244297
if err != nil {
245298
return nil
246299
}
247300

248-
var result []*Entry //nolint:prealloc
301+
var result []*templates.Entry //nolint:prealloc
249302
replaces := replaceRegexp.FindAllStringSubmatch(string(modFile), -1)
250303
for i := 0; i < len(replaces); i++ {
251304
split := strings.Split(strings.TrimSpace(replaces[i][0]), " => ")
@@ -261,7 +314,7 @@ func (b *Builder) getDepsReplace(repl string) []*Entry {
261314
moduleReplace = path.Join(repl, moduleReplace)
262315
}
263316

264-
result = append(result, &Entry{
317+
result = append(result, &templates.Entry{
265318
Module: moduleName,
266319
Replace: moduleReplace,
267320
})
@@ -298,8 +351,3 @@ func moveFile(from, to string) error {
298351

299352
return toFile.Close()
300353
}
301-
302-
func (b *Builder) Write(d []byte) (int, error) {
303-
b.log.Debug("[STDERR OUTPUT]", zap.ByteString("log", d))
304-
return len(d), nil
305-
}

builder/builder_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
remotePackageOne = "https://github.com/my/package_one"
1818
remotePackageTwo = "https://github.com/my/package_two"
1919
replaceGoModOneRelative = `module go.dev/my/module
20-
go 1.19
20+
go 1.20
2121
2222
require (
2323
github.com/fatih/color v1.13.0
@@ -26,7 +26,7 @@ require (
2626
replace github.com/dummy/package => ./something
2727
`
2828
replaceGoModOneAbsolute = `module go.dev/my/module
29-
go 1.19
29+
go 1.20
3030
3131
require (
3232
github.com/fatih/color v1.13.0
@@ -35,7 +35,7 @@ require (
3535
replace github.com/dummy/package => /tmp/dummy
3636
`
3737
replaceGoModMultipleRelative = `module go.dev/my/module
38-
go 1.19
38+
go 1.20
3939
4040
require (
4141
github.com/fatih/color v1.13.0
@@ -47,7 +47,7 @@ replace (
4747
)
4848
`
4949
replaceGoModMultipleAbsolute = `module go.dev/my/module
50-
go 1.19
50+
go 1.20
5151
5252
require (
5353
github.com/fatih/color v1.13.0
@@ -59,7 +59,7 @@ replace (
5959
)
6060
`
6161
replaceGoModMultipleRemote = `module go.dev/my/module
62-
go 1.19
62+
go 1.20
6363
6464
require (
6565
github.com/fatih/color v1.13.0

builder/template.go

-95
This file was deleted.

0 commit comments

Comments
 (0)