diff --git a/README.md b/README.md index cff87aa..77080d3 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Generate Gateway API resources from an OpenAPI 3.x specification | Subcommand | Description | Flags | | ---------- | ------------------------------------------------ | --------------------------------- | -| `httproute`| Generate Gateway API HTTPRoute from OpenAPI 3.0.X| `--oas string` Path or URL to OpenAPI spec (required) | +| `httproute`| Generate Gateway API HTTPRoute from OpenAPI 3.0.X| `--oas string` Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required). `-o` Output format: 'yaml' or 'json'. Default: yaml | ##### `generate kuadrant` @@ -88,8 +88,8 @@ Generate Kuadrant resources from an OpenAPI 3.x specification | Subcommand | Description | Flags | | ---------------- | ------------------------------------------------- | --------------------------------- | -| `authpolicy` | Generate a [Kuadrant AuthPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/auth/) from an OpenAPI 3.0.x specification | `--oas string` Path or URL to OpenAPI spec (required) | -| `ratelimitpolicy`| Generate [Kuadrant RateLimitPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/rate-limiting/) from an OpenAPI 3.0.x specification | `--oas string` Path or URL to OpenAPI spec (required) | +| `authpolicy` | Generate a [Kuadrant AuthPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/auth/) from an OpenAPI 3.0.x specification | `--oas string` Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required). `-o` Output format: 'yaml' or 'json'. Default: yaml | +| `ratelimitpolicy`| Generate [Kuadrant RateLimitPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/rate-limiting/) from an OpenAPI 3.0.x specification | `--oas string` Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required). `-o` Output format: 'yaml' or 'json'. Default: yaml | #### `install` diff --git a/cmd/generate_gatewayapi_httproute.go b/cmd/generate_gatewayapi_httproute.go index fea188d..d8e5ab4 100644 --- a/cmd/generate_gatewayapi_httproute.go +++ b/cmd/generate_gatewayapi_httproute.go @@ -8,12 +8,14 @@ import ( "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" ) var ( - generateGatewayAPIHTTPRouteOAS string + generateGatewayAPIHTTPRouteOAS string + generateGatewayAPIHTTPRouteFormat string ) //kuadrantctl generate gatewayapi httproute --oas [OAS_FILE_PATH | OAS_URL | @] @@ -27,7 +29,8 @@ func generateGatewayApiHttpRouteCommand() *cobra.Command { } // OpenAPI ref - cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "/path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)") + cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)") + cmd.Flags().StringVarP(&generateGatewayAPIHTTPRouteFormat, "output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml") err := cmd.MarkFlagRequired("oas") if err != nil { panic(err) @@ -55,12 +58,17 @@ func runGenerateGatewayApiHttpRoute(cmd *cobra.Command, args []string) error { httpRoute := buildHTTPRoute(doc) - jsonData, err := json.Marshal(httpRoute) + 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 } - fmt.Fprintln(cmd.OutOrStdout(), string(jsonData)) + fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes)) return nil } diff --git a/cmd/generate_kuadrant_authpolicy.go b/cmd/generate_kuadrant_authpolicy.go index 177e50a..51878c5 100644 --- a/cmd/generate_kuadrant_authpolicy.go +++ b/cmd/generate_kuadrant_authpolicy.go @@ -7,6 +7,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" 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" @@ -16,6 +17,11 @@ import ( "github.com/kuadrant/kuadrantctl/pkg/utils" ) +var ( + generateAuthPolicyOAS string + generateAuthPolicyFormat string +) + //kuadrantctl generate kuadrant authpolicy --oas [OAS_FILE_PATH | OAS_URL | @] func generateKuadrantAuthPolicyCommand() *cobra.Command { @@ -27,7 +33,8 @@ func generateKuadrantAuthPolicyCommand() *cobra.Command { } // OpenAPI ref - cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "/path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)") + cmd.Flags().StringVar(&generateAuthPolicyOAS, "oas", "", "Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)") + cmd.Flags().StringVarP(&generateAuthPolicyFormat, "output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml") err := cmd.MarkFlagRequired("oas") if err != nil { panic(err) @@ -37,7 +44,7 @@ func generateKuadrantAuthPolicyCommand() *cobra.Command { } func runGenerateKuadrantAuthPolicy(cmd *cobra.Command, args []string) error { - oasDataRaw, err := utils.ReadExternalResource(generateGatewayAPIHTTPRouteOAS) + oasDataRaw, err := utils.ReadExternalResource(generateAuthPolicyOAS) if err != nil { return err } @@ -55,12 +62,17 @@ func runGenerateKuadrantAuthPolicy(cmd *cobra.Command, args []string) error { ap := buildAuthPolicy(doc) - jsonData, err := json.Marshal(ap) + 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 } - fmt.Fprintln(cmd.OutOrStdout(), string(jsonData)) + fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes)) return nil } diff --git a/cmd/generate_kuadrant_ratelimitpolicy.go b/cmd/generate_kuadrant_ratelimitpolicy.go index 9115607..713e3dd 100644 --- a/cmd/generate_kuadrant_ratelimitpolicy.go +++ b/cmd/generate_kuadrant_ratelimitpolicy.go @@ -3,10 +3,12 @@ package cmd import ( "encoding/json" "fmt" + "os" "github.com/getkin/kin-openapi/openapi3" 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" @@ -18,26 +20,32 @@ import ( //kuadrantctl generate kuadrant ratelimitpolicy --oas [OAS_FILE_PATH | OAS_URL | @] +var ( + generateRateLimitPolicyOAS string + generateRateLimitPolicyFormat string +) + func generateKuadrantRateLimitPolicyCommand() *cobra.Command { cmd := &cobra.Command{ Use: "ratelimitpolicy", - Short: "Generate Kuadrant RateLimitPolicy from OpenAPI 3.0.X", - Long: "Generate Kuadrant RateLimitPolicy from OpenAPI 3.0.X", + Short: "Generate Kuadrant Rate Limit Policy from OpenAPI 3.0.X", + Long: "Generate Kuadrant Rate Limit Policy from OpenAPI 3.0.X", RunE: runGenerateKuadrantRateLimitPolicy, } - // OpenAPI ref - cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "/path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)") - err := cmd.MarkFlagRequired("oas") - if err != nil { - panic(err) + cmd.Flags().StringVar(&generateRateLimitPolicyOAS, "oas", "", "Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)") + cmd.Flags().StringVarP(&generateRateLimitPolicyFormat, "output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml") + + if err := cmd.MarkFlagRequired("oas"); err != nil { + fmt.Println("Error setting 'oas' flag as required:", err) + os.Exit(1) } return cmd } func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, args []string) error { - oasDataRaw, err := utils.ReadExternalResource(generateGatewayAPIHTTPRouteOAS) + oasDataRaw, err := utils.ReadExternalResource(generateRateLimitPolicyOAS) if err != nil { return err } @@ -55,12 +63,17 @@ func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, args []string) error rlp := buildRateLimitPolicy(doc) - jsonData, err := json.Marshal(rlp) + var outputBytes []byte + if generateRateLimitPolicyFormat == "json" { + outputBytes, err = json.Marshal(rlp) + } else { // default to YAML if not explicitly JSON + outputBytes, err = yaml.Marshal(rlp) + } if err != nil { return err } - fmt.Fprintln(cmd.OutOrStdout(), string(jsonData)) + fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes)) return nil } diff --git a/doc/generate-gateway-api-httproute.md b/doc/generate-gateway-api-httproute.md index 931d3d8..931590d 100644 --- a/doc/generate-gateway-api-httproute.md +++ b/doc/generate-gateway-api-httproute.md @@ -21,8 +21,9 @@ Usage: kuadrantctl generate gatewayapi httproute [flags] Flags: - -h, --help help for httproute - --oas string /path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required) + -h, --help help for httproute + --oas string Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required) + -o Output format: 'yaml' or 'json'. Default: yaml Global Flags: -v, --verbose verbose output diff --git a/doc/generate-kuadrant-auth-policy.md b/doc/generate-kuadrant-auth-policy.md index 6294291..71a1b7c 100644 --- a/doc/generate-kuadrant-auth-policy.md +++ b/doc/generate-kuadrant-auth-policy.md @@ -172,7 +172,8 @@ Usage: Flags: -h, --help help for authpolicy - --oas string /path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required) + --oas string Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required) + -o Output format: 'yaml' or 'json'. Default: yaml Global Flags: -v, --verbose verbose output diff --git a/doc/generate-kuadrant-rate-limit-policy.md b/doc/generate-kuadrant-rate-limit-policy.md index ad4b3d6..cbf185f 100644 --- a/doc/generate-kuadrant-rate-limit-policy.md +++ b/doc/generate-kuadrant-rate-limit-policy.md @@ -21,7 +21,8 @@ Usage: Flags: -h, --help help for ratelimitpolicy - --oas string /path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required) + --oas string Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required) + -o Output format: 'yaml' or 'json'. Default: yaml Global Flags: -v, --verbose verbose output diff --git a/pkg/utils/external_resource_reader.go b/pkg/utils/external_resource_reader.go index 43dd6bf..db37ac1 100644 --- a/pkg/utils/external_resource_reader.go +++ b/pkg/utils/external_resource_reader.go @@ -21,11 +21,11 @@ import ( ) // ReadExternalResource reads data streams from external resources. Currently implemented: -// - '@' for STDIN +// - '-' or '@' for STDIN // - URLs (HTTP[S]) // - Files func ReadExternalResource(resource string) ([]byte, error) { - if resource == "@" { + if resource == "-" || resource == "@" { return io.ReadAll(os.Stdin) }