diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..9dfc450 --- /dev/null +++ b/cmd/version.go @@ -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) +} diff --git a/hack/common b/hack/common index 31450a1..5ffe5c0 160000 --- a/hack/common +++ b/hack/common @@ -1 +1 @@ -Subproject commit 31450a115bbe0e08d51a5d41d769174d260238d2 +Subproject commit 5ffe5c0dd747e62fd80cb2dc7ba93fd724084c9c diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..69eb18c --- /dev/null +++ b/internal/version/version.go @@ -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), + } +}