]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
safcm: add "version" sub-command
authorSimon Ruderich <simon@ruderich.org>
Tue, 6 Apr 2021 08:57:57 +0000 (10:57 +0200)
committerSimon Ruderich <simon@ruderich.org>
Tue, 6 Apr 2021 08:57:57 +0000 (10:57 +0200)
Embed current Git and Go version at build time.

Makefile
cmd/safcm/main.go
cmd/safcm/version.go [new file with mode: 0644]

index 8ccec9daa1fa3489f6d46ac19396a1ff62b8e2ad..ddde397b2ce427b5995cce8933a426332b17aa36 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,15 @@
+LDFLAGS = '\
+    -X "main.versionGit=$(shell git describe --long --tags --dirty --always)" \
+    -X "main.versionGo=$(shell go version)" \
+'
+
+
 all: safcm
 
 safcm:
        go fmt ./...
        cd cmd/safcm-remote && ./build.sh
-       go build -race ruderich.org/simon/safcm/cmd/safcm
+       go build -ldflags $(LDFLAGS) -race ruderich.org/simon/safcm/cmd/safcm
        @# For proper permissions after initial clone with a strict umask
        cd cmd/safcm/testdata/project && ../../../../safcm fixperms 2> /dev/null
 
index 5916b848060a583d30c8e9208d948e01511926e0..ca037c539a12451f7d5ba51f284af306a346551d 100644 (file)
@@ -28,6 +28,7 @@ func usage() {
        log.SetFlags(0)
        log.Fatalf("usage: %[1]s sync [<options>] <host|group...>\n"+
                "       %[1]s fixperms\n"+
+               "       %[1]s version\n"+
                "", os.Args[0])
 }
 
@@ -45,6 +46,11 @@ func main() {
                        usage()
                }
                err = MainFixperms()
+       case "version":
+               if len(os.Args) != 2 {
+                       usage()
+               }
+               err = MainVersion()
        default:
                usage()
        }
diff --git a/cmd/safcm/version.go b/cmd/safcm/version.go
new file mode 100644 (file)
index 0000000..f1ad1b3
--- /dev/null
@@ -0,0 +1,35 @@
+// "version" sub-command: display version information
+
+// Copyright (C) 2021  Simon Ruderich
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+package main
+
+import (
+       "fmt"
+)
+
+// Set via -ldflags -X during build
+var (
+       versionGit string
+       versionGo  string
+)
+
+func MainVersion() error {
+       fmt.Printf("safcm %s, compiled with %s\n",
+               versionGit,
+               versionGo)
+       return nil
+}