git » summer » commit cb0d27e

Add version information

author Alberto Bertogli
2023-04-02 09:22:49 UTC
committer Alberto Bertogli
2023-04-02 09:22:49 UTC
parent 0cb02bd68b40a32a156cf6162c705f8374f2fe00

Add version information

summer.go +5 -1
test/help.t +11 -0
ui.go +17 -0

diff --git a/summer.go b/summer.go
index bcbaf39..2375457 100644
--- a/summer.go
+++ b/summer.go
@@ -27,6 +27,8 @@ Usage:
   summer generate <dir>
       Write checksums for the given directory. Pre-existing checksums are
       overwritten without verification.
+  summer version
+      Print software version information.
 
 Flags:
 `
@@ -49,7 +51,7 @@ func main() {
 	op := flag.Arg(0)
 	root := flag.Arg(1)
 
-	if root == "" {
+	if op != "version" && root == "" {
 		Usage()
 		os.Exit(1)
 	}
@@ -70,6 +72,8 @@ func main() {
 		err = verify(db, root)
 	case "update":
 		err = update(db, root)
+	case "version":
+		PrintVersion()
 	default:
 		Fatalf("unknown command %q", op)
 	}
diff --git a/test/help.t b/test/help.t
index fbbb18f..ffb1568 100644
--- a/test/help.t
+++ b/test/help.t
@@ -22,6 +22,8 @@ No arguments.
     summer generate <dir>
         Write checksums for the given directory. Pre-existing checksums are
         overwritten without verification.
+    summer version
+        Print software version information.
   
   Flags:
     -db string
@@ -51,6 +53,8 @@ Too few arguments.
     summer generate <dir>
         Write checksums for the given directory. Pre-existing checksums are
         overwritten without verification.
+    summer version
+        Print software version information.
   
   Flags:
     -db string
@@ -80,6 +84,8 @@ No valid path (the argument is given, but it is empty).
     summer generate <dir>
         Write checksums for the given directory. Pre-existing checksums are
         overwritten without verification.
+    summer version
+        Print software version information.
   
   Flags:
     -db string
@@ -94,3 +100,8 @@ Unknown command.
   $ summer badcommand .
   unknown command "badcommand"
   [1]
+
+Version information.
+
+  $ summer version
+  summer version \w+ \(....-..-.. ..:..:.. .*\) (re)
diff --git a/ui.go b/ui.go
index 32b0b56..ca0d49c 100644
--- a/ui.go
+++ b/ui.go
@@ -4,6 +4,8 @@ import (
 	"flag"
 	"fmt"
 	"os"
+	"runtime/debug"
+	"time"
 )
 
 var (
@@ -53,3 +55,18 @@ func PrintModified(path string) {
 func PrintMatched(path string) {
 	Verbosef("%q: match", path)
 }
+
+func PrintVersion() {
+	info, _ := debug.ReadBuildInfo()
+	rev := ""
+	ts := time.Time{}
+	for _, s := range info.Settings {
+		switch s.Key {
+		case "vcs.revision":
+			rev = s.Value
+		case "vcs.time":
+			ts, _ = time.Parse(time.RFC3339, s.Value)
+		}
+	}
+	Printf("summer version %s (%s)", rev, ts)
+}