-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathversion.go
More file actions
70 lines (59 loc) · 1.9 KB
/
Copy pathversion.go
File metadata and controls
70 lines (59 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package version
import (
"fmt"
"os"
"runtime"
)
var (
// Version is the application version - set by build flags
Version = "test"
// GitCommit is the git commit hash - set by build flags
GitCommit = "unknown"
// BuildDate is the build date - set by build flags
BuildDate = "unknown"
// BuildEnv is the environment this binary was built for - set by build flags
BuildEnv = "unknown"
)
// Info returns detailed version information
func Info() string {
return fmt.Sprintf("Version: %s, Commit: %s, Built: %s, BuildEnv: %s, Go: %s",
Version, GitCommit, BuildDate, BuildEnv, runtime.Version())
}
// Short returns a short version string
func Short() string {
return Version
}
// Component returns version info for a specific component
func Component(componentName string) string {
return fmt.Sprintf("%s %s (commit: %s, built: %s, env: %s)",
componentName, Version, GitCommit, BuildDate, BuildEnv)
}
// Environment returns the build environment
func Environment() string {
if BuildEnv == "unknown" {
return "test" // Default fallback
}
return BuildEnv
}
// EnvironmentInfo returns detailed environment information including runtime detection
func EnvironmentInfo() string {
buildEnv := Environment()
runtimeEnv := "unknown"
// Try to detect runtime environment from MINEXUS_ENV
if envVar := os.Getenv("MINEXUS_ENV"); envVar != "" {
runtimeEnv = envVar
}
if buildEnv == runtimeEnv {
return fmt.Sprintf("Environment: %s (build matches runtime)", buildEnv)
}
return fmt.Sprintf("Environment: build=%s, runtime=%s", buildEnv, runtimeEnv)
}
// CheckAndHandleVersionFlag checks if version flag was provided and prints version if so.
// Returns true if version flag was handled, false otherwise.
func CheckAndHandleVersionFlag(componentName string) bool {
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
fmt.Printf("%s %s\n", componentName, Info())
return true
}
return false
}