From 67ec07874c1fe6093373673f7f6725788334c024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Wed, 2 Oct 2024 13:21:00 +0200 Subject: [PATCH 1/2] chore: upgrades to go 1.22 and tinygo 0.33 --- .github/workflows/ci.yaml | 4 +- .github/workflows/nightly-coraza-check.yaml | 4 +- go.mod | 2 +- go.work | 2 +- magefile.go | 80 ++++++++++++++++++++- testing/coreruleset/go.mod | 2 +- 6 files changed, 85 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e45feff..ef5457c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -15,8 +15,8 @@ on: - "LICENSE" env: - GO_VERSION: "1.21.x" - TINYGO_VERSION: "0.30.0" + GO_VERSION: "1.22" + TINYGO_VERSION: "0.33" jobs: build: diff --git a/.github/workflows/nightly-coraza-check.yaml b/.github/workflows/nightly-coraza-check.yaml index 763a528..e28f346 100644 --- a/.github/workflows/nightly-coraza-check.yaml +++ b/.github/workflows/nightly-coraza-check.yaml @@ -8,8 +8,8 @@ on: - cron: "0 4 * * *" env: - GO_VERSION: "1.21.x" - TINYGO_VERSION: "0.30.0" + GO_VERSION: "1.22" + TINYGO_VERSION: "0.33" jobs: test: diff --git a/go.mod b/go.mod index afc2093..c802029 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/corazawaf/coraza-http-wasm -go 1.21 +go 1.22 require ( github.com/corazawaf/coraza-coreruleset/v4 v4.0.0 diff --git a/go.work b/go.work index b55fb4b..9982d78 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.21.0 +go 1.22 use ( . diff --git a/magefile.go b/magefile.go index 654fe40..dd25aa7 100644 --- a/magefile.go +++ b/magefile.go @@ -8,6 +8,9 @@ import ( "io" "os" "path/filepath" + "regexp" + "strconv" + "strings" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" @@ -18,12 +21,85 @@ import ( var Default = Build var ( - golangCILintVer = "v1.56.2" // https://github.com/golangci/golangci-lint/releases - gosImportsVer = "v0.3.8" // https://github.com/rinchsan/gosimports/releases/tag/v0.3.1 + minGoVersion = "1.22" + minTinygoVersion = "0.33.0" + golangCILintVer = "v1.61.0" // https://github.com/golangci/golangci-lint/releases + gosImportsVer = "v0.3.8" // https://github.com/rinchsan/gosimports/releases/tag/v0.3.1 ) var errCommitFormatting = errors.New("files not formatted, please commit formatting changes") +func init() { + for _, check := range []struct { + lang string + minVersion string + }{ + {"tinygo", minTinygoVersion}, + {"go", minGoVersion}, + } { + if err := checkVersion(check.lang, check.minVersion); err != nil { + fmt.Printf("Error: %v\n", err) + os.Exit(1) + } + } +} + +// checkVersion checks the minimum version of the specified language is supported. +// Note: While it is likely, there are no guarantees that a newer version of the language will work +func checkVersion(lang string, minVersion string) error { + var compare []string + + switch lang { + case "go": + // Version can/cannot include patch version e.g. + // - go version go1.19 darwin/arm64 + // - go version go1.19.2 darwin/amd64 + goVersionRegex := regexp.MustCompile("go([0-9]+).([0-9]+).?([0-9]+)?") + v, err := sh.Output("go", "version") + if err != nil { + return fmt.Errorf("unexpected go error: %v", err) + } + compare = goVersionRegex.FindStringSubmatch(v) + if len(compare) != 4 { + return fmt.Errorf("unexpected go semver: %q", v) + } + case "tinygo": + tinygoVersionRegex := regexp.MustCompile("tinygo version ([0-9]+).([0-9]+).?([0-9]+)?") + v, err := sh.Output("tinygo", "version") + if err != nil { + return fmt.Errorf("unexpected tinygo error: %v", err) + } + // Assume a dev build is valid. + if strings.Contains(v, "-dev") { + return nil + } + compare = tinygoVersionRegex.FindStringSubmatch(v) + if len(compare) != 4 { + return fmt.Errorf("unexpected tinygo semver: %q", v) + } + default: + return fmt.Errorf("unexpected language: %s", lang) + } + + compare = compare[1:] + if compare[2] == "" { + compare[2] = "0" + } + + base := strings.SplitN(minVersion, ".", 3) + if len(base) == 2 { + base = append(base, "0") + } + for i := 0; i < 3; i++ { + baseN, _ := strconv.Atoi(base[i]) + compareN, _ := strconv.Atoi(compare[i]) + if baseN > compareN { + return fmt.Errorf("unexpected %s version, minimum want %q, have %q", lang, minVersion, strings.Join(compare, ".")) + } + } + return nil +} + // Format formats code in this repository. func Format() error { if err := sh.RunV("go", "mod", "tidy"); err != nil { diff --git a/testing/coreruleset/go.mod b/testing/coreruleset/go.mod index 5056497..caa4c75 100644 --- a/testing/coreruleset/go.mod +++ b/testing/coreruleset/go.mod @@ -1,6 +1,6 @@ module github.com/jcchavezs/coraza-http-wasm/testing/coreruleset -go 1.21.0 +go 1.22 require ( github.com/bmatcuk/doublestar/v4 v4.6.1 From f33ef5e1cca0539d860fd6aa59dcb826b391b374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Wed, 2 Oct 2024 13:24:00 +0200 Subject: [PATCH 2/2] fix: tinygo version. --- .github/workflows/ci.yaml | 2 +- .github/workflows/nightly-coraza-check.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ef5457c..711aea5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,7 +16,7 @@ on: env: GO_VERSION: "1.22" - TINYGO_VERSION: "0.33" + TINYGO_VERSION: "0.33.0" jobs: build: diff --git a/.github/workflows/nightly-coraza-check.yaml b/.github/workflows/nightly-coraza-check.yaml index e28f346..1b7541e 100644 --- a/.github/workflows/nightly-coraza-check.yaml +++ b/.github/workflows/nightly-coraza-check.yaml @@ -9,7 +9,7 @@ on: env: GO_VERSION: "1.22" - TINYGO_VERSION: "0.33" + TINYGO_VERSION: "0.33.0" jobs: test: