From 41abaffd5eacf3f973537146b212dfdf669cb929 Mon Sep 17 00:00:00 2001 From: "koizumi.aoi" Date: Wed, 22 Mar 2023 11:47:03 +0000 Subject: [PATCH] Use internal versioning Signed-off-by: Aoi K git-svn-id: file:///srv/svn/repo/suika/trunk@809 f0ae65fe-ee39-954e-97ec-027ff2717ef4 --- Makefile | 7 ++++++- version.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 version.go diff --git a/Makefile b/Makefile index 8b82393..98f6fbb 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,18 @@ GO ?= go RM ?= rm SCDOC ?= scdoc -GOFLAGS ?= -v +GOFLAGS ?= -v -ldflags "-w -X `go list`.Version=$(VERSION) -X `go list`.Commit=$(COMMIT) -X `go list`.Build=$(BUILD)" PREFIX ?= /usr/local BINDIR ?= bin MANDIR ?= share/man MKDIR ?= mkdir CP ?= cp +VERSION = `git describe --abbrev=0 --tags 2>/dev/null || echo "$VERSION"` +COMMIT = `git rev-parse --short HEAD || echo "$COMMIT"` +BRANCH = `git rev-parse --abbrev-ref HEAD` +BUILD = `git show -s --pretty=format:%cI` + all: suika suikactl suika-znc-import doc/suika.1 suika: diff --git a/version.go b/version.go new file mode 100644 index 0000000..0e5d7a6 --- /dev/null +++ b/version.go @@ -0,0 +1,50 @@ +package suika + +import ( + "fmt" + "runtime/debug" + "strings" +) + +const ( + defaultVersion = "0.0.0" + defaultCommit = "HEAD" + defaultBuild = "0000-01-01:00:00+00:00" +) + +var ( + // Version is the tagged release version in the form .. + // following semantic versioning and is overwritten by the build system. + Version = defaultVersion + + // Commit is the commit sha of the build (normally from Git) and is overwritten + // by the build system. + Commit = defaultCommit + + // Build is the date and time of the build as an RFC3339 formatted string + // and is overwritten by the build system. + Build = defaultBuild +) + +// FullVersion display the full version and build +func FullVersion() string { + var sb strings.Builder + + isDefault := Version == defaultVersion && Commit == defaultCommit && Build == defaultBuild + + if !isDefault { + sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build)) + } + + if info, ok := debug.ReadBuildInfo(); ok { + if isDefault { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Version)) + } + sb.WriteString(fmt.Sprintf(" %s", info.GoVersion)) + if info.Main.Sum != "" { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum)) + } + } + + return sb.String() +}