From 3dec13c1d77234f3586f17596f65c36dd32e30d0 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Mon, 25 Mar 2024 21:28:09 +0100 Subject: [PATCH] Drop support for Go older than 1.18 (#612) We don't support Go older than 1.21 in the Go modules file, so we can drop the special handling for older Go in the version package. Signed-off-by: SuperQ --- version/info.go | 49 +++++++++++++++++++++++++++++ version/info_default.go | 25 --------------- version/info_go118.go | 69 ----------------------------------------- 3 files changed, 49 insertions(+), 94 deletions(-) delete mode 100644 version/info_default.go delete mode 100644 version/info_go118.go diff --git a/version/info.go b/version/info.go index 044032c7..197d95e5 100644 --- a/version/info.go +++ b/version/info.go @@ -17,6 +17,7 @@ import ( "bytes" "fmt" "runtime" + "runtime/debug" "strings" "text/template" ) @@ -31,6 +32,9 @@ var ( GoVersion = runtime.Version() GoOS = runtime.GOOS GoArch = runtime.GOARCH + + computedRevision string + computedTags string ) // versionInfoTmpl contains the template used by Info. @@ -74,3 +78,48 @@ func Info() string { func BuildContext() string { return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s, tags=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate, GetTags()) } + +func GetRevision() string { + if Revision != "" { + return Revision + } + return computedRevision +} + +func GetTags() string { + return computedTags +} + +func init() { + computedRevision, computedTags = computeRevision() +} + +func computeRevision() (string, string) { + var ( + rev = "unknown" + tags = "unknown" + modified bool + ) + + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + return rev, tags + } + for _, v := range buildInfo.Settings { + if v.Key == "vcs.revision" { + rev = v.Value + } + if v.Key == "vcs.modified" { + if v.Value == "true" { + modified = true + } + } + if v.Key == "-tags" { + tags = v.Value + } + } + if modified { + return rev + "-modified", tags + } + return rev, tags +} diff --git a/version/info_default.go b/version/info_default.go deleted file mode 100644 index 684996f1..00000000 --- a/version/info_default.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2022 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build !go1.18 -// +build !go1.18 - -package version - -func GetRevision() string { - return Revision -} - -func getTags() string { - return "unknown" // Not available prior to Go 1.18 -} diff --git a/version/info_go118.go b/version/info_go118.go deleted file mode 100644 index 992623c6..00000000 --- a/version/info_go118.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2022 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build go1.18 -// +build go1.18 - -package version - -import "runtime/debug" - -var ( - computedRevision string - computedTags string -) - -func GetRevision() string { - if Revision != "" { - return Revision - } - return computedRevision -} - -func GetTags() string { - return computedTags -} - -func init() { - computedRevision, computedTags = computeRevision() -} - -func computeRevision() (string, string) { - var ( - rev = "unknown" - tags = "unknown" - modified bool - ) - - buildInfo, ok := debug.ReadBuildInfo() - if !ok { - return rev, tags - } - for _, v := range buildInfo.Settings { - if v.Key == "vcs.revision" { - rev = v.Value - } - if v.Key == "vcs.modified" { - if v.Value == "true" { - modified = true - } - } - if v.Key == "-tags" { - tags = v.Value - } - } - if modified { - return rev + "-modified", tags - } - return rev, tags -}