You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by zh...@apache.org on 2025/07/16 02:09:29 UTC

(dubbo-kubernetes) branch release-0.1 updated: [dubboctl] add version command (#746)

This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch release-0.1
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/release-0.1 by this push:
     new cbb48b82 [dubboctl] add version command (#746)
cbb48b82 is described below

commit cbb48b82a084a4469dcad6897c3f1e7e8fc9d2b1
Author: Jian Zhong <11...@qq.com>
AuthorDate: Wed Jul 16 10:09:24 2025 +0800

    [dubboctl] add version command (#746)
---
 dubboctl/cmd/root.go            |  8 +++-----
 dubboctl/pkg/version/version.go | 18 ++++++++++++++----
 pkg/version/cobra.go            | 35 ++++-------------------------------
 pkg/version/version.go          | 38 ++++++++++++++++++++++++--------------
 4 files changed, 45 insertions(+), 54 deletions(-)

diff --git a/dubboctl/cmd/root.go b/dubboctl/cmd/root.go
index e643c40e..4cafabed 100644
--- a/dubboctl/cmd/root.go
+++ b/dubboctl/cmd/root.go
@@ -28,6 +28,7 @@ import (
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk"
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/util"
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/validate"
+	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/version"
 	"github.com/apache/dubbo-kubernetes/operator/cmd/cluster"
 	"github.com/spf13/cobra"
 	"net/http"
@@ -123,10 +124,6 @@ func GetRootCmd(args []string) *cobra.Command {
 	rootCmd.AddCommand(validateCmd)
 	hideFlags(validateCmd, ChartFlag)
 
-	// versionCmd := version.NewVersionCommand(ctx)
-	// rootCmd.AddCommand(versionCmd)
-	// hideFlags(versionCmd, ChartFlag)
-
 	createCmd := CreateCmd(ctx, rootCmd, factory)
 	rootCmd.AddCommand(createCmd)
 	hideFlags(createCmd, ChartFlag)
@@ -139,8 +136,9 @@ func GetRootCmd(args []string) *cobra.Command {
 	rootCmd.AddCommand(imageCmd)
 	hideFlags(imageCmd, ChartFlag)
 
-	return rootCmd
+	rootCmd.AddCommand(version.NewVersionCommand())
 
+	return rootCmd
 }
 
 func hideFlags(origin *cobra.Command, hide ...string) {
diff --git a/dubboctl/pkg/version/version.go b/dubboctl/pkg/version/version.go
index f984a60b..41b9f776 100644
--- a/dubboctl/pkg/version/version.go
+++ b/dubboctl/pkg/version/version.go
@@ -18,13 +18,23 @@
 package version
 
 import (
-	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
-	dubboVersion "github.com/apache/dubbo-kubernetes/pkg/version"
+	"fmt"
+	"github.com/apache/dubbo-kubernetes/pkg/version"
 	"github.com/spf13/cobra"
+	"github.com/spf13/pflag"
+	"os"
 )
 
-func NewVersionCommand(_ cli.Context) *cobra.Command {
+func NewVersionCommand() *cobra.Command {
 	var versionCmd *cobra.Command
-	versionCmd = dubboVersion.CobraCommandWithOptions()
+	versionCmd = version.CobraCommandWithOptions()
+	versionCmd.Flags().VisitAll(func(flag *pflag.Flag) {
+		if flag.Name == "short" {
+			err := flag.Value.Set("true")
+			if err != nil {
+				fmt.Fprintf(os.Stdout, "set flag %q as true failed due to error %v", flag.Name, err)
+			}
+		}
+	})
 	return versionCmd
 }
diff --git a/pkg/version/cobra.go b/pkg/version/cobra.go
index dccc244f..50ff6be8 100644
--- a/pkg/version/cobra.go
+++ b/pkg/version/cobra.go
@@ -18,25 +18,17 @@
 package version
 
 import (
-	"encoding/json"
-	"errors"
 	"fmt"
 	"github.com/spf13/cobra"
-	"sigs.k8s.io/yaml"
 )
 
 type Version struct {
-	ClientVersion *BuildInfo `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
+	ClientVersion *BuildVersion `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
 }
 
-var (
-	Info BuildInfo
-)
-
 func CobraCommandWithOptions() *cobra.Command {
 	var (
 		short   bool
-		output  string
 		version Version
 	)
 	ves := &cobra.Command{
@@ -44,33 +36,14 @@ func CobraCommandWithOptions() *cobra.Command {
 		Short: "Display the information of the currently built version",
 		Long:  "The version command displays the information of the currently built version.",
 		RunE: func(cmd *cobra.Command, args []string) error {
-			if output != "" && output != "yaml" && output != "json" {
-				return errors.New(`--output must be 'yaml' or 'json'`)
+			version.ClientVersion = &Cobra
+			if short {
+				_, _ = fmt.Fprintf(cmd.OutOrStdout(), "client version: %s\n", version.ClientVersion.Version)
 			}
-			version.ClientVersion = &Info
-			switch output {
-			case "":
-				if short {
-					_, _ = fmt.Fprintf(cmd.OutOrStdout(), "client version: %s\n", version.ClientVersion.Version)
-
-				} else {
-					_, _ = fmt.Fprintf(cmd.OutOrStdout(), "client version: %s\n", version.ClientVersion.LongForm())
-				}
-			case "yaml":
-				if marshaled, err := yaml.Marshal(&version); err == nil {
-					_, _ = fmt.Fprintln(cmd.OutOrStdout(), string(marshaled))
-				}
-			case "json":
-				if marshaled, err := json.MarshalIndent(&version, "", "  "); err == nil {
-					_, _ = fmt.Fprintln(cmd.OutOrStdout(), string(marshaled))
-				}
-			}
-
 			return nil
 		},
 	}
 
 	ves.Flags().BoolVarP(&short, "short", "s", false, "Use --short=false to generate full version information")
-	ves.Flags().StringVarP(&output, "output", "o", "", "One of 'yaml' or 'json'.")
 	return ves
 }
diff --git a/pkg/version/version.go b/pkg/version/version.go
index 56dce906..8ae9ffed 100644
--- a/pkg/version/version.go
+++ b/pkg/version/version.go
@@ -24,26 +24,28 @@ import (
 )
 
 var (
-	buildVersion = "unknown"
+	buildVersion     = "unknown"
+	buildGitRevision = "unknown"
+	buildStatus      = "unknown"
+	buildTag         = "unknown"
 )
 
-// type BuildInfo struct {
-//	Version string `json:"version"`
-// }
-
-func (b BuildInfo) String() string {
-	return fmt.Sprintf("%v", b.Version)
+type BuildVersion struct {
+	Version       string `json:"version"`
+	GitRevision   string `json:"revision"`
+	GolangVersion string `json:"golang_version"`
+	BuildStatus   string `json:"status"`
+	GitTag        string `json:"tag"`
 }
 
-func (b BuildInfo) LongForm() string {
-	return fmt.Sprintf("%#v", b)
+func (b BuildVersion) String() string {
+	return fmt.Sprintf("%v-%v-%v",
+		b.Version,
+		b.GitRevision,
+		b.BuildStatus)
 }
 
-func init() {
-	Info = BuildInfo{
-		Version: buildVersion,
-	}
-}
+var Cobra BuildVersion
 
 var (
 	Product      = "Dubbo"
@@ -127,4 +129,12 @@ func init() {
 		BuildDate:    buildDate,
 		BasedOnDubbo: basedOndubbo,
 	}
+
+	Cobra = BuildVersion{
+		Version:       buildVersion,
+		GitRevision:   buildGitRevision,
+		GolangVersion: runtime.Version(),
+		BuildStatus:   buildStatus,
+		GitTag:        buildTag,
+	}
 }