Skip to content

Commit

Permalink
Merge pull request #67 from Kuadrant/reduce-yaml-verbosity
Browse files Browse the repository at this point in the history
reduce yaml output verbosity
  • Loading branch information
jasonmadigan authored May 1, 2024
2 parents 484dc61 + 6f89897 commit b87543f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
18 changes: 11 additions & 7 deletions cmd/generate_gatewayapi_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
Expand Down
18 changes: 11 additions & 7 deletions cmd/generate_kuadrant_authpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
20 changes: 13 additions & 7 deletions cmd/generate_kuadrant_ratelimitpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit b87543f

Please sign in to comment.