From b35da82d3c807cefc560322a1cd4a97238cf7835 Mon Sep 17 00:00:00 2001 From: Christian Ege Date: Tue, 14 May 2024 14:56:05 +0200 Subject: [PATCH] fix: use the right version information when go install is used Signed-off-by: Christian Ege --- cmd/ovp8xx/cmd/root.go | 3 +++ cmd/ovp8xx/ovp8xx.go | 10 +++++++ internal/versioninfo/version.go | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 internal/versioninfo/version.go diff --git a/cmd/ovp8xx/cmd/root.go b/cmd/ovp8xx/cmd/root.go index dce12e1..b1b5435 100644 --- a/cmd/ovp8xx/cmd/root.go +++ b/cmd/ovp8xx/cmd/root.go @@ -11,6 +11,8 @@ import ( "github.com/spf13/cobra" ) +// SetVersionInfo sets the version information for the root command. +// It formats the version, commit, and date into a string and assigns it to the rootCmd.Version variable. func SetVersionInfo(version, commit, date string) { rootCmd.Version = fmt.Sprintf("%s (Built on %s from Git SHA %s)", version, date, commit) } @@ -33,4 +35,5 @@ func Execute() { func init() { rootCmd.PersistentFlags().String("ip", ovp8xx.GetEnv("OVP8XX_IP", "192.168.0.69"), "The IP address or hostname of the OVP8XX. If not provided the default will be taken from the environment variable OVP8XX_IP") + } diff --git a/cmd/ovp8xx/ovp8xx.go b/cmd/ovp8xx/ovp8xx.go index 89e8b00..e7a34f1 100644 --- a/cmd/ovp8xx/ovp8xx.go +++ b/cmd/ovp8xx/ovp8xx.go @@ -2,6 +2,7 @@ package main import ( "github.com/graugans/go-ovp8xx/v2/cmd/ovp8xx/cmd" + "github.com/graugans/go-ovp8xx/v2/internal/versioninfo" ) var ( @@ -11,6 +12,15 @@ var ( ) func main() { + // If the version is "dev", it means that the binary is built using "go install", + // "go build" or "go run". + // However, if the binary is build by Goreleaser we use that version. + if version == "dev" { + version = versioninfo.Version + commit = versioninfo.Revision + date = versioninfo.LastCommit.String() + } + cmd.SetVersionInfo( version, commit, diff --git a/internal/versioninfo/version.go b/internal/versioninfo/version.go new file mode 100644 index 0000000..63335e9 --- /dev/null +++ b/internal/versioninfo/version.go @@ -0,0 +1,46 @@ +// Package versioninfo uses runtime.ReadBuildInfo() to set global executable revision information if possible. +package versioninfo + +// SPDX-License-Identifier: MIT +// Copyright (c) 2021 Carl Johnson +// Based on https://github.com/earthboundkid/versioninfo + +import ( + "runtime/debug" + "time" +) + +var ( + // Version will be the version tag if the binary is built with "go install url/tool@version". + // If the binary is built some other way, it will be "(devel)". + Version = "unknown" + // Revision is taken from the vcs.revision tag in Go 1.18+. + Revision = "unknown" + // LastCommit is taken from the vcs.time tag in Go 1.18+. + LastCommit time.Time + // DirtyBuild is taken from the vcs.modified tag in Go 1.18+. + DirtyBuild = true +) + +func init() { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + for _, kv := range info.Settings { + if kv.Value == "" { + continue + } + switch kv.Key { + case "vcs.revision": + Revision = kv.Value + case "vcs.time": + LastCommit, _ = time.Parse(time.RFC3339, kv.Value) + case "vcs.modified": + DirtyBuild = kv.Value == "true" + } + } + if info.Main.Version != "" { + Version = info.Main.Version + } +}