Skip to content

Commit e16d505

Browse files
json output for schema
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
1 parent 6e58d21 commit e16d505

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

internal/cmd/preview.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"os"
@@ -26,6 +27,7 @@ func registerPreviewCmd(rootCmd *cobra.Command) {
2627

2728
schemaCmd.AddCommand(schemaCompileCmd)
2829
schemaCompileCmd.Flags().String("out", "", "output filepath; omitting writes to stdout")
30+
schemaCompileCmd.Flags().Bool("json", false, "output schema as JSON")
2931
}
3032

3133
var previewCmd = &cobra.Command{
@@ -105,6 +107,21 @@ func schemaCompileCmdFunc(cmd *cobra.Command, args []string) error {
105107
// Add a newline at the end for hygiene's sake
106108
terminated := generated + "\n"
107109

110+
if cobrautil.MustGetBool(cmd, "json") {
111+
output := struct {
112+
SchemaText string `json:"schemaText"`
113+
}{
114+
SchemaText: terminated,
115+
}
116+
117+
jsonOutput, err := json.Marshal(output)
118+
if err != nil {
119+
return err
120+
}
121+
122+
terminated = string(jsonOutput)
123+
}
124+
108125
if outputFilepath == "" {
109126
// Print to stdout
110127
fmt.Print(terminated)

0 commit comments

Comments
 (0)