Skip to content

Commit 74e1bb5

Browse files
authored
BUG: Output better "version" string when running main.go directly (#3658)
1 parent a0d04a1 commit 74e1bb5

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

build/build.go

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import (
66
"log"
77
"os"
88
"os/exec"
9-
"strings"
10-
)
119

12-
var sha = flag.String("sha", "", "SHA of current commit")
10+
"github.com/StackExchange/dnscontrol/v4/pkg/version"
11+
)
1312

1413
var goos = flag.String("os", "", "OS to build (linux, windows, or darwin) Defaults to all.")
1514

1615
func main() {
1716
flag.Parse()
18-
flags := fmt.Sprintf(`-s -w -X "github.com/StackExchange/dnscontrol/v4/pkg/version.version=%s"`, getVersion())
17+
flags := fmt.Sprintf(`-s -w -X "github.com/StackExchange/dnscontrol/v4/pkg/version.version=%s"`, version.Version())
1918
pkg := "github.com/StackExchange/dnscontrol/v4"
2019

2120
build := func(out, goos string) {
@@ -50,32 +49,3 @@ func main() {
5049
}
5150
}
5251
}
53-
54-
func getVersion() string {
55-
if *sha != "" {
56-
return *sha
57-
}
58-
// check teamcity build version
59-
if v := os.Getenv("BUILD_VCS_NUMBER"); v != "" {
60-
return v
61-
}
62-
// check git
63-
cmd := exec.Command("git", "rev-parse", "HEAD")
64-
v, err := cmd.CombinedOutput()
65-
if err != nil {
66-
return ""
67-
}
68-
ver := strings.TrimSpace(string(v))
69-
// see if dirty
70-
cmd = exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
71-
err = cmd.Run()
72-
// exit status 1 indicates dirty tree
73-
if err != nil {
74-
if err.Error() == "exit status 1" {
75-
ver += "[dirty]"
76-
} else {
77-
log.Printf("!%s!", err.Error())
78-
}
79-
}
80-
return ver
81-
}

pkg/version/version.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
package version
22

3-
import "runtime/debug"
3+
import (
4+
"os/exec"
5+
"runtime/debug"
6+
"strings"
7+
)
48

59
// Set by GoReleaser
610
var version string
711

12+
// VCSVersion retrieves the version information from git.
13+
//
14+
// If the current commit is untagged, the version string will show the last
15+
// tag, followed by the number of commits since the tag, then the short
16+
// hash of the current commit.
17+
//
18+
// If the tree is dirty, "-dirty" is appended.
19+
func VCSVersion() string {
20+
cmd := exec.Command("git", "describe", "--tags", "--always", "--dirty")
21+
v, err := cmd.CombinedOutput()
22+
if err != nil {
23+
return ""
24+
}
25+
ver := strings.TrimSpace(string(v))
26+
return ver
27+
}
28+
29+
// Version returns either the tag set by GoReleaser, or the version information
30+
// from Git.
831
func Version() string {
932
if version != "" {
1033
return version
1134
}
1235
bi, ok := debug.ReadBuildInfo()
13-
if !ok {
14-
return "dev"
36+
if !ok ||
37+
// When running with "go run main.go" no module information is available
38+
bi.Main.Version == "" ||
39+
// Go gives no commit information if not on a tag
40+
bi.Main.Version == "(devel)" {
41+
return VCSVersion()
1542
}
1643
return bi.Main.Version
1744
}

0 commit comments

Comments
 (0)