From e16d505859f224c6b6c56f827ef48f4b28e4d39c Mon Sep 17 00:00:00 2001 From: Kartikay Date: Sun, 23 Feb 2025 16:16:54 +0530 Subject: [PATCH] json output for schema Signed-off-by: Kartikay --- internal/cmd/preview.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/cmd/preview.go b/internal/cmd/preview.go index 90367d3..ee5afd8 100644 --- a/internal/cmd/preview.go +++ b/internal/cmd/preview.go @@ -1,6 +1,7 @@ package cmd import ( + "encoding/json" "errors" "fmt" "os" @@ -26,6 +27,7 @@ func registerPreviewCmd(rootCmd *cobra.Command) { schemaCmd.AddCommand(schemaCompileCmd) schemaCompileCmd.Flags().String("out", "", "output filepath; omitting writes to stdout") + schemaCompileCmd.Flags().Bool("json", false, "output schema as JSON") } var previewCmd = &cobra.Command{ @@ -105,6 +107,21 @@ func schemaCompileCmdFunc(cmd *cobra.Command, args []string) error { // Add a newline at the end for hygiene's sake terminated := generated + "\n" + if cobrautil.MustGetBool(cmd, "json") { + output := struct { + SchemaText string `json:"schemaText"` + }{ + SchemaText: terminated, + } + + jsonOutput, err := json.Marshal(output) + if err != nil { + return err + } + + terminated = string(jsonOutput) + } + if outputFilepath == "" { // Print to stdout fmt.Print(terminated)