From cdbd99b2b8cbec887ccb4ef004f3f91cb22f6d21 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 14 Oct 2022 18:55:18 +0000 Subject: [PATCH 1/2] Use own Go version instead of hardcoded 1.17 for `make fmt` Signed-off-by: Yarden Shoham --- build/code-batch-process.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 24b13470ab459..2f8ed0128dcd4 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -13,6 +13,7 @@ import ( "os/exec" "path/filepath" "regexp" + "runtime/debug" "strconv" "strings" @@ -206,6 +207,21 @@ Example: `, "file-batch-exec") } +func getGoVersion() string { + goModFile, err := os.ReadFile("go.mod") + if err != nil { + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + log.Fatalf("Failed to read build info") + os.Exit(1) + } + return strings.TrimPrefix(buildInfo.GoVersion, "go") + } + goModVersionRegex := regexp.MustCompile(`go \d+\.\d+`) + goModVersionLine := goModVersionRegex.Find(goModFile) + return string(goModVersionLine[3:]) +} + func newFileCollectorFromMainOptions(mainOptions map[string]string) (fc *fileCollector, err error) { fileFilter := mainOptions["file-filter"] if fileFilter == "" { @@ -270,7 +286,7 @@ func main() { log.Print("the -d option is not supported by gitea-fmt") } cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-l"), containsString(subArgs, "-w"))) - cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("GOFUMPT_PACKAGE"), "-extra", "-lang", "1.17"}, substArgs...))) + cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("GOFUMPT_PACKAGE"), "-extra", "-lang", getGoVersion()}, substArgs...))) case "misspell": cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("MISSPELL_PACKAGE")}, substArgs...))) default: From 8ed1935faeec5a9d8fa524a2b891409d6170c695 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 15 Oct 2022 11:18:45 +0000 Subject: [PATCH 2/2] Remove fallback to build info Signed-off-by: Yarden Shoham --- build/code-batch-process.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 2f8ed0128dcd4..dce5288cf3771 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -13,7 +13,6 @@ import ( "os/exec" "path/filepath" "regexp" - "runtime/debug" "strconv" "strings" @@ -210,12 +209,8 @@ Example: func getGoVersion() string { goModFile, err := os.ReadFile("go.mod") if err != nil { - buildInfo, ok := debug.ReadBuildInfo() - if !ok { - log.Fatalf("Failed to read build info") - os.Exit(1) - } - return strings.TrimPrefix(buildInfo.GoVersion, "go") + log.Fatalf(`Faild to read "go.mod": %v`, err) + os.Exit(1) } goModVersionRegex := regexp.MustCompile(`go \d+\.\d+`) goModVersionLine := goModVersionRegex.Find(goModFile)