mirror of
https://github.com/safedep/vet.git
synced 2025-12-11 09:25:44 -06:00
* fix: missing version information while building using go install * refactor: missing version on go install builds * refactor: go version set by both ldflags and go install(runtime) Signed-off-by: Kunal Singh <kunalsin9h@gmail.com> --------- Signed-off-by: Kunal Singh <kunalsin9h@gmail.com>
41 lines
951 B
Go
41 lines
951 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
runtimeDebug "runtime/debug"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// When building with CI or Make, version is set using `ldflags`
|
|
var version string
|
|
var commit string
|
|
|
|
func init() {
|
|
// Only use buildInfo if version wasn't set by ldflags, that is its being build by `go install`
|
|
if version == "" {
|
|
// Main.Version is based on the version control system tag or commit.
|
|
// This useful when app is build with `go install`
|
|
// See: https://antonz.org/go-1-24/#main-modules-version
|
|
var buildInfo, _ = runtimeDebug.ReadBuildInfo()
|
|
version = buildInfo.Main.Version
|
|
}
|
|
}
|
|
|
|
func newVersionCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "version",
|
|
Short: "Show version and build information",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Fprintf(os.Stdout, "Version: %s\n", version)
|
|
fmt.Fprintf(os.Stdout, "CommitSHA: %s\n", commit)
|
|
|
|
os.Exit(1)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
return cmd
|
|
}
|