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 a9257550 [dubboctl] Update sdk client factory logic (#541)
a9257550 is described below

commit a9257550c5c58ca9a767608eb7ad85c7443e940c
Author: Jian Zhong <[email protected]>
AuthorDate: Tue Jan 14 21:25:28 2025 +0800

    [dubboctl] Update sdk client factory logic (#541)
---
 dubboctl/cmd/create.go           | 51 +++++++++------------------------
 dubboctl/cmd/root.go             | 26 +++++++++++++----
 dubboctl/pkg/sdk/client.go       | 19 ++----------
 dubboctl/pkg/util/sortedset.go   | 62 ++++++++++++++++++++++++++++++++++++++++
 operator/cmd/cluster/manifest.go |  8 ++----
 5 files changed, 100 insertions(+), 66 deletions(-)

diff --git a/dubboctl/cmd/create.go b/dubboctl/cmd/create.go
index 11e135b7..efa9e6ec 100644
--- a/dubboctl/cmd/create.go
+++ b/dubboctl/cmd/create.go
@@ -3,11 +3,8 @@ package cmd
 import (
        "fmt"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
-       "github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk/dubbo"
        "github.com/apache/dubbo-kubernetes/operator/cmd/cluster"
-       "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
-       "github.com/apache/dubbo-kubernetes/pkg/kube"
        "github.com/ory/viper"
        "github.com/spf13/cobra"
        "os"
@@ -23,16 +20,14 @@ func addTemplateFlags(cmd *cobra.Command, args 
*templateArgs) {
        cmd.PersistentFlags().StringVarP(&args.template, "template", "t", "", 
"java or go sdk template")
 }
 
-func CreateCmd(ctx cli.Context) *cobra.Command {
+func CreateCmd(_ cli.Context, cmd *cobra.Command, clientFactory ClientFactory) 
*cobra.Command {
        rootArgs := &cluster.RootArgs{}
        tempArgs := &templateArgs{}
-       sc := sdkGenerateCmd(ctx, rootArgs, tempArgs)
+       sc := sdkGenerateCmd(cmd, clientFactory)
        cc := &cobra.Command{
                Use:   "create",
                Short: "Create a custom dubbo sdk sample",
-               RunE: func(cmd *cobra.Command, args []string) error {
-                       return nil
-               },
+               Long:  "The create command will generates dubbo sdk.",
        }
        cluster.AddFlags(cc, rootArgs)
        cluster.AddFlags(sc, rootArgs)
@@ -41,38 +36,20 @@ func CreateCmd(ctx cli.Context) *cobra.Command {
        return cc
 }
 
-var kubeClientFunc func() (kube.CLIClient, error)
-
-func sdkGenerateCmd(ctx cli.Context, _ *cluster.RootArgs, tempArgs 
*templateArgs) *cobra.Command {
+func sdkGenerateCmd(cmd *cobra.Command, clientFactory ClientFactory) 
*cobra.Command {
        return &cobra.Command{
                Use:   "sdk",
-               Short: "Generate SDK samples for Dubbo supported languages",
-               Long:  "The SDK subcommand generates an SDK sample provided by 
Dubbo supported languages.",
+               Short: "Generate sdk samples for Dubbo supported languages",
+               Long:  "The sdk subcommand generates an SDK sample provided by 
Dubbo supported languages.",
                Example: `  # Create a java sample sdk.
   dubboctl create sdk java -t mydubbo
 
   # Create a go sample sdk.
   dubboctl create sdk go -t mydubbogo
 `,
-               Args: func(cmd *cobra.Command, args []string) error {
-                       if len(args) > 0 {
-                               return fmt.Errorf("generate accepts no 
positional arguments, got %#v", args)
-                       }
-                       return nil
-               },
+               Args: cobra.ExactArgs(1),
                RunE: func(cmd *cobra.Command, args []string) error {
-                       if kubeClientFunc == nil {
-                               kubeClientFunc = ctx.CLIClient
-                       }
-                       var kubeClient kube.CLIClient
-                       kc, err := kubeClientFunc()
-                       if err != nil {
-                               return err
-                       }
-                       kubeClient = kc
-
-                       cl := clog.NewConsoleLogger(cmd.OutOrStdout(), 
cmd.ErrOrStderr(), cluster.InstallerScope)
-                       return runCreate(kubeClient, tempArgs, cl)
+                       return runCreate(cmd, clientFactory)
                },
        }
 }
@@ -85,14 +62,12 @@ type createArgs struct {
        Initialzed bool
 }
 
-func runCreate(kc kube.CLIClient, tempArgs *templateArgs, cl clog.Logger) 
error {
-       dcfg, err := newCreate(kc, tempArgs, cl)
+func runCreate(cmd *cobra.Command, clientFactory ClientFactory) error {
+       dcfg, err := newCreate(cmd, clientFactory)
        if err != nil {
                return err
        }
-       var newClient sdk.ClientFactory
-       var cmd *cobra.Command
-       client, cancel := newClient()
+       client, cancel := clientFactory()
        defer cancel()
        _, err = client.Initialize(&dubbo.DubboConfig{
                Root:     dcfg.Path,
@@ -103,11 +78,11 @@ func runCreate(kc kube.CLIClient, tempArgs *templateArgs, 
cl clog.Logger) error
        if err != nil {
                return err
        }
-       fmt.Fprintf(cmd.OutOrStderr(), "Created %v dubbo application in %v\n", 
dcfg.Runtime, dcfg.Path)
+       fmt.Fprintf(cmd.OutOrStderr(), "Created %v dubbo sdk in %v\n", 
dcfg.Runtime, dcfg.Path)
        return nil
 }
 
-func newCreate(kc kube.CLIClient, tempArgs *templateArgs, cl clog.Logger) 
(dcfg createArgs, err error) {
+func newCreate(cmd *cobra.Command, clientFactory ClientFactory) (dcfg 
createArgs, err error) {
        var (
                path         string
                dirName      string
diff --git a/dubboctl/cmd/root.go b/dubboctl/cmd/root.go
index 826a21d3..91733f08 100644
--- a/dubboctl/cmd/root.go
+++ b/dubboctl/cmd/root.go
@@ -18,28 +18,43 @@ package cmd
 import (
        "flag"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
+       "github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk"
        "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"
 )
 
+type option func(*sdk.Client)
+
+type client struct {
+       clientFactory ClientFactory
+}
+
+type ClientFactory func(...option) (*sdk.Client, func())
+
 func AddFlags(cmd *cobra.Command) {
        cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
 }
 
 func GetRootCmd(args []string) *cobra.Command {
        rootCmd := &cobra.Command{
-               Use:          "dubboctl",
-               Short:        "Dubbo command line utilities",
-               SilenceUsage: true,
-               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)
        flags := rootCmd.PersistentFlags()
        rootOptions := cli.AddRootFlags(flags)
        ctx := cli.NewCLIContext(rootOptions)
+       dcfg := client{}
+       clientFactory := dcfg.clientFactory
+       if clientFactory == nil {
+               // TODO
+       }
 
        installCmd := cluster.InstallCmd(ctx)
        rootCmd.AddCommand(installCmd)
@@ -63,10 +78,9 @@ func GetRootCmd(args []string) *cobra.Command {
        rootCmd.AddCommand(versionCmd)
        hideFlags(versionCmd, cli.NamespaceFlag, cli.DubboNamespaceFlag, 
cli.ChartFlag)
 
-       createCmd := CreateCmd(ctx)
+       createCmd := CreateCmd(ctx, rootCmd, clientFactory)
        rootCmd.AddCommand(createCmd)
        hideFlags(createCmd, cli.NamespaceFlag, cli.DubboNamespaceFlag, 
cli.ChartFlag)
-
        return rootCmd
 }
 
diff --git a/dubboctl/pkg/sdk/client.go b/dubboctl/pkg/sdk/client.go
index f2968576..b0539d07 100644
--- a/dubboctl/pkg/sdk/client.go
+++ b/dubboctl/pkg/sdk/client.go
@@ -4,6 +4,7 @@ import (
        "bufio"
        "fmt"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk/dubbo"
+       "github.com/apache/dubbo-kubernetes/dubboctl/pkg/util"
        "github.com/pkg/errors"
        "github.com/spf13/cobra"
        "os"
@@ -17,17 +18,13 @@ type Client struct {
        templates *Templates
 }
 
-type Option func(*Client)
-
-type ClientFactory func(...Option) (*Client, func())
-
 func (c *Client) Templates() *Templates {
        return c.templates
 }
 
 func (c *Client) Runtimes() ([]string, error) {
-       // TODO
-       return nil, nil
+       runtimes := util.NewSortedSet()
+       return runtimes.Items(), nil
 }
 
 func (c *Client) Initialize(dcfg *dubbo.DubboConfig, initialized bool, _ 
*cobra.Command) (*dubbo.DubboConfig, error) {
@@ -100,16 +97,6 @@ func hasInitialized(path string) (bool, error) {
 func nameFromPath(path string) string {
        pathParts := strings.Split(strings.TrimRight(path, 
string(os.PathSeparator)), string(os.PathSeparator))
        return pathParts[len(pathParts)-1]
-       /* the above may have edge conditions as it assumes the trailing value
-        * is a directory name.  If errors are encountered, we _may_ need to 
use the
-        * inbuilt logic in the std lib and either check if the path indicated 
is a
-        * directory (appending slash) and then run:
-                                        base := 
filepath.Base(filepath.Dir(path))
-                                        if base == string(os.PathSeparator) || 
base == "." {
-                                                                        return 
"" // Consider it underivable: string zero value
-                                        }
-                                        return base
-       */
 }
 
 func assertEmptyRoot(path string) (err error) {
diff --git a/dubboctl/pkg/util/sortedset.go b/dubboctl/pkg/util/sortedset.go
new file mode 100644
index 00000000..ebf14b66
--- /dev/null
+++ b/dubboctl/pkg/util/sortedset.go
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package util
+
+import (
+       "sort"
+       "sync"
+)
+
+// sorted set of strings.
+//
+// write-optimized and suitable only for fairly small values of N.
+// Should this increase dramatically in size, a different implementation,
+// such as a linked list, might be more appropriate.
+type sortedSet struct {
+       members map[string]bool
+       sync.Mutex
+}
+
+func NewSortedSet() *sortedSet {
+       return &sortedSet{
+               members: make(map[string]bool),
+       }
+}
+
+func (s *sortedSet) Add(value string) {
+       s.Lock()
+       s.members[value] = true
+       s.Unlock()
+}
+
+func (s *sortedSet) Remove(value string) {
+       s.Lock()
+       delete(s.members, value)
+       s.Unlock()
+}
+
+func (s *sortedSet) Items() []string {
+       s.Lock()
+       defer s.Unlock()
+       n := []string{}
+       for k := range s.members {
+               n = append(n, k)
+       }
+       sort.Strings(n)
+       return n
+}
diff --git a/operator/cmd/cluster/manifest.go b/operator/cmd/cluster/manifest.go
index 0b75c668..7e513066 100644
--- a/operator/cmd/cluster/manifest.go
+++ b/operator/cmd/cluster/manifest.go
@@ -63,12 +63,8 @@ func manifestGenerateCmd(ctx cli.Context, _ *RootArgs, 
mgArgs *manifestGenerateA
   # Generate the demo profile
   dubboctl manifest generate --set profile=demo
 `,
-               Args: func(cmd *cobra.Command, args []string) error {
-                       if len(args) != 0 {
-                               return fmt.Errorf("generate accepts no 
positional arguments, got %#v", args)
-                       }
-                       return nil
-               },
+
+               Args: cobra.ExactArgs(1),
                RunE: func(cmd *cobra.Command, args []string) error {
                        if kubeClientFunc == nil {
                                kubeClientFunc = ctx.CLIClient

Reply via email to