Add JSON and YAML output options to flux tree cmd

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
pull/1998/head
Stefan Prodan 3 years ago
parent 80ef184b60
commit 078cfe92c2
No known key found for this signature in database
GPG Key ID: 3299AEB0E4085BAF

@ -18,6 +18,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"strings" "strings"
@ -28,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
) )
var treeKsCmd = &cobra.Command{ var treeKsCmd = &cobra.Command{
@ -45,12 +47,15 @@ var treeKsCmd = &cobra.Command{
type TreeKsFlags struct { type TreeKsFlags struct {
compact bool compact bool
output string
} }
var treeKsArgs TreeKsFlags var treeKsArgs TreeKsFlags
func init() { func init() {
treeKsCmd.Flags().BoolVar(&treeKsArgs.compact, "compact", false, "list Flux resources only.") treeKsCmd.Flags().BoolVar(&treeKsArgs.compact, "compact", false, "list Flux resources only.")
treeKsCmd.Flags().StringVarP(&treeKsArgs.output, "output", "o", "",
"the format in which the tree should be printed. can be 'json' or 'yaml'")
treeCmd.AddCommand(treeKsCmd) treeCmd.AddCommand(treeKsCmd)
} }
@ -89,7 +94,22 @@ func treeKsCmdRun(cmd *cobra.Command, args []string) error {
return err return err
} }
switch treeKsArgs.output {
case "json":
data, err := json.MarshalIndent(kTree, "", " ")
if err != nil {
return err
}
rootCmd.Println(string(data))
case "yaml":
data, err := yaml.Marshal(kTree)
if err != nil {
return err
}
rootCmd.Println(string(data))
default:
rootCmd.Println(kTree.Print()) rootCmd.Println(kTree.Print())
}
return nil return nil
} }

@ -36,8 +36,8 @@ const (
type ( type (
objMetadataTree struct { objMetadataTree struct {
objMetadata object.ObjMetadata Resource object.ObjMetadata `json:"resource"`
items []ObjMetadataTree ResourceTree []ObjMetadataTree `json:"resources,omitempty"`
} }
ObjMetadataTree interface { ObjMetadataTree interface {
@ -58,27 +58,27 @@ type (
func New(objMetadata object.ObjMetadata) ObjMetadataTree { func New(objMetadata object.ObjMetadata) ObjMetadataTree {
return &objMetadataTree{ return &objMetadataTree{
objMetadata: objMetadata, Resource: objMetadata,
items: []ObjMetadataTree{}, ResourceTree: []ObjMetadataTree{},
} }
} }
func (t *objMetadataTree) Add(objMetadata object.ObjMetadata) ObjMetadataTree { func (t *objMetadataTree) Add(objMetadata object.ObjMetadata) ObjMetadataTree {
n := New(objMetadata) n := New(objMetadata)
t.items = append(t.items, n) t.ResourceTree = append(t.ResourceTree, n)
return n return n
} }
func (t *objMetadataTree) AddTree(tree ObjMetadataTree) { func (t *objMetadataTree) AddTree(tree ObjMetadataTree) {
t.items = append(t.items, tree) t.ResourceTree = append(t.ResourceTree, tree)
} }
func (t *objMetadataTree) Text() string { func (t *objMetadataTree) Text() string {
return ssa.FmtObjMetadata(t.objMetadata) return ssa.FmtObjMetadata(t.Resource)
} }
func (t *objMetadataTree) Items() []ObjMetadataTree { func (t *objMetadataTree) Items() []ObjMetadataTree {
return t.items return t.ResourceTree
} }
func (t *objMetadataTree) Print() string { func (t *objMetadataTree) Print() string {

Loading…
Cancel
Save