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 f5837f05 [dubboctl] add create validate logic (#607)
f5837f05 is described below
commit f5837f0541dcd094aa63502fe051521cc8dd1461
Author: Jian Zhong <[email protected]>
AuthorDate: Tue Feb 18 15:21:08 2025 +0800
[dubboctl] add create validate logic (#607)
---
dubboctl/cmd/create.go | 100 +++++++++++++++++++++++++++++++++++++++++++++
dubboctl/pkg/sdk/client.go | 9 ++++
2 files changed, 109 insertions(+)
diff --git a/dubboctl/cmd/create.go b/dubboctl/cmd/create.go
index e4e02595..ab702828 100644
--- a/dubboctl/cmd/create.go
+++ b/dubboctl/cmd/create.go
@@ -1,13 +1,16 @@
package cmd
import (
+ "errors"
"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/ory/viper"
"github.com/spf13/cobra"
"os"
+ "strings"
)
type createArgs struct {
@@ -82,6 +85,7 @@ type createConfig struct {
Path string
Runtime string
Template string
+ Repo string
DirName string
Initialzed bool
}
@@ -112,6 +116,10 @@ func runCreate(cmd *cobra.Command, args []string,
clientFactory ClientFactory) e
dclient, cancel := clientFactory()
defer cancel()
+ if err = dcfg.validate(dclient); err != nil {
+ return err
+ }
+
_, err = dclient.Initialize(&dubbo.DubboConfig{
Root: dcfg.Path,
Name: dcfg.DirName,
@@ -125,6 +133,98 @@ func runCreate(cmd *cobra.Command, args []string,
clientFactory ClientFactory) e
return nil
}
+type ErrNoRuntime error
+type ErrInvalidRuntime error
+type ErrInvalidTemplate error
+
+func (c createConfig) validate(client *sdk.Client) (err error) {
+ if c.Runtime == "" {
+ return noRuntimeError(client)
+ }
+
+ if c.Runtime != "" && c.Repo == "" &&
+ !isValidRuntime(client, c.Runtime) {
+ return newInvalidRuntimeError(client, c.Runtime)
+ }
+
+ if c.Template != "" && c.Repo == "" &&
+ !isValidTemplate(client, c.Runtime, c.Template) {
+ return newInvalidTemplateError(client, c.Runtime, c.Template)
+ }
+
+ return
+}
+
+func newInvalidRuntimeError(client *sdk.Client, runtime string) error {
+ b := strings.Builder{}
+ fmt.Fprintf(&b, "The language runtime '%v' is not recognized.\n",
runtime)
+ runtimes, err := client.Runtimes()
+ if err != nil {
+ return err
+ }
+ for _, v := range runtimes {
+ fmt.Fprintf(&b, " %v\n", v)
+ }
+ return ErrInvalidRuntime(errors.New(b.String()))
+}
+
+func isValidTemplate(client *sdk.Client, runtime, template string) bool {
+ if !isValidRuntime(client, runtime) {
+ return false
+ }
+ templates, err := client.Templates().List(runtime)
+ if err != nil {
+ return false
+ }
+ for _, v := range templates {
+ if v == template {
+ return true
+ }
+ }
+ return false
+}
+
+func isValidRuntime(client *sdk.Client, runtime string) bool {
+ runtimes, err := client.Runtimes()
+ if err != nil {
+ return false
+ }
+ for _, v := range runtimes {
+ if v == runtime {
+ return true
+ }
+ }
+ return false
+}
+
+func noRuntimeError(client *sdk.Client) error {
+ b := strings.Builder{}
+ fmt.Fprintln(&b, "Required flag \"language\" not set.")
+ fmt.Fprintln(&b, "Available language runtimes are:")
+ runtimes, err := client.Runtimes()
+ if err != nil {
+ return err
+ }
+ for _, v := range runtimes {
+ fmt.Fprintf(&b, " %v\n", v)
+ }
+ return ErrNoRuntime(errors.New(b.String()))
+}
+
+func newInvalidTemplateError(client *sdk.Client, runtime, template string)
error {
+ b := strings.Builder{}
+ fmt.Fprintf(&b, "The template '%v' was not found for language runtime
'%v'.\n", template, runtime)
+ fmt.Fprintln(&b, "Available templates for this language runtime are:")
+ templates, err := client.Templates().List(runtime)
+ if err != nil {
+ return err
+ }
+ for _, v := range templates {
+ fmt.Fprintf(&b, " %v\n", v)
+ }
+ return ErrInvalidTemplate(errors.New(b.String()))
+}
+
func cwd() (cwd string) {
cwd, err := os.Getwd()
if err != nil {
diff --git a/dubboctl/pkg/sdk/client.go b/dubboctl/pkg/sdk/client.go
index bce5ed3e..732446a2 100644
--- a/dubboctl/pkg/sdk/client.go
+++ b/dubboctl/pkg/sdk/client.go
@@ -72,6 +72,15 @@ func (c *Client) Templates() *Templates {
func (c *Client) Runtimes() ([]string, error) {
runtimes := util.NewSortedSet()
+ repos, err := c.Repositories().All()
+ if err != nil {
+ return []string{}, err
+ }
+ for _, repo := range repos {
+ for _, runtime := range repo.Runtimes {
+ runtimes.Add(runtime.Name)
+ }
+ }
return runtimes.Items(), nil
}