@@ -2,6 +2,7 @@ package cmd
22
33import (
44 "context"
5+ "encoding/json"
56 "errors"
67 "fmt"
78 "io"
@@ -105,6 +106,7 @@ func registerAdditionalSchemaCmds(schemaCmd *cobra.Command) {
105106
106107 schemaCmd .AddCommand (schemaCompileCmd )
107108 schemaCompileCmd .Flags ().String ("out" , "" , "output filepath; omitting writes to stdout" )
109+ schemaCompileCmd .Flags ().Bool ("json" , false , "output schema as JSON" )
108110}
109111
110112func schemaDiffCmdFunc (_ * cobra.Command , args []string ) error {
@@ -392,6 +394,7 @@ func determinePrefixForSchema(ctx context.Context, specifiedPrefix string, clien
392394
393395func schemaCompileOuter (cmd * cobra.Command , args []string ) (bool , error ) {
394396 outputFilepath := cobrautil .MustGetString (cmd , "out" )
397+ asJson := cobrautil .MustGetBool (cmd , "json" )
395398
396399 var outputFile * os.File
397400 var toStdout bool
@@ -413,12 +416,12 @@ func schemaCompileOuter(cmd *cobra.Command, args []string) (bool, error) {
413416 }()
414417 }
415418
416- return toStdout , schemaCompileInner (cmd .Context (), args , outputFile )
419+ return toStdout , schemaCompileInner (cmd .Context (), args , asJson , outputFile )
417420}
418421
419422// Compiles an input schema written in the new composable schema syntax
420423// and produces it as a fully-realized schema
421- func schemaCompileInner (ctx context.Context , args []string , writer io.Writer ) error {
424+ func schemaCompileInner (ctx context.Context , args []string , asJson bool , writer io.Writer ) error {
422425 inputFilepath := args [0 ]
423426 inputSourceFolder := filepath .Dir (inputFilepath )
424427 schemaBytes , err := os .ReadFile (inputFilepath )
@@ -449,6 +452,21 @@ func schemaCompileInner(ctx context.Context, args []string, writer io.Writer) er
449452 // Add a newline at the end for hygiene's sake
450453 terminated := generated + "\n "
451454
455+ if asJson {
456+ output := struct {
457+ SchemaText string `json:"schemaText"`
458+ }{
459+ SchemaText : terminated ,
460+ }
461+
462+ jsonOutput , err := json .Marshal (output )
463+ if err != nil {
464+ return err
465+ }
466+
467+ terminated = string (jsonOutput )
468+ }
469+
452470 _ , err = fmt .Fprint (writer , terminated )
453471 if err != nil {
454472 return fmt .Errorf ("failed to write schema: %w" , err )
0 commit comments