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

Support -o json and -o name on get #60

Merged
merged 9 commits into from
Aug 28, 2024
Merged
Changes from 5 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
62 changes: 58 additions & 4 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -52,6 +53,41 @@ func buildAlias(name string) []string {
return []string{strings.ToLower(name), removeTrailingSIfAny(strings.ToLower(name)), removeTrailingSIfAny(name)}
}

func printResource(result interface{}, format string) error {
if format == "json" {
jsonOutput, err := json.MarshalIndent(result, "", " ")
if err != nil {
return fmt.Errorf("error marshalling JSON: %s\n%s", err, result)
}
fmt.Println(string(jsonOutput))
} else if format == "name" {
// show Kind/Name
switch res := result.(type) {
case []resource.Resource:
for _, r := range res {
fmt.Println(r.Kind + "/" + r.Name)
}
case resource.Resource:
fmt.Println(res.Kind + "/" + res.Name)
default:
return fmt.Errorf("unexpected resource type")
}
} else if format == "yaml" {
switch res := result.(type) {
case []resource.Resource:
for _, r := range res {
fmt.Println("---") // '---' indicates the start of a new document in YAML
r.PrintPreservingOriginalFieldOrder()
}
case resource.Resource:
res.PrintPreservingOriginalFieldOrder()
default:
return fmt.Errorf("unexpected resource type")
}
}
sderosiaux marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func initGet(kinds schema.KindCatalog) {
rootCmd.AddCommand(getCmd)

Expand Down Expand Up @@ -80,25 +116,42 @@ func initGet(kinds schema.KindCatalog) {
parentValue[i] = *v
}
var err error

format, err := cmd.Flags().GetString("output")
if err != nil {
fmt.Println("Error retrieving output format:", err)
return
}

if format != "json" && format != "yaml" && format != "name" {
fmt.Fprintf(os.Stderr, "Invalid output format: %s. Expected 'json' or 'yaml' or 'name'.\n", format)
return
}
sderosiaux marked this conversation as resolved.
Show resolved Hide resolved

if len(args) == 0 {
var result []resource.Resource
if isGatewayKind {
result, err = gatewayApiClient().Get(&kind, parentValue, queryParams)
} else {
result, err = consoleApiClient().Get(&kind, parentValue, queryParams)
}
for _, r := range result {
r.PrintPreservingOriginalFieldOrder()
fmt.Println("---")
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching resources: %s\n", err)
return
}
err = printResource(result, format)
} else if len(args) == 1 {
var result resource.Resource
if isGatewayKind {
result, err = gatewayApiClient().Describe(&kind, parentValue, args[0])
} else {
result, err = consoleApiClient().Describe(&kind, parentValue, args[0])
}
result.PrintPreservingOriginalFieldOrder()
if err != nil {
fmt.Fprintf(os.Stderr, "Error describing resource: %s\n", err)
return
}
err = printResource(result, format)
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down Expand Up @@ -127,6 +180,7 @@ func initGet(kinds schema.KindCatalog) {
kindCmd.MarkFlagRequired(flag.FlagName)
}
}
kindCmd.Flags().StringP("output", "o", "yaml", "Output format. One of: json|yaml|name")
getCmd.AddCommand(kindCmd)
}
}
Loading