From a6a7b157582062d9762c01642db1ce3e7e3357ef Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Thu, 30 Nov 2023 19:22:09 +0100 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20support=20go=20mod=20versi?= =?UTF-8?q?on=20pinning=20in=20version=20command=20(#2711)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To pin a version just add a simple comment in front of the dependency in go.mod: ``` // pin v1.1.0-rc5 github.com/opencontainers/image-spec v1.1.0-rc5 ``` The `version` cmd is respecting the pinning during go mod version bumps. --- go.mod | 2 ++ providers-sdk/v1/util/version/version.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/go.mod b/go.mod index 1c9497e52b..eba12c76bd 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/Masterminds/semver v1.5.0 github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c github.com/StackExchange/wmi v1.2.1 + // pin v0.3.0 github.com/alecthomas/participle v0.3.0 github.com/alecthomas/participle/v2 v2.1.0 github.com/aws/aws-sdk-go v1.48.7 @@ -68,6 +69,7 @@ require ( github.com/muesli/reflow v0.3.0 github.com/muesli/termenv v0.15.2 github.com/olekukonko/tablewriter v0.0.5 + // pin v1.1.0-rc5 github.com/opencontainers/image-spec v1.1.0-rc5 github.com/package-url/packageurl-go v0.1.2 github.com/pierrec/lz4/v4 v4.1.18 diff --git a/providers-sdk/v1/util/version/version.go b/providers-sdk/v1/util/version/version.go index 465b3fc361..249bbb8d4c 100644 --- a/providers-sdk/v1/util/version/version.go +++ b/providers-sdk/v1/util/version/version.go @@ -155,6 +155,17 @@ func checkGoModUpdate(providerPath string, updateStrategy UpdateStrategy) { modPath = require.Mod.Path + "@" + require.Mod.Version } + if require.Syntax.Comments.Before != nil { + for i := range require.Syntax.Comments.Before { + comment := require.Syntax.Comments.Before[i].Token + if strings.HasPrefix(comment, "// pin") { + version := strings.TrimSpace(strings.TrimPrefix(comment, "// pin")) + log.Info().Msgf("Found pin comment for %s: %s", require.Mod.Path, version) + modPath = require.Mod.Path + "@" + version + } + } + } + cmd := exec.Command("go", "get", "-u", modPath) // Redirect standard output and standard error to the console