Skip to content

Commit b7d1908

Browse files
authored
Avoids missing version description when using go install (#600)
1 parent 9c30b44 commit b7d1908

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

main.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9+
"runtime/debug"
910
"strings"
1011

1112
"github.com/fatih/color"
@@ -213,13 +214,30 @@ func init() {
213214

214215
// Output build info (version, commit, date and builtBy)
215216
if versionFlag {
216-
fmt.Printf(
217-
"Version:\t%s\nCommit:\t\t%s\nBuilt\t\t%s by %s\n",
218-
version,
219-
commit,
220-
date,
221-
builtBy,
222-
)
217+
var buildInfo string
218+
if date != "unknown" && builtBy != "unknown" {
219+
buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
220+
}
221+
222+
if commit != "none" {
223+
buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
224+
}
225+
226+
if version == "dev" {
227+
bi, ok := debug.ReadBuildInfo()
228+
if ok {
229+
version = bi.Main.Version
230+
if strings.HasPrefix(version, "v") {
231+
version = bi.Main.Version[1:]
232+
}
233+
if len(buildInfo) == 0 {
234+
fmt.Printf("version %s\n", version)
235+
os.Exit(0)
236+
}
237+
}
238+
}
239+
240+
fmt.Printf("Version:\t%s\n%s", version, buildInfo)
223241
os.Exit(0)
224242
}
225243
}

0 commit comments

Comments
 (0)