Skip to content

Commit 7651641

Browse files
committed
add version command
1 parent cc86f69 commit 7651641

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

cmd/version.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/openmcp-project/bootstrapper/internal/version"
9+
)
10+
11+
// versionCmd represents the version command
12+
var versionCmd = &cobra.Command{
13+
Use: "version",
14+
Short: "Print the version information",
15+
Long: `Print the version information of the openMCP bootstrapper CLI.
16+
17+
This command displays detailed version information including the build version,
18+
Git commit, build date, Go version, and platform information.`,
19+
Run: func(cmd *cobra.Command, args []string) {
20+
printVersion()
21+
},
22+
}
23+
24+
func printVersion() {
25+
v := version.GetVersion()
26+
27+
if v == nil {
28+
fmt.Println("Version information not available")
29+
return
30+
}
31+
32+
fmt.Printf("Version: %s\n", v.GitVersion)
33+
if v.GitCommit != "" {
34+
fmt.Printf("Git Commit: %s\n", v.GitCommit)
35+
}
36+
if v.GitTreeState != "" {
37+
fmt.Printf("Git State: %s\n", v.GitTreeState)
38+
}
39+
if v.BuildDate != "" {
40+
fmt.Printf("Build Date: %s\n", v.BuildDate)
41+
}
42+
fmt.Printf("Go Version: %s\n", v.GoVersion)
43+
fmt.Printf("Compiler: %s\n", v.Compiler)
44+
fmt.Printf("Platform: %s\n", v.Platform)
45+
}
46+
47+
func init() {
48+
RootCmd.AddCommand(versionCmd)
49+
}

internal/version/version.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"strings"
7+
8+
apimachineryversion "k8s.io/apimachinery/pkg/version"
9+
)
10+
11+
var (
12+
// These variables are set during build time using -ldflags
13+
// buildVersion is the semantic version of the build.
14+
buildVersion = ""
15+
// gitTreeState is either "clean" or "dirty" depending on the state of the git tree.
16+
gitTreeState = ""
17+
// gitCommit is the git commit hash of the build.
18+
gitCommit = ""
19+
// buildDate is the date of the build.
20+
buildDate = ""
21+
)
22+
23+
// GetVersion returns the version information of the build.
24+
func GetVersion() *apimachineryversion.Info {
25+
var (
26+
version = strings.Split(buildVersion, ".")
27+
gitMajor string
28+
gitMinor string
29+
)
30+
31+
if len(version) >= 2 {
32+
gitMajor = version[0]
33+
gitMinor = version[1]
34+
}
35+
36+
return &apimachineryversion.Info{
37+
Major: gitMajor,
38+
Minor: gitMinor,
39+
GitVersion: buildVersion,
40+
GitCommit: gitCommit,
41+
GitTreeState: gitTreeState,
42+
BuildDate: buildDate,
43+
GoVersion: runtime.Version(),
44+
Compiler: runtime.Compiler,
45+
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
46+
}
47+
}

0 commit comments

Comments
 (0)