Skip to content

Commit

Permalink
source version info from go debug info so it works with go install
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Oct 26, 2023
1 parent a99fa9b commit 871a609
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"runtime/debug"
"strconv"

"github.com/spf13/cobra"
"github.com/xigxog/fox/internal/log"
"github.com/xigxog/kubefox/libs/core/kubefox"
)

var verCmd = &cobra.Command{
Expand All @@ -20,8 +22,30 @@ func init() {
}

func runVer(cmd *cobra.Command, args []string) {
var (
version, commit, date string
modified bool = true
)
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
commit = s.Value
case "vcs.time":
date = s.Value
case "vcs.modified":
modified, _ = strconv.ParseBool(s.Value)
}
}
}

if modified {
log.Warn("binary built from source with uncommitted changes")
}
log.Marshal(map[string]any{
"gitCommit": kubefox.GitCommit,
"gitRef": kubefox.GitRef,
"version": version,
"commit": commit,
"date": date,
})
}

0 comments on commit 871a609

Please sign in to comment.