main: bake build date into version string

$ gocryptfs -version
gocryptfs v0.12-36-ge021b9d-dirty; go-fuse a4c968c; 2016-07-03 go1.6.2
This commit is contained in:
Jakob Unterwurzacher 2016-07-03 16:49:42 +02:00
parent e021b9d00c
commit 0d5d6fc99b
2 changed files with 24 additions and 8 deletions

View File

@ -24,6 +24,9 @@ GITVERSIONFUSE=$(
fi
)
# Build Unix timestamp, something like 1467554204.
BUILDTIME=$(date +%s)
# Make sure we have the go binary
go version > /dev/null
@ -32,10 +35,10 @@ V=$(go version | cut -d" " -f3 | cut -c3-5)
if [ $V == "1.3" -o $V == "1.4" ]
then
go build -ldflags="-X main.GitVersion $GITVERSION -X main.GitVersionFuse $GITVERSIONFUSE"
go build -ldflags="-X main.GitVersion $GITVERSION -X main.GitVersionFuse $GITVERSIONFUSE -X main.BuildTime $BUILDTIME"
else
# Go 1.5 wants an "=" here
go build -ldflags="-X main.GitVersion=$GITVERSION -X main.GitVersionFuse=$GITVERSIONFUSE"
go build -ldflags="-X main.GitVersion=$GITVERSION -X main.GitVersionFuse=$GITVERSIONFUSE -X main.BuildTime=$BUILDTIME"
fi
./gocryptfs -version

25
main.go
View File

@ -51,9 +51,16 @@ type argContainer struct {
var flagSet *flag.FlagSet
// GitVersion will be set by the build script "build.bash"
var GitVersion = "[version not set - please compile using ./build.bash]"
var GitVersionFuse = "[version not set - please compile using ./build.bash]"
const pleaseBuildBash = "[not set - please compile using ./build.bash]"
// gocryptfs version according to git, set by build.bash
var GitVersion = pleaseBuildBash
// go-fuse library version, set by build.bash
var GitVersionFuse = pleaseBuildBash
// Unix timestamp, set by build.bash
var BuildTime = "0"
// initDir initializes an empty directory for use as a gocryptfs cipherdir.
func initDir(args *argContainer) {
@ -145,11 +152,17 @@ func changePassword(args *argContainer) {
os.Exit(0)
}
// printVersion - print a version string like
// "gocryptfs v0.3.1-31-g6736212-dirty; on-disk format 2"
// printVersion prints a version string like this:
// gocryptfs v0.12-36-ge021b9d-dirty; go-fuse a4c968c; 2016-07-03 go1.6.2
func printVersion() {
humanTime := "0000-00-00"
if i, _ := strconv.ParseInt(BuildTime, 10, 64); i > 0 {
t := time.Unix(i, 0).UTC()
humanTime = fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
}
built := fmt.Sprintf("%s %s", humanTime, runtime.Version())
fmt.Printf("%s %s; go-fuse %s; %s\n",
tlog.ProgramName, GitVersion, GitVersionFuse, runtime.Version())
tlog.ProgramName, GitVersion, GitVersionFuse, built)
}
func main() {