Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all #62

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"encoding/json"
"fmt"
"os"
"sort"
"strconv"
"strings"

"github.com/conduktor/ctl/resource"
"github.com/conduktor/ctl/schema"
"github.com/conduktor/ctl/utils"
"github.com/spf13/cobra"
"github.com/thediveo/enumflag/v2"
)
Expand Down Expand Up @@ -114,6 +114,44 @@ func initGet(kinds schema.KindCatalog) {
rootCmd.AddCommand(getCmd)
var format OutputFormat = YAML

var allCmd = &cobra.Command{
Use: "all",
Short: "Get all global resources",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var allResources []resource.Resource

kindsByName := sortedKeys(kinds)

for _, key := range kindsByName {
kind := kinds[key]

// keep only the Kinds where kind.GetParentFlag() is empty and not of GatewayKind (demands extra configuration, TODO fix if config is provided)
if len(kind.GetParentFlag()) > 0 {
continue
}
if _, isGatewayKind := kind.GetLatestKindVersion().(*schema.GatewayKindVersion); isGatewayKind {
continue
}

resources, err := consoleApiClient().Get(&kind, []string{}, map[string]string{})
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching resource %s: %s\n", kind.GetName(), err)
continue
}
allResources = append(allResources, resources...)
}
err := printResource(allResources, format)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
},
}
allCmd.Flags().VarP(enumflag.New(&format, "output", OutputFormatIds, enumflag.EnumCaseInsensitive), "output", "o", "Output format. One of: json|yaml|name")
getCmd.AddCommand(allCmd)

// Add all kinds to the 'get' command
for name, kind := range kinds {
gatewayKind, isGatewayKind := kind.GetLatestKindVersion().(*schema.GatewayKindVersion)
args := cobra.MaximumNArgs(1)
Expand Down Expand Up @@ -176,23 +214,28 @@ func initGet(kinds schema.KindCatalog) {
kindCmd.MarkFlagRequired(flag)
}
for key, flag := range listFlags {
var flagSetted = false
var isFlagSet bool
if flag.Type == "string" {
flagSetted = true
isFlagSet = true
listFlagValue[key] = kindCmd.Flags().String(flag.FlagName, "", "")
} else if flag.Type == "boolean" {
flagSetted = true
isFlagSet = true
listFlagValue[key] = kindCmd.Flags().Bool(flag.FlagName, false, "")
} else {
if *debug || utils.CdkDebug() {
fmt.Fprintf(os.Stderr, "Unknown flag type %s\n", flag.Type)
}
}
if flagSetted && flag.Required {
if isFlagSet && flag.Required {
kindCmd.MarkFlagRequired(flag.FlagName)
}
}
kindCmd.Flags().VarP(enumflag.New(&format, "output", OutputFormatIds, enumflag.EnumCaseInsensitive), "output", "o", "Output format. One of: json|yaml|name")
getCmd.AddCommand(kindCmd)
}
}

func sortedKeys(kinds schema.KindCatalog) []string {
keys := make([]string, 0, len(kinds))
for key := range kinds {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
Loading