Skip to content

Commit 64e22cf

Browse files
committed
Merge branch 'main' into Issue-83-better-help
2 parents fdfa993 + e2792b5 commit 64e22cf

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

cmd/main.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"flag"
56
"fmt"
67
"os"
8+
"runtime/debug"
79
"strings"
10+
"text/tabwriter"
11+
"time"
812
)
913

1014
var (
@@ -84,7 +88,7 @@ func exitTerseUsage() {
8488
// Keep this in sync with the README.
8589
func usage() string {
8690
usage := "GoCSV is a command line CSV processing tool.\n"
87-
usage += fmt.Sprintf("Version: %s (%s)\n", VERSION, GIT_HASH)
91+
usage += version() + "\n"
8892
usage += "Subcommands:\n"
8993
for _, subcommand := range subcommands {
9094
usage += usageForSubcommand(subcommand)
@@ -93,6 +97,39 @@ func usage() string {
9397
return usage
9498
}
9599

100+
func version() string {
101+
if VERSION != "" && GIT_HASH != "" {
102+
return fmt.Sprintf("Version: %s (%s)", VERSION, GIT_HASH)
103+
}
104+
105+
s := ""
106+
if bi, ok := debug.ReadBuildInfo(); ok {
107+
s += "go:\t" + bi.GoVersion + "\n"
108+
for _, x := range bi.Settings {
109+
if x.Key == "vcs.revision" {
110+
// short hash
111+
s += "vcs.revision:\t" + x.Value[:7] + "\n"
112+
}
113+
if x.Key == "vcs.time" {
114+
t, _ := time.Parse(time.RFC3339, x.Value)
115+
t = t.Local()
116+
s += "vcs.time:\t" + t.Format(time.RFC3339) + "\n"
117+
}
118+
if x.Key == "vcs.modified" {
119+
s += "vcs.modified:\t" + x.Value + "\n"
120+
}
121+
}
122+
}
123+
s += "local-build:\t" + time.Now().Format(time.RFC3339)
124+
125+
buf := &bytes.Buffer{}
126+
w := tabwriter.NewWriter(buf, 0, 0, 1, ' ', 0)
127+
fmt.Fprint(w, s)
128+
w.Flush()
129+
130+
return buf.String()
131+
}
132+
96133
func Main() {
97134
args := os.Args
98135
if len(args) == 1 {
@@ -106,7 +143,7 @@ func Main() {
106143
fmt.Println(usage())
107144
return
108145
case "version":
109-
fmt.Printf("%s (%s)\n", VERSION, GIT_HASH)
146+
fmt.Println(version())
110147
return
111148
}
112149

0 commit comments

Comments
 (0)