diff --git a/ctl/dump/dump.go b/ctl/dump/dump.go index 962c5aa8a..4982f246d 100644 --- a/ctl/dump/dump.go +++ b/ctl/dump/dump.go @@ -29,6 +29,7 @@ import ( "github.com/spf13/cobra" "google.golang.org/protobuf/encoding/protojson" + "sigs.k8s.io/yaml" adminv2 "kmesh.net/kmesh/api/v2/admin" "kmesh.net/kmesh/ctl/utils" @@ -62,7 +63,7 @@ kmeshctl dump kernel-native -o json`, }, } - cmd.Flags().StringVarP(&outputFormat, "output", "o", "table", "Output format: table or json") + cmd.Flags().StringVarP(&outputFormat, "output", "o", "table", "Output format: table, json, or yaml") return cmd } @@ -108,6 +109,17 @@ func RunDump(cmd *cobra.Command, args []string, outputFormat string) error { return nil } + if outputFormat == "yaml" { + yamlData, err := yaml.JSONToYAML(body) + if err != nil { + log.Errorf("failed to convert JSON to YAML: %v, falling back to raw output", err) + fmt.Println(string(body)) + return nil + } + fmt.Println(string(yamlData)) + return nil + } + switch mode { case constants.KernelNativeMode: printKernelNativeTable(body) diff --git a/docs/ctl/kmeshctl_dump.md b/docs/ctl/kmeshctl_dump.md index c914e3218..545b20633 100644 --- a/docs/ctl/kmeshctl_dump.md +++ b/docs/ctl/kmeshctl_dump.md @@ -23,7 +23,7 @@ kmeshctl dump kernel-native -o json ```bash -h, --help help for dump - -o, --output string Output format: table or json (default "table") + -o, --output string Output format: table, json, or yaml (default "table") ``` ### SEE ALSO