Skip to content

Commit

Permalink
fix: use the right version information when go install is used
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Ege <[email protected]>
  • Loading branch information
graugans committed May 14, 2024
1 parent ca35c8e commit b35da82
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/ovp8xx/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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")

}
10 changes: 10 additions & 0 deletions cmd/ovp8xx/ovp8xx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down
46 changes: 46 additions & 0 deletions internal/versioninfo/version.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit b35da82

Please sign in to comment.