Skip to content

Commit 79d3bbc

Browse files
authored
feat: add version command (#84)
* add version command * update hack/common * add version command
1 parent d49fb89 commit 79d3bbc

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

cmd/version.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
apimachineryversion "k8s.io/apimachinery/pkg/version"
8+
"sigs.k8s.io/yaml"
9+
10+
ocmcli "github.com/openmcp-project/bootstrapper/internal/ocm-cli"
11+
12+
"github.com/openmcp-project/bootstrapper/internal/version"
13+
)
14+
15+
// versionCmd represents the version command
16+
var versionCmd = &cobra.Command{
17+
Use: "version",
18+
Short: "Print the version information",
19+
Long: `Print the version information of the openMCP bootstrapper CLI.
20+
21+
This command displays detailed version information including the build version,
22+
Git commit, build date, Go version, and platform information.`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
ownVersion := version.GetVersion()
25+
26+
if ownVersion == nil {
27+
fmt.Println("Version information not available")
28+
return
29+
}
30+
31+
printVersion(ownVersion, "openMCP Bootstrapper CLI")
32+
33+
var ocmVersion apimachineryversion.Info
34+
out, err := ocmcli.ExecuteOutput(cmd.Context(), []string{"version"}, nil, ocmcli.NoOcmConfig)
35+
if err != nil {
36+
fmt.Printf("Error retrieving ocm-cli version: %ownVersion\n", err)
37+
return
38+
}
39+
40+
err = yaml.Unmarshal(out, &ocmVersion)
41+
if err != nil {
42+
fmt.Printf("Error parsing ocm-cli version output: %ownVersion\n", err)
43+
return
44+
}
45+
46+
printVersion(&ocmVersion, "OCM CLI")
47+
},
48+
}
49+
50+
func printVersion(v *apimachineryversion.Info, header string) {
51+
fmt.Printf("\n%s\n", header)
52+
fmt.Printf("========================\n")
53+
fmt.Printf("Version: %s\n", v.GitVersion)
54+
if v.GitCommit != "" {
55+
fmt.Printf("Git Commit: %s\n", v.GitCommit)
56+
}
57+
if v.GitTreeState != "" {
58+
fmt.Printf("Git State: %s\n", v.GitTreeState)
59+
}
60+
if v.BuildDate != "" {
61+
fmt.Printf("Build Date: %s\n", v.BuildDate)
62+
}
63+
fmt.Printf("Go Version: %s\n", v.GoVersion)
64+
fmt.Printf("Compiler: %s\n", v.Compiler)
65+
fmt.Printf("Platform: %s\n", v.Platform)
66+
fmt.Printf("===================\n")
67+
}
68+
69+
func init() {
70+
RootCmd.AddCommand(versionCmd)
71+
}

hack/common

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)