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 3928ebc9 [dubboctl] add profile show command logic (#568)
3928ebc9 is described below

commit 3928ebc940de1c4b6b6007d636b057f709abb982
Author: Jian Zhong <[email protected]>
AuthorDate: Thu Jan 30 14:58:59 2025 +0800

    [dubboctl] add profile show command logic (#568)
---
 dubboctl/cmd/repo.go                  | 12 +++----
 dubboctl/pkg/util/path.go             |  2 +-
 dubboctl/pkg/validate/validate.go     |  2 +-
 go.mod                                |  1 +
 go.sum                                |  1 +
 operator/cmd/cluster/profile.go       | 59 +++++++++++++++++++++++++++++++++--
 operator/cmd/cluster/shared.go        | 35 +++++++++++++++------
 operator/pkg/install/installer.go     |  2 +-
 operator/pkg/render/manifest.go       |  2 +-
 operator/pkg/uninstall/uninstaller.go |  2 +-
 operator/pkg/util/errs.go             |  2 +-
 11 files changed, 96 insertions(+), 24 deletions(-)

diff --git a/dubboctl/cmd/repo.go b/dubboctl/cmd/repo.go
index 52a4c779..74676f06 100644
--- a/dubboctl/cmd/repo.go
+++ b/dubboctl/cmd/repo.go
@@ -23,13 +23,13 @@ func RepoCmd(_ cli.Context, cmd *cobra.Command, 
clientFactory ClientFactory) *co
                Short: "Manage exist Dubbo sdk module libraries",
                Long:  "The repo command Manage existing Dubbo SDK module 
libraries",
                Example: `  # Add a new template library.
-  dubboctl repo add [name] [URL]
+  dubboctl repo add [<name>] [<url>]
        
   # View the list of template library.
   dubboctl repo list
        
   # Remove an existing template library.
-  dubboctl repo remove [name]
+  dubboctl repo remove [<name>]
 `,
        }
 
@@ -43,7 +43,7 @@ func RepoCmd(_ cli.Context, cmd *cobra.Command, clientFactory 
ClientFactory) *co
 
 func addCmd(cmd *cobra.Command, clientFactory ClientFactory) *cobra.Command {
        ac := &cobra.Command{
-               Use:   "add [name] [URL]",
+               Use:   "add [<name>] [<url>]",
                Short: "Add a new template library.",
                Long:  "The add subcommand is used to add a new template 
library.",
                RunE: func(cmd *cobra.Command, args []string) error {
@@ -54,14 +54,14 @@ func addCmd(cmd *cobra.Command, clientFactory 
ClientFactory) *cobra.Command {
 }
 
 func runAdd(cmd *cobra.Command, args []string, clientFactory ClientFactory) 
(err error) {
-       if err = util.CreatePath(); err != nil {
+       if err = util.GetCreatePath(); err != nil {
                return
        }
        client, done := clientFactory()
        defer done()
 
        if len(args) != 2 {
-               return fmt.Errorf("Usage: dubboctl repo add [name] [URL]")
+               return fmt.Errorf("Usage: dubboctl repo add [<name>] [<url>]")
        }
 
        p := struct {
@@ -114,7 +114,7 @@ func runList(cmd *cobra.Command, args []string, 
clientFactory ClientFactory) (er
 
 func removeCmd(cmd *cobra.Command, clientFactory ClientFactory) *cobra.Command 
{
        rc := &cobra.Command{
-               Use:     "remove [name]",
+               Use:     "remove [<name>]",
                Short:   "Remove an existing template library.",
                Long:    "The delete subcommand is used to delete a template 
from an existing repository.",
                Aliases: []string{"delete"},
diff --git a/dubboctl/pkg/util/path.go b/dubboctl/pkg/util/path.go
index 328c00ab..402ee27e 100644
--- a/dubboctl/pkg/util/path.go
+++ b/dubboctl/pkg/util/path.go
@@ -17,7 +17,7 @@ func Dir() (path string) {
        return
 }
 
-func CreatePath() (err error) {
+func GetCreatePath() (err error) {
        if err = os.MkdirAll(Dir(), os.ModePerm); err != nil {
                return fmt.Errorf("error creating global config path: %v", err)
        }
diff --git a/dubboctl/pkg/validate/validate.go 
b/dubboctl/pkg/validate/validate.go
index eb83ffc8..ecdd3ece 100644
--- a/dubboctl/pkg/validate/validate.go
+++ b/dubboctl/pkg/validate/validate.go
@@ -83,7 +83,7 @@ func (v *validator) validateResource(dubboNamespace string, 
un *unstructured.Uns
                                return nil, err
                        }
                        if len(warnings) > 0 {
-                               return validation.Warning(warnings.ToErrors()), 
nil
+                               return validation.Warning(warnings.ToError()), 
nil
                        }
                }
        }
diff --git a/go.mod b/go.mod
index 06254950..9cebb9ef 100644
--- a/go.mod
+++ b/go.mod
@@ -31,6 +31,7 @@ require (
        github.com/envoyproxy/protoc-gen-validate v1.0.4
        github.com/fatih/color v1.18.0
        github.com/fullstorydev/grpcurl v1.9.1
+       github.com/ghodss/yaml v1.0.0
        github.com/gin-gonic/gin v1.9.1
        github.com/go-co-op/gocron v1.9.0
        github.com/go-git/go-billy/v5 v5.6.2
diff --git a/go.sum b/go.sum
index ba2d98e2..0d9a259a 100644
--- a/go.sum
+++ b/go.sum
@@ -472,6 +472,7 @@ github.com/fxamacker/cbor/v2 v2.7.0/go.mod 
h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXE
 github.com/gabriel-vasile/mimetype v1.4.2 
h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
 github.com/gabriel-vasile/mimetype v1.4.2/go.mod 
h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
 github.com/getsentry/raven-go v0.2.0/go.mod 
h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
+github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
 github.com/ghodss/yaml v1.0.0/go.mod 
h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
 github.com/gin-contrib/sse v0.1.0 
h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
 github.com/gin-contrib/sse v0.1.0/go.mod 
h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
diff --git a/operator/cmd/cluster/profile.go b/operator/cmd/cluster/profile.go
index 23ecd77d..524e77cc 100644
--- a/operator/cmd/cluster/profile.go
+++ b/operator/cmd/cluster/profile.go
@@ -1,12 +1,15 @@
 package cluster
 
 import (
+       "encoding/json"
        "fmt"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
        "github.com/apache/dubbo-kubernetes/operator/pkg/helm"
        "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
+       "github.com/ghodss/yaml"
        "github.com/spf13/cobra"
        "sort"
+       "strings"
 )
 
 const (
@@ -64,9 +67,10 @@ func ProfileCmd(ctx cli.Context) *cobra.Command {
                        "  dubboctl profile list\n" +
                        "  dubboctl install --set profile=demo",
        }
-       pc.AddCommand(plc)
        addProfileListFlags(plc, plArgs)
        addProfileShowFlags(psc, psArgs)
+       pc.AddCommand(plc)
+       pc.AddCommand(psc)
        AddFlags(pc, rootArgs)
        return pc
 }
@@ -86,7 +90,7 @@ func profileListCmd(rootArgs *RootArgs, plArgs 
*profileListArgs) *cobra.Command
 func profileShowCmd(rootArgs *RootArgs, pdArgs *profileShowArgs) 
*cobra.Command {
        return &cobra.Command{
                Use:   "show [<profile>]",
-               Short: "Show an Dubbo configuration profile",
+               Short: "Shows an Dubbo configuration profile",
                Long:  "The show subcommand show the values in an Dubbo 
configuration profile.",
                Args: func(cmd *cobra.Command, args []string) error {
                        if len(args) > 1 {
@@ -135,5 +139,56 @@ func profileShow(args []string, rootArgs *RootArgs, pdArgs 
*profileShowArgs, l c
                setFlags = append(setFlags, "profile="+args[0])
        }
 
+       y, _, err := GenerateConfig(pdArgs.filenames, setFlags, nil, l)
+       if err != nil {
+               return err
+       }
+
+       switch pdArgs.outputFormat {
+       case jsonOutput:
+               j, err := yamlToPrettyJSON(y)
+               if err != nil {
+                       return err
+               }
+               l.Print(j + "\n")
+       case yamlOutput:
+               l.Print(y + "\n")
+       case flagsOutput:
+               f, err := yamlToFlags(y)
+               if err != nil {
+                       return err
+               }
+               l.Print(strings.Join(f, "\n") + "\n")
+       }
+
        return nil
 }
+
+func yamlToFlags(yml string) ([]string, error) {
+
+       uglyJSON, err := yaml.YAMLToJSON([]byte(yml))
+       if err != nil {
+               return []string{}, err
+       }
+       var decoded map[string]interface{}
+       if err := json.Unmarshal(uglyJSON, &decoded); err != nil {
+               return []string{}, err
+       }
+       return nil, nil
+}
+
+func yamlToPrettyJSON(yml string) (string, error) {
+       uglyJSON, err := yaml.YAMLToJSON([]byte(yml))
+       if err != nil {
+               return "", err
+       }
+       var decoded map[string]interface{}
+       if err := json.Unmarshal(uglyJSON, &decoded); err != nil {
+               return "", err
+       }
+       prettyJSON, err := json.MarshalIndent(decoded, "", "    ")
+       if err != nil {
+               return "", err
+       }
+       return string(prettyJSON), nil
+}
diff --git a/operator/cmd/cluster/shared.go b/operator/cmd/cluster/shared.go
index c63c30dd..299e90c1 100644
--- a/operator/cmd/cluster/shared.go
+++ b/operator/cmd/cluster/shared.go
@@ -53,18 +53,17 @@ func NewPrinterForWriter(w io.Writer) Printer {
        return &writerPrinter{writer: w}
 }
 
-func GenerateConfig(filenames []string, setFlags []string, force bool, 
kubeConfig *rest.Config,
-       l clog.Logger) (string, *dopv1alpha1.DubboOperator, error) {
+func GenerateConfig(filenames []string, setFlags []string,
+       kubeConfig *rest.Config, l clog.Logger) (string, 
*dopv1alpha1.DubboOperator, error) {
        if err := validateSetFlags(setFlags); err != nil {
                return "", nil, err
        }
 
-       _, _, err := readYamlProfile(filenames, setFlags, force, l)
+       fy, profile, err := readYamlProfile(filenames, setFlags, l)
        if err != nil {
                return "", nil, err
        }
-
-       return "", nil, err
+       return overlayYAMLStrings(profile, fy, setFlags, kubeConfig, l)
 }
 
 func validateSetFlags(setFlags []string) error {
@@ -77,23 +76,39 @@ func validateSetFlags(setFlags []string) error {
        return nil
 }
 
-func readYamlProfile(filenames []string, setFlags []string, force bool, l 
clog.Logger) (string, string, error) {
+func overlayYAMLStrings(
+       profile string, fy string,
+       setFlags []string, kubeConfig *rest.Config, l clog.Logger,
+) (string, *dopv1alpha1.DubboOperator, error) {
+       dopsString, dops, err := GenDOPFromProfile(profile, fy, setFlags, 
kubeConfig, l)
+       if err != nil {
+               return "", nil, err
+       }
+
+       return dopsString, dops, nil
+}
+
+func GenDOPFromProfile(profileOrPath, fileOverlayYAML string, setFlags 
[]string, kubeConfig *rest.Config, l clog.Logger) (string, 
*dopv1alpha1.DubboOperator, error) {
+       return "", nil, nil
+}
+
+func readYamlProfile(filenames []string, setFlags []string, l clog.Logger) 
(string, string, error) {
        profile := DefaultProfileName
-       fy, fp, err := ParseYAMLfilenames(filenames, force, l)
+       fy, fp, err := ParseYAMLfilenames(filenames, l)
        if err != nil {
                return "", "", err
        }
        if fp != "" {
                profile = fp
        }
-       psf := GetValueForSetFlag(setFlags, "profile")
+       psf := getValueForSetFlag(setFlags, "profile")
        if psf != "" {
                profile = psf
        }
        return fy, profile, nil
 }
 
-func ParseYAMLfilenames(filenames []string, force bool, l clog.Logger) 
(overlayYAML string, profile string, err error) {
+func ParseYAMLfilenames(filenames []string, l clog.Logger) (overlayYAML 
string, profile string, err error) {
        if filenames == nil {
                return "", "", nil
        }
@@ -130,7 +145,7 @@ func readLayeredYAMLs(filenames []string, stdinReader 
io.Reader) (string, error)
        return ly, nil
 }
 
-func GetValueForSetFlag(setFlags []string, path string) string {
+func getValueForSetFlag(setFlags []string, path string) string {
        ret := ""
        for _, sf := range setFlags {
                p, v := getPV(sf)
diff --git a/operator/pkg/install/installer.go 
b/operator/pkg/install/installer.go
index e2c52401..ee9fc9ba 100644
--- a/operator/pkg/install/installer.go
+++ b/operator/pkg/install/installer.go
@@ -202,7 +202,7 @@ func (i Installer) prune(manifests []manifest.ManifestSet) 
error {
                        }
                }
        }
-       return errs.ToErrors()
+       return errs.ToError()
 }
 
 var componentDependencies = map[component.Name][]component.Name{
diff --git a/operator/pkg/render/manifest.go b/operator/pkg/render/manifest.go
index aa914917..4c7cd22d 100644
--- a/operator/pkg/render/manifest.go
+++ b/operator/pkg/render/manifest.go
@@ -165,7 +165,7 @@ func GenerateManifest(files []string, setFlags []string, 
logger clog.Logger, _ k
 
 func validateDubboOperator(dop values.Map, logger clog.Logger) error {
        warnings, errs := validation.ParseAndValidateDubboOperator(dop)
-       if err := errs.ToErrors(); err != nil {
+       if err := errs.ToError(); err != nil {
                return err
        }
        if logger != nil {
diff --git a/operator/pkg/uninstall/uninstaller.go 
b/operator/pkg/uninstall/uninstaller.go
index abf88469..5a8cb50d 100644
--- a/operator/pkg/uninstall/uninstaller.go
+++ b/operator/pkg/uninstall/uninstaller.go
@@ -94,7 +94,7 @@ func DeleteObjectsList(c kube.CLIClient, dryRun bool, log 
clog.Logger, objectsLi
                        }
                }
        }
-       return errs.ToErrors()
+       return errs.ToError()
 }
 
 func DeleteResource(kc kube.CLIClient, dryRun bool, _ clog.Logger, obj 
*unstructured.Unstructured) error {
diff --git a/operator/pkg/util/errs.go b/operator/pkg/util/errs.go
index d2d6cd10..8574871f 100644
--- a/operator/pkg/util/errs.go
+++ b/operator/pkg/util/errs.go
@@ -12,7 +12,7 @@ func (e Errors) Error() string {
        return ToString(e, defaultSeparator)
 }
 
-func (e Errors) ToErrors() error {
+func (e Errors) ToError() error {
        if len(e) == 0 {
                return nil
        }

Reply via email to