From 6f898971e813888efe839db3a39714e94e00b1e8 Mon Sep 17 00:00:00 2001 From: Jason Madigan Date: Wed, 1 May 2024 10:40:35 +0100 Subject: [PATCH] reduce yaml output verbosity --- cmd/generate_gatewayapi_httproute.go | 18 +++++++++++------- cmd/generate_kuadrant_authpolicy.go | 18 +++++++++++------- cmd/generate_kuadrant_ratelimitpolicy.go | 20 +++++++++++++------- go.mod | 1 + go.sum | 2 ++ 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/cmd/generate_gatewayapi_httproute.go b/cmd/generate_gatewayapi_httproute.go index d8e5ab4..f2c1582 100644 --- a/cmd/generate_gatewayapi_httproute.go +++ b/cmd/generate_gatewayapi_httproute.go @@ -5,10 +5,10 @@ import ( "fmt" "github.com/getkin/kin-openapi/openapi3" + "github.com/ghodss/yaml" "github.com/kuadrant/kuadrantctl/pkg/gatewayapi" "github.com/kuadrant/kuadrantctl/pkg/utils" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" ) @@ -57,15 +57,19 @@ func runGenerateGatewayApiHttpRoute(cmd *cobra.Command, args []string) error { } httpRoute := buildHTTPRoute(doc) + jsonBytes, err := json.Marshal(httpRoute) + if err != nil { + return err + } var outputBytes []byte if generateGatewayAPIHTTPRouteFormat == "json" { - outputBytes, err = json.Marshal(httpRoute) - } else { // default to YAML if not explicitly JSON - outputBytes, err = yaml.Marshal(httpRoute) - } - if err != nil { - return err + outputBytes = jsonBytes + } else { + outputBytes, err = yaml.JSONToYAML(jsonBytes) // use `omitempty`'s from the json Marshal + if err != nil { + return err + } } fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes)) diff --git a/cmd/generate_kuadrant_authpolicy.go b/cmd/generate_kuadrant_authpolicy.go index 51878c5..7b338d6 100644 --- a/cmd/generate_kuadrant_authpolicy.go +++ b/cmd/generate_kuadrant_authpolicy.go @@ -5,9 +5,9 @@ import ( "fmt" "github.com/getkin/kin-openapi/openapi3" + "github.com/ghodss/yaml" kuadrantapiv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" @@ -61,15 +61,19 @@ func runGenerateKuadrantAuthPolicy(cmd *cobra.Command, args []string) error { } ap := buildAuthPolicy(doc) + jsonBytes, err := json.Marshal(ap) + if err != nil { + return err + } var outputBytes []byte if generateAuthPolicyFormat == "json" { - outputBytes, err = json.Marshal(ap) - } else { // default to YAML if not explicitly JSON - outputBytes, err = yaml.Marshal(ap) - } - if err != nil { - return err + outputBytes = jsonBytes + } else { + outputBytes, err = yaml.JSONToYAML(jsonBytes) // use `omitempty`'s from the json Marshal + if err != nil { + return err + } } fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes)) diff --git a/cmd/generate_kuadrant_ratelimitpolicy.go b/cmd/generate_kuadrant_ratelimitpolicy.go index 713e3dd..503c35b 100644 --- a/cmd/generate_kuadrant_ratelimitpolicy.go +++ b/cmd/generate_kuadrant_ratelimitpolicy.go @@ -6,9 +6,10 @@ import ( "os" "github.com/getkin/kin-openapi/openapi3" + "github.com/ghodss/yaml" + kuadrantapiv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" @@ -63,16 +64,21 @@ func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, args []string) error rlp := buildRateLimitPolicy(doc) - var outputBytes []byte - if generateRateLimitPolicyFormat == "json" { - outputBytes, err = json.Marshal(rlp) - } else { // default to YAML if not explicitly JSON - outputBytes, err = yaml.Marshal(rlp) - } + jsonBytes, err := json.Marshal(rlp) if err != nil { return err } + var outputBytes []byte + if generateRateLimitPolicyFormat == "json" { + outputBytes = jsonBytes + } else { + outputBytes, err = yaml.JSONToYAML(jsonBytes) // use `omitempty`'s from the json Marshal + if err != nil { + return err + } + } + fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes)) return nil } diff --git a/go.mod b/go.mod index fa79738..9b254cc 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/evanphx/json-patch/v5 v5.7.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/ghodss/yaml v1.0.0 // indirect github.com/go-logr/logr v1.2.4 // indirect github.com/go-logr/zapr v1.2.4 // indirect github.com/go-openapi/jsonpointer v0.20.0 // indirect diff --git a/go.sum b/go.sum index 661e0eb..d0bb24f 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/getkin/kin-openapi v0.120.0 h1:MqJcNJFrMDFNc07iwE8iFC5eT2k/NPUFDIpNeiZv8Jg= github.com/getkin/kin-openapi v0.120.0/go.mod h1:PCWw/lfBrJY4HcdqE3jj+QFkaFK8ABoqo7PvqVhXXqw= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=