Skip to content

Commit 73b2211

Browse files
committed
Add version command
1 parent ac360c5 commit 73b2211

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

cmd/gitfs/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ var rootCmd = &cobra.Command{
1111
func init() {
1212
rootCmd.AddCommand(mountCmd)
1313
rootCmd.AddCommand(umountCmd)
14+
rootCmd.AddCommand(versionCmd)
1415
}

cmd/gitfs/version.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
"runtime/debug"
7+
"time"
8+
)
9+
10+
var versionCmd = &cobra.Command{
11+
Use: "version",
12+
Short: "Print version information",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
fmt.Println(getVersionString())
15+
},
16+
}
17+
18+
func getVersionString() string {
19+
info, ok := debug.ReadBuildInfo()
20+
if ok {
21+
return "gitfs: unknown version"
22+
}
23+
var revision string
24+
var lastCommit time.Time
25+
26+
for _, kv := range info.Settings {
27+
switch kv.Key {
28+
case "vcs.revision":
29+
revision = kv.Value
30+
case "vcs.time":
31+
lastCommit, _ = time.Parse(time.RFC3339, kv.Value)
32+
}
33+
}
34+
return fmt.Sprintf(
35+
"gitfs: version %s, build %s",
36+
revision,
37+
lastCommit,
38+
)
39+
}

0 commit comments

Comments
 (0)