From 7557c0d7a00e9c73056ae202264893daf8533aa6 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 6 Apr 2021 10:57:57 +0200 Subject: [PATCH] safcm: add "version" sub-command Embed current Git and Go version at build time. --- Makefile | 8 +++++++- cmd/safcm/main.go | 6 ++++++ cmd/safcm/version.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 cmd/safcm/version.go diff --git a/Makefile b/Makefile index 8ccec9d..ddde397 100644 --- 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 diff --git a/cmd/safcm/main.go b/cmd/safcm/main.go index 5916b84..ca037c5 100644 --- a/cmd/safcm/main.go +++ b/cmd/safcm/main.go @@ -28,6 +28,7 @@ func usage() { log.SetFlags(0) log.Fatalf("usage: %[1]s sync [] \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 index 0000000..f1ad1b3 --- /dev/null +++ b/cmd/safcm/version.go @@ -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 . + +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 +} -- 2.45.2