mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2026-01-10 18:24:25 +08:00
BUG: Output better "version" string when running main.go directly (#3658)
This commit is contained in:
parent
a0d04a181a
commit
74e1bb50da
2 changed files with 33 additions and 36 deletions
|
|
@ -6,16 +6,15 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var sha = flag.String("sha", "", "SHA of current commit")
|
||||
"github.com/StackExchange/dnscontrol/v4/pkg/version"
|
||||
)
|
||||
|
||||
var goos = flag.String("os", "", "OS to build (linux, windows, or darwin) Defaults to all.")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
flags := fmt.Sprintf(`-s -w -X "github.com/StackExchange/dnscontrol/v4/pkg/version.version=%s"`, getVersion())
|
||||
flags := fmt.Sprintf(`-s -w -X "github.com/StackExchange/dnscontrol/v4/pkg/version.version=%s"`, version.Version())
|
||||
pkg := "github.com/StackExchange/dnscontrol/v4"
|
||||
|
||||
build := func(out, goos string) {
|
||||
|
|
@ -50,32 +49,3 @@ func main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getVersion() string {
|
||||
if *sha != "" {
|
||||
return *sha
|
||||
}
|
||||
// check teamcity build version
|
||||
if v := os.Getenv("BUILD_VCS_NUMBER"); v != "" {
|
||||
return v
|
||||
}
|
||||
// check git
|
||||
cmd := exec.Command("git", "rev-parse", "HEAD")
|
||||
v, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
ver := strings.TrimSpace(string(v))
|
||||
// see if dirty
|
||||
cmd = exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
|
||||
err = cmd.Run()
|
||||
// exit status 1 indicates dirty tree
|
||||
if err != nil {
|
||||
if err.Error() == "exit status 1" {
|
||||
ver += "[dirty]"
|
||||
} else {
|
||||
log.Printf("!%s!", err.Error())
|
||||
}
|
||||
}
|
||||
return ver
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,44 @@
|
|||
package version
|
||||
|
||||
import "runtime/debug"
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Set by GoReleaser
|
||||
var version string
|
||||
|
||||
// VCSVersion retrieves the version information from git.
|
||||
//
|
||||
// If the current commit is untagged, the version string will show the last
|
||||
// tag, followed by the number of commits since the tag, then the short
|
||||
// hash of the current commit.
|
||||
//
|
||||
// If the tree is dirty, "-dirty" is appended.
|
||||
func VCSVersion() string {
|
||||
cmd := exec.Command("git", "describe", "--tags", "--always", "--dirty")
|
||||
v, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
ver := strings.TrimSpace(string(v))
|
||||
return ver
|
||||
}
|
||||
|
||||
// Version returns either the tag set by GoReleaser, or the version information
|
||||
// from Git.
|
||||
func Version() string {
|
||||
if version != "" {
|
||||
return version
|
||||
}
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return "dev"
|
||||
if !ok ||
|
||||
// When running with "go run main.go" no module information is available
|
||||
bi.Main.Version == "" ||
|
||||
// Go gives no commit information if not on a tag
|
||||
bi.Main.Version == "(devel)" {
|
||||
return VCSVersion()
|
||||
}
|
||||
return bi.Main.Version
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue