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

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


The following commit(s) were added to refs/heads/master by this push:
     new 29bf1620 [operator] update version command
29bf1620 is described below

commit 29bf16209449105b060fd239ab2a1ddfc9514c52
Author: mfordjody <[email protected]>
AuthorDate: Thu Dec 26 13:27:37 2024 +0800

    [operator] update version command
---
 dubboctl/cmd/root.go              |  13 ++-
 dubboctl/pkg/validate/validate.go |   5 +-
 dubboctl/pkg/version/version.go   |  13 +++
 operator/cmd/cluster/uninstall.go |   7 +-
 operator/pkg/version/version.go   |   1 -
 pkg/version/cobra.go              |  56 +++++++++++++
 pkg/version/version.go            | 162 +++++++++++++++++++++-----------------
 7 files changed, 172 insertions(+), 85 deletions(-)

diff --git a/dubboctl/cmd/root.go b/dubboctl/cmd/root.go
index a98eb2fc..18b40e1a 100644
--- a/dubboctl/cmd/root.go
+++ b/dubboctl/cmd/root.go
@@ -19,6 +19,7 @@ import (
        "flag"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
        "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"
 )
@@ -29,9 +30,11 @@ func AddFlags(cmd *cobra.Command) {
 
 func GetRootCmd(args []string) *cobra.Command {
        rootCmd := &cobra.Command{
-               Use:   "dubboctl",
-               Short: "Dubbo command line utilities",
-               Long:  `Dubbo configuration command line utility for debug and 
use dubbo applications.`,
+               Use:           "dubboctl",
+               Short:         "Dubbo command line utilities",
+               SilenceUsage:  true,
+               SilenceErrors: true,
+               Long:          `Dubbo configuration command line utility for 
debug and use dubbo applications.`,
        }
        AddFlags(rootCmd)
        rootCmd.SetArgs(args)
@@ -57,6 +60,10 @@ func GetRootCmd(args []string) *cobra.Command {
        rootCmd.AddCommand(validateCmd)
        hideFlags(validateCmd, cli.NamespaceFlag, cli.DubboNamespaceFlag, 
cli.ChartFlag)
 
+       versionCmd := version.NewVersionCommand(ctx)
+       rootCmd.AddCommand(versionCmd)
+       hideFlags(versionCmd, cli.NamespaceFlag, cli.DubboNamespaceFlag, 
cli.ChartFlag)
+
        return rootCmd
 }
 
diff --git a/dubboctl/pkg/validate/validate.go 
b/dubboctl/pkg/validate/validate.go
index 640f6360..eb83ffc8 100644
--- a/dubboctl/pkg/validate/validate.go
+++ b/dubboctl/pkg/validate/validate.go
@@ -112,9 +112,8 @@ func NewValidateCommand(ctx cli.Context) *cobra.Command {
   # Validate current services under 'default' namespace with in the cluster
   kubectl get services -o yaml | dubboctl validate -f -
 `,
-               Args:         cobra.NoArgs,
-               Aliases:      []string{"v"},
-               SilenceUsage: true,
+               Args:    cobra.NoArgs,
+               Aliases: []string{"v"},
                RunE: func(cmd *cobra.Command, _ []string) error {
                        dn := ctx.DubboNamespace()
                        return validateFiles(&dn, files, cmd.OutOrStderr())
diff --git a/dubboctl/pkg/version/version.go b/dubboctl/pkg/version/version.go
new file mode 100644
index 00000000..6498b2a0
--- /dev/null
+++ b/dubboctl/pkg/version/version.go
@@ -0,0 +1,13 @@
+package version
+
+import (
+       "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
+       dubboVersion "github.com/apache/dubbo-kubernetes/pkg/version"
+       "github.com/spf13/cobra"
+)
+
+func NewVersionCommand(_ cli.Context) *cobra.Command {
+       var versionCmd *cobra.Command
+       versionCmd = dubboVersion.CobraCommandWithOptions()
+       return versionCmd
+}
diff --git a/operator/cmd/cluster/uninstall.go 
b/operator/cmd/cluster/uninstall.go
index e1b5f52d..b1c97afe 100644
--- a/operator/cmd/cluster/uninstall.go
+++ b/operator/cmd/cluster/uninstall.go
@@ -37,10 +37,9 @@ func UninstallCmd(ctx cli.Context) *cobra.Command {
        rootArgs := &RootArgs{}
        uiArgs := &uninstallArgs{}
        uicmd := &cobra.Command{
-               Use:          "uninstall",
-               Short:        "Uninstall Dubbo related resources",
-               Long:         "The uninstall command will uninstall the dubbo 
cluster",
-               SilenceUsage: true,
+               Use:   "uninstall",
+               Short: "Uninstall Dubbo related resources",
+               Long:  "The uninstall command will uninstall the dubbo cluster",
                Example: ` # Uninstall a single control plane by dop file
   dubboctl uninstall -f dop.yaml
   
diff --git a/operator/pkg/version/version.go b/operator/pkg/version/version.go
deleted file mode 100644
index f37d99d0..00000000
--- a/operator/pkg/version/version.go
+++ /dev/null
@@ -1 +0,0 @@
-package version
diff --git a/pkg/version/cobra.go b/pkg/version/cobra.go
new file mode 100644
index 00000000..b2995715
--- /dev/null
+++ b/pkg/version/cobra.go
@@ -0,0 +1,56 @@
+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"`
+}
+
+var (
+       Info BuildInfo
+)
+
+func CobraCommandWithOptions() *cobra.Command {
+       var (
+               short   bool
+               output  string
+               version Version
+       )
+       ves := &cobra.Command{
+               Use:   "version",
+               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 = &Info
+                       switch output {
+                       case "":
+                               if short {
+                                       _, _ = fmt.Fprintf(cmd.OutOrStdout(), 
"client version: %s\n", version.ClientVersion.Version)
+                               }
+                       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 acb6afeb..636fea39 100644
--- a/pkg/version/version.go
+++ b/pkg/version/version.go
@@ -17,92 +17,106 @@
 
 package version
 
-import (
-       "fmt"
-       "runtime"
-       "strings"
-)
+import "fmt"
 
 var (
-       Product      = "Dubbo"
-       basedOndubbo = ""
-       version      = "unknown"
-       gitTag       = "unknown"
-       gitCommit    = "unknown"
-       buildDate    = "unknown"
-       Envoy        = "unknown"
+       buildVersion = "unknown"
 )
 
 type BuildInfo struct {
-       Product      string
-       Version      string
-       GitTag       string
-       GitCommit    string
-       BuildDate    string
-       BasedOnDubbo string
+       Version string `json:"version"`
 }
 
-func (b BuildInfo) FormatDetailedProductInfo() string {
-       base := []string{
-               fmt.Sprintf("Product:       %s", b.Product),
-               fmt.Sprintf("Version:       %s", b.Version),
-               fmt.Sprintf("Git Tag:       %s", b.GitTag),
-               fmt.Sprintf("Git Commit:    %s", b.GitCommit),
-               fmt.Sprintf("Build Date:    %s", b.BuildDate),
-       }
-       if b.BasedOnDubbo != "" {
-               base = append(base, fmt.Sprintf("Based on dubbo: %s", 
b.BasedOnDubbo))
-       }
-       return strings.Join(
-               base,
-               "\n",
-       )
+func (b BuildInfo) String() string {
+       return fmt.Sprintf("%v-%v-%v", b.Version)
 }
 
-func shortCommit(c string) string {
-       if len(c) < 7 {
-               return c
+func init() {
+       Info = BuildInfo{
+               Version: buildVersion,
        }
-       return c[:7]
 }
 
-func (b BuildInfo) AsMap() map[string]string {
-       res := map[string]string{
-               "product":    b.Product,
-               "version":    b.Version,
-               "build_date": b.BuildDate,
-               "git_commit": shortCommit(b.GitCommit),
-               "git_tag":    b.GitTag,
-       }
-       if b.BasedOnDubbo != "" {
-               res["based_on_dubbo"] = b.BasedOnDubbo
-       }
-       return res
-}
+//var (
+//     Product      = "Dubbo"
+//     basedOndubbo = ""
+//     version      = "unknown"
+//     gitTag       = "unknown"
+//     gitCommit    = "unknown"
+//     buildDate    = "unknown"
+//     Envoy        = "unknown"
+//)
 
-func (b BuildInfo) UserAgent(component string) string {
-       commit := shortCommit(b.GitCommit)
-       if b.BasedOnDubbo != "" {
-               commit = fmt.Sprintf("%s/dubbo-%s", commit, b.BasedOnDubbo)
-       }
-       return fmt.Sprintf("%s/%s (%s; %s; %s/%s)",
-               component,
-               b.Version,
-               runtime.GOOS,
-               runtime.GOARCH,
-               b.Product,
-               commit)
-}
+//type BuildInfo struct {
+//     Product      string
+//     Version      string
+//     GitTag       string
+//     GitCommit    string
+//     BuildDate    string
+//     BasedOnDubbo string
+//}
 
-var Build BuildInfo
+//func (b BuildInfo) FormatDetailedProductInfo() string {
+//     base := []string{
+//             fmt.Sprintf("Product:       %s", b.Product),
+//             fmt.Sprintf("Version:       %s", b.Version),
+//             fmt.Sprintf("Git Tag:       %s", b.GitTag),
+//             fmt.Sprintf("Git Commit:    %s", b.GitCommit),
+//             fmt.Sprintf("Build Date:    %s", b.BuildDate),
+//     }
+//     if b.BasedOnDubbo != "" {
+//             base = append(base, fmt.Sprintf("Based on dubbo: %s", 
b.BasedOnDubbo))
+//     }
+//     return strings.Join(
+//             base,
+//             "\n",
+//     )
+//}
 
-func init() {
-       Build = BuildInfo{
-               Product:      Product,
-               Version:      version,
-               GitTag:       gitTag,
-               GitCommit:    gitCommit,
-               BuildDate:    buildDate,
-               BasedOnDubbo: basedOndubbo,
-       }
-}
+//func shortCommit(c string) string {
+//     if len(c) < 7 {
+//             return c
+//     }
+//     return c[:7]
+//}
+//
+//func (b BuildInfo) AsMap() map[string]string {
+//     res := map[string]string{
+//             "product":    b.Product,
+//             "version":    b.Version,
+//             "build_date": b.BuildDate,
+//             "git_commit": shortCommit(b.GitCommit),
+//             "git_tag":    b.GitTag,
+//     }
+//     if b.BasedOnDubbo != "" {
+//             res["based_on_dubbo"] = b.BasedOnDubbo
+//     }
+//     return res
+//}
+//
+//func (b BuildInfo) UserAgent(component string) string {
+//     commit := shortCommit(b.GitCommit)
+//     if b.BasedOnDubbo != "" {
+//             commit = fmt.Sprintf("%s/dubbo-%s", commit, b.BasedOnDubbo)
+//     }
+//     return fmt.Sprintf("%s/%s (%s; %s; %s/%s)",
+//             component,
+//             b.Version,
+//             runtime.GOOS,
+//             runtime.GOARCH,
+//             b.Product,
+//             commit)
+//}
+//
+//var Build BuildInfo
+//
+//func init() {
+//     Build = BuildInfo{
+//             Product:      Product,
+//             Version:      version,
+//             GitTag:       gitTag,
+//             GitCommit:    gitCommit,
+//             BuildDate:    buildDate,
+//             BasedOnDubbo: basedOndubbo,
+//     }
+//}

Reply via email to