Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
apimachineryversion "k8s.io/apimachinery/pkg/version"
"sigs.k8s.io/yaml"

ocmcli "github.com/openmcp-project/bootstrapper/internal/ocm-cli"

"github.com/openmcp-project/bootstrapper/internal/version"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version information",
Long: `Print the version information of the openMCP bootstrapper CLI.

This command displays detailed version information including the build version,
Git commit, build date, Go version, and platform information.`,
Run: func(cmd *cobra.Command, args []string) {
ownVersion := version.GetVersion()

if ownVersion == nil {
fmt.Println("Version information not available")
return
}

printVersion(ownVersion, "openMCP Bootstrapper CLI")

var ocmVersion apimachineryversion.Info
out, err := ocmcli.ExecuteOutput(cmd.Context(), []string{"version"}, nil, ocmcli.NoOcmConfig)
if err != nil {
fmt.Printf("Error retrieving ocm-cli version: %ownVersion\n", err)
return
}

err = yaml.Unmarshal(out, &ocmVersion)
if err != nil {
fmt.Printf("Error parsing ocm-cli version output: %ownVersion\n", err)
return
}

printVersion(&ocmVersion, "OCM CLI")
},
}

func printVersion(v *apimachineryversion.Info, header string) {
fmt.Printf("\n%s\n", header)
fmt.Printf("========================\n")
fmt.Printf("Version: %s\n", v.GitVersion)
if v.GitCommit != "" {
fmt.Printf("Git Commit: %s\n", v.GitCommit)
}
if v.GitTreeState != "" {
fmt.Printf("Git State: %s\n", v.GitTreeState)
}
if v.BuildDate != "" {
fmt.Printf("Build Date: %s\n", v.BuildDate)
}
fmt.Printf("Go Version: %s\n", v.GoVersion)
fmt.Printf("Compiler: %s\n", v.Compiler)
fmt.Printf("Platform: %s\n", v.Platform)
fmt.Printf("===================\n")
}

func init() {
RootCmd.AddCommand(versionCmd)
}
2 changes: 1 addition & 1 deletion hack/common
47 changes: 47 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package version

import (
"fmt"
"runtime"
"strings"

apimachineryversion "k8s.io/apimachinery/pkg/version"
)

var (
// These variables are set during build time using -ldflags
// buildVersion is the semantic version of the build.
buildVersion = ""
// gitTreeState is either "clean" or "dirty" depending on the state of the git tree.
gitTreeState = ""
// gitCommit is the git commit hash of the build.
gitCommit = ""
// buildDate is the date of the build.
buildDate = ""
)

// GetVersion returns the version information of the build.
func GetVersion() *apimachineryversion.Info {
var (
version = strings.Split(buildVersion, ".")
gitMajor string
gitMinor string
)

if len(version) >= 2 {
gitMajor = version[0]
gitMinor = version[1]
}

return &apimachineryversion.Info{
Major: gitMajor,
Minor: gitMinor,
GitVersion: buildVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}