Skip to content

Commit

Permalink
Merge pull request #188 from iveelsm/mikey/version-command
Browse files Browse the repository at this point in the history
Add support for a version command to check the binary version
  • Loading branch information
kuskoman authored Oct 14, 2023
2 parents c4b5742 + b3dc1a7 commit afbd3d0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ GOOS_EXES := $(foreach goos,$(GOOS_VALUES),$(if $(filter windows,$(goos)),out/ma

GITHUB_REPO := github.com/kuskoman/logstash-exporter
VERSION ?= $(shell git symbolic-ref --short HEAD)
SEMANTIC_VERSION ?= $(shell git describe --tags --abbrev=1 --dirty 2> /dev/null)
GIT_COMMIT := $(shell git rev-parse HEAD)
DOCKER_IMG ?= "logstash-exporter"

Expand All @@ -20,6 +21,7 @@ all: $(GOOS_BINARIES)
VERSIONINFO_PKG := config
ldflags := -s -w \
-X '$(GITHUB_REPO)/$(VERSIONINFO_PKG).Version=$(VERSION)' \
-X '$(GITHUB_REPO)/$(VERSIONINFO_PKG).SemanticVersion=$(SEMANTIC_VERSION)' \
-X '$(GITHUB_REPO)/$(VERSIONINFO_PKG).GitCommit=$(GIT_COMMIT)' \
-X '$(GITHUB_REPO)/$(VERSIONINFO_PKG).BuildDate=$(shell date -u +%Y-%m-%dT%H:%M:%S%Z)'

Expand Down
10 changes: 10 additions & 0 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"flag"
"fmt"
"log"
"log/slog"
"os"
Expand All @@ -13,6 +15,14 @@ import (
)

func main() {
version := flag.Bool("version", false, "prints the version and exits")

flag.Parse()
if *version {
fmt.Printf("%s\n", config.SemanticVersion)
return
}

warn := godotenv.Load()
if warn != nil {
log.Println(warn)
Expand Down
33 changes: 20 additions & 13 deletions config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ var (
// Version is the current version of Logstash Exporter.
Version = "unknown"

// Semantic Version is the current version of the Logstash Exporter
// This version uses the latest tag as well as the sha prefix
// if there have been any commits since the last tag
SemanticVersion = "unknown"

// GitCommit is the git commit hash of the current build.
GitCommit = "unknown"

Expand All @@ -19,26 +24,28 @@ var (
// GetVersionInfo returns a VersionInfo struct with the current build information.
func GetVersionInfo() *VersionInfo {
return &VersionInfo{
Version: Version,
GitCommit: GitCommit,
GoVersion: runtime.Version(),
BuildArch: runtime.GOARCH,
BuildOS: runtime.GOOS,
BuildDate: BuildDate,
Version: Version,
SemanticVersion: SemanticVersion,
GitCommit: GitCommit,
GoVersion: runtime.Version(),
BuildArch: runtime.GOARCH,
BuildOS: runtime.GOOS,
BuildDate: BuildDate,
}
}

// VersionInfo contains the current build information.
type VersionInfo struct {
Version string
GitCommit string
GoVersion string
BuildArch string
BuildOS string
BuildDate string
Version string
SemanticVersion string
GitCommit string
GoVersion string
BuildArch string
BuildOS string
BuildDate string
}

// String returns a string representation of the VersionInfo struct.
func (v *VersionInfo) String() string {
return fmt.Sprintf("Version: %s, GitCommit: %s, GoVersion: %s, BuildArch: %s, BuildOS: %s, BuildDate: %s", v.Version, v.GitCommit, v.GoVersion, v.BuildArch, v.BuildOS, v.BuildDate)
return fmt.Sprintf("Version: %s, SemanticVersion: %s, GitCommit: %s, GoVersion: %s, BuildArch: %s, BuildOS: %s, BuildDate: %s", v.Version, v.SemanticVersion, v.GitCommit, v.GoVersion, v.BuildArch, v.BuildOS, v.BuildDate)
}
19 changes: 12 additions & 7 deletions config/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func TestGetBuildInfo(t *testing.T) {
t.Error("Expected Version to be set")
}

if versionInfo.SemanticVersion == "" {
t.Error("Expected SemanticVersion to be set")
}

if versionInfo.GitCommit == "" {
t.Error("Expected GitCommit to be set")
}
Expand All @@ -35,15 +39,16 @@ func TestGetBuildInfo(t *testing.T) {

func TestVersionInfoString(t *testing.T) {
versionInfo := &VersionInfo{
Version: "test-version",
GitCommit: "test-commit",
GoVersion: "test-go-version",
BuildArch: "test-arch",
BuildOS: "test-os",
BuildDate: "test-date",
Version: "test-version",
SemanticVersion: "v0.0.1-0548a52",
GitCommit: "test-commit",
GoVersion: "test-go-version",
BuildArch: "test-arch",
BuildOS: "test-os",
BuildDate: "test-date",
}

expectedString := "Version: test-version, GitCommit: test-commit, GoVersion: test-go-version, BuildArch: test-arch, BuildOS: test-os, BuildDate: test-date"
expectedString := "Version: test-version, SemanticVersion: v0.0.1-0548a52, GitCommit: test-commit, GoVersion: test-go-version, BuildArch: test-arch, BuildOS: test-os, BuildDate: test-date"
versionInfoString := versionInfo.String()

if versionInfoString != expectedString {
Expand Down
2 changes: 1 addition & 1 deletion server/versioninfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestHandleVersionInfo(t *testing.T) {
}

firstWrite := string(w.writes[0])
expectedFirstWrite := `{"Version":"version","GitCommit":"git commit","GoVersion":"go version","BuildArch":"build arch","BuildOS":"build os","BuildDate":"build date"}`
expectedFirstWrite := `{"Version":"version","SemanticVersion":"","GitCommit":"git commit","GoVersion":"go version","BuildArch":"build arch","BuildOS":"build os","BuildDate":"build date"}`
expectedFirstWrite = expectedFirstWrite + "\n"
if firstWrite != expectedFirstWrite {
t.Errorf("expected first write to be %s, but got: %s", expectedFirstWrite, firstWrite)
Expand Down

0 comments on commit afbd3d0

Please sign in to comment.