From cbda3eaac1f4f648debefd8349f6b5821ca976bb Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Wed, 7 Feb 2024 16:27:21 +0100 Subject: [PATCH 1/2] make buildutil compatible with 'go work' --- cmd/buildutil/info.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/buildutil/info.go b/cmd/buildutil/info.go index afc2585..5e16f07 100644 --- a/cmd/buildutil/info.go +++ b/cmd/buildutil/info.go @@ -223,8 +223,8 @@ func CollectBuildInformation(ctx context.Context, p BuildParameters) (BuildInfo, e := NewChainExecutor(ctx) info.BuildDate = time.Now().Format(time.RFC3339) - info.Go.Module = e.OutputString(p.GoCommand, "list", "-m", "-mod=mod") - info.Go.Dir = e.OutputString(p.GoCommand, "list", "-m", "-mod=mod", "-f", "{{.Dir}}") + info.Go.Module = e.OutputString(p.GoCommand, "list") + info.Go.Dir = e.OutputString(p.GoCommand, "list", "-f", "{{.Dir}}") info.System.OS = e.OutputString(p.GoCommand, "env", "GOOS") info.System.Arch = e.OutputString(p.GoCommand, "env", "GOARCH") info.System.Ext = e.OutputString(p.GoCommand, "env", "GOEXE") From 3bd6b470259852c501d2219415668926b9f26526 Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Wed, 7 Feb 2024 16:50:37 +0100 Subject: [PATCH 2/2] do not use go list --- buildutil | 2 +- cmd/buildutil/info.go | 28 ++++++++++++++++++++++++++-- go.mod | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/buildutil b/buildutil index 3031dbf..b52809a 100755 --- a/buildutil +++ b/buildutil @@ -2,4 +2,4 @@ set -eu go mod vendor go generate ./... -exec go run github.com/rebuy-de/rebuy-go-sdk/v6/cmd/buildutil "$@" +exec go run ./cmd/buildutil "$@" diff --git a/cmd/buildutil/info.go b/cmd/buildutil/info.go index 5e16f07..cdf6345 100644 --- a/cmd/buildutil/info.go +++ b/cmd/buildutil/info.go @@ -15,6 +15,7 @@ import ( "github.com/pkg/errors" "github.com/rebuy-de/rebuy-go-sdk/v6/pkg/cmdutil" "github.com/sirupsen/logrus" + "golang.org/x/mod/modfile" "golang.org/x/tools/go/packages" ) @@ -210,6 +211,20 @@ type TargetInfo struct { PGO string } +func ReadGoMod() (*modfile.File, error) { + data, err := os.ReadFile("go.mod") + if err != nil { + return nil, fmt.Errorf("read go.mod file: %w", err) + } + + file, err := modfile.Parse("go.mod", data, nil) + if err != nil { + return nil, fmt.Errorf("parse go.mod file: %w", err) + } + + return file, nil +} + func CollectBuildInformation(ctx context.Context, p BuildParameters) (BuildInfo, error) { var ( err error @@ -220,11 +235,20 @@ func CollectBuildInformation(ctx context.Context, p BuildParameters) (BuildInfo, logrus.Info("Collecting build information") + gomod, err := ReadGoMod() + if err != nil { + return info, err + } + + info.Go.Module = gomod.Module.Mod.Path + info.Go.Dir, err = os.Getwd() + if err != nil { + return info, err + } + e := NewChainExecutor(ctx) info.BuildDate = time.Now().Format(time.RFC3339) - info.Go.Module = e.OutputString(p.GoCommand, "list") - info.Go.Dir = e.OutputString(p.GoCommand, "list", "-f", "{{.Dir}}") info.System.OS = e.OutputString(p.GoCommand, "env", "GOOS") info.System.Arch = e.OutputString(p.GoCommand, "env", "GOARCH") info.System.Ext = e.OutputString(p.GoCommand, "env", "GOEXE") diff --git a/go.mod b/go.mod index acb4e34..55bbff2 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,7 @@ require ( github.com/stretchr/testify v1.8.4 github.com/tidwall/pretty v1.2.1 golang.org/x/exp v0.0.0-20240119083558-1b970713d09a + golang.org/x/mod v0.14.0 golang.org/x/oauth2 v0.16.0 golang.org/x/sync v0.6.0 golang.org/x/tools v0.17.0 @@ -136,7 +137,6 @@ require ( github.com/yuin/gopher-lua v1.1.0 // indirect gitlab.com/digitalxero/go-conventional-commit v1.0.7 // indirect golang.org/x/crypto v0.18.0 // indirect - golang.org/x/mod v0.14.0 // indirect golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/term v0.16.0 // indirect