Skip to content

Commit

Permalink
Add version info to config package
Browse files Browse the repository at this point in the history
  • Loading branch information
kuskoman committed Apr 13, 2023
1 parent b2d16cb commit b280e28
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
44 changes: 44 additions & 0 deletions config/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"fmt"
"runtime"
)

var (
// Version is the current version of Logstash Exporter.
Version = "unknown"

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

// BuildDate is the date of the current build.
BuildDate = "unknown"
)

// GetBuildInfo returns a VersionInfo struct with the current build information.
func GetBuildInfo() *VersionInfo {
return &VersionInfo{
Version: Version,
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
}

// String returns a string representation of the VersionInfo struct.
func (v *VersionInfo) String() string {
return fmt.Sprintf("%+v", *v)
}
52 changes: 52 additions & 0 deletions config/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package config

import (
"runtime"
"testing"
)

func TestGetBuildInfo(t *testing.T) {
versionInfo := GetBuildInfo()

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

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

if versionInfo.GoVersion != runtime.Version() {
t.Errorf("Expected GoVersion: %s, but got: %s", runtime.Version(), versionInfo.GoVersion)
}

if versionInfo.BuildArch != runtime.GOARCH {
t.Errorf("Expected BuildArch: %s, but got: %s", runtime.GOARCH, versionInfo.BuildArch)
}

if versionInfo.BuildOS != runtime.GOOS {
t.Errorf("Expected BuildOS: %s, but got: %s", runtime.GOOS, versionInfo.BuildOS)
}

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

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",
}

expectedString := "{Version:test-version GitCommit:test-commit GoVersion:test-go-version BuildArch:test-arch BuildOS:test-os BuildDate:test-date}"
versionInfoString := versionInfo.String()

if versionInfoString != expectedString {
t.Errorf("Expected string: %s, but got: %s", expectedString, versionInfoString)
}
}

0 comments on commit b280e28

Please sign in to comment.