Add an improved version string contianing the commit timestamp

pull/177/head
James Mills 2 weeks ago
parent 95663345d4
commit a42f9e85ec
Signed by: prologic
GPG Key ID: AC4C014F1440EBD6
  1. 41
      Makefile
  2. 8
      cmd/salty-chat/root.go
  3. 32
      version.go

@ -2,9 +2,11 @@
.PHONY: help deps dev build install image release test clean clean-all
export CGO_ENABLED=0
VERSION=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "0.0.0")
COMMIT=$(shell git rev-parse --short HEAD || echo "HEAD")
VERSION=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "$VERSION")
COMMIT=$(shell git rev-parse --short HEAD || echo "$COMMIT")
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
BUILD=$(shell git show -s --pretty=format:%cI)
GOCMD=go
GOCMD=go
DESTDIR=/usr/local/bin
@ -49,14 +51,19 @@ cli: ## Build the salty-chat command-line client and tui
ifeq ($(DEBUG), 1)
@echo "Building in debug mode..."
@$(GOCMD) build -tags "netgo static_build" -installsuffix netgo \
-ldflags "-X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT)" \
-ldflags "\
-X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT) \
-X $(shell go list).Build=$(BUILD) \
" \
./cmd/salty-chat/
else
@$(GOCMD) build -tags "netgo static_build" -installsuffix netgo \
-ldflags "-w \
-X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT)" \
-X $(shell go list).Commit=$(COMMIT) \
-X $(shell go list).Build=$(BUILD) \
" \
./cmd/salty-chat/
endif
@ -64,14 +71,19 @@ server: generate pwa ## Build the saltyd server and broker (also includes the PW
ifeq ($(DEBUG), 1)
@echo "Building in debug mode..."
@$(GOCMD) build -tags "embed netgo static_build" -installsuffix netgo \
-ldflags "-X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT)" \
-ldflags "\
-X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT) \
-X $(shell go list).Build=$(BUILD) \
" \
./cmd/saltyd/
else
@$(GOCMD) build -tags "embed netgo static_build" -installsuffix netgo \
-ldflags "-w \
-X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT)" \
-X $(shell go list).Commit=$(COMMIT) \
-X $(shell go list).Build=$(BUILD) \
" \
./cmd/saltyd/
endif
@ -95,11 +107,18 @@ install: build ## Install salty-chat (cli) and saltyd (server) to $DESTDIR
ifeq ($(PUBLISH), 1)
image: generate ## Build the Docker image
@docker build --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" -t $(IMAGE):$(TAG) .
@docker push $(IMAGE):$(TAG)
@docker buildx build \
--build-arg VERSION="$(VERSION)" \
--build-arg COMMIT="$(COMMIT)" \
--build-arg BUILD="$(BUILD)" \
--platform linux/amd64,linux/arm64 --push -t $(IMAGE):$(TAG) .
else
image: generate
@docker build --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" -t $(IMAGE):$(TAG) .
@docker build \
--build-arg VERSION="$(VERSION)" \
--build-arg COMMIT="$(COMMIT)" \
--build-arg BUILD="$(BUILD)" \
-t $(IMAGE):$(TAG) .
endif
release: generate ## Release a new version to Gitea

@ -49,6 +49,14 @@ See https://salty.im for more details.`,
// and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if os.Args[1] == "-v" || os.Args[1] == "--version" {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
cmd, _, err := rootCmd.Find(os.Args[1:])
// default cmd if no cmd is given
if err == nil && strings.Fields(cmd.Use)[0] == strings.Fields(rootCmd.Use)[0] && cmd.Flags().Parse(os.Args[1:]) != pflag.ErrHelp {

@ -3,27 +3,35 @@ package saltyim
import (
"fmt"
"runtime/debug"
"strings"
)
var (
// Version release version
// Version is the tagged release version in the form <major>.<minor>.<patch>
// following semantic versioning and is overwritten by the build system.
Version = "0.0.0"
// Commit will be overwritten automatically by the build system
// Commit is the commit sha of the build (normally from Git) and is overwritten
// by the build system.
Commit = "HEAD"
// Build is the date and time of the build as an RFC3339 formatted string
// and is overwritten by the build system.
Build = "0000-01-01:00:00+00:00"
)
// FullVersion display the full version and build
func FullVersion() string {
if Version != "0.0.0" && Commit != "HEAD" {
return fmt.Sprintf("%s@%s", Version, Commit)
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build))
if info, ok := debug.ReadBuildInfo(); ok {
sb.WriteString(fmt.Sprintf(" %s %s", info.Main.Version, info.GoVersion))
if info.Main.Sum != "" {
sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum))
}
}
return fmt.Sprintf(
"%s built with %s (checksum: %s)",
info.Main.Version, info.GoVersion, info.Main.Sum,
)
return sb.String()
}

Loading…
Cancel
Save