Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cmd/preview.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cmd

import (
Expand All @@ -11,4 +11,4 @@
}

rootCmd.AddCommand(previewCmd)
}
}
22 changes: 20 additions & 2 deletions internal/cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -105,6 +106,7 @@ func registerAdditionalSchemaCmds(schemaCmd *cobra.Command) {

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

func schemaDiffCmdFunc(_ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -392,6 +394,7 @@ func determinePrefixForSchema(ctx context.Context, specifiedPrefix string, clien

func schemaCompileOuter(cmd *cobra.Command, args []string) (bool, error) {
outputFilepath := cobrautil.MustGetString(cmd, "out")
asJson := cobrautil.MustGetBool(cmd, "json")

var outputFile *os.File
var toStdout bool
Expand All @@ -413,12 +416,12 @@ func schemaCompileOuter(cmd *cobra.Command, args []string) (bool, error) {
}()
}

return toStdout, schemaCompileInner(cmd.Context(), args, outputFile)
return toStdout, schemaCompileInner(cmd.Context(), args, asJson, outputFile)
}

// Compiles an input schema written in the new composable schema syntax
// and produces it as a fully-realized schema
func schemaCompileInner(ctx context.Context, args []string, writer io.Writer) error {
func schemaCompileInner(ctx context.Context, args []string, asJson bool, writer io.Writer) error {
inputFilepath := args[0]
inputSourceFolder := filepath.Dir(inputFilepath)
schemaBytes, err := os.ReadFile(inputFilepath)
Expand Down Expand Up @@ -449,6 +452,21 @@ func schemaCompileInner(ctx context.Context, args []string, writer io.Writer) er
// Add a newline at the end for hygiene's sake
terminated := generated + "\n"

if asJson {
output := struct {
SchemaText string `json:"schemaText"`
}{
SchemaText: terminated,
}

jsonOutput, err := json.Marshal(output)
if err != nil {
return err
}

terminated = string(jsonOutput)
}

_, err = fmt.Fprint(writer, terminated)
if err != nil {
return fmt.Errorf("failed to write schema: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cmd

//go:generate go run go.uber.org/mock/mockgen -destination=mock_schema_client_test.go -package=cmd github.com/authzed/authzed-go/proto/authzed/api/v1 SchemaServiceClient
Expand Down Expand Up @@ -201,7 +201,7 @@
var buf []byte
writer := &testWriter{buffer: &buf}

err := schemaCompileInner(t.Context(), files, writer)
err := schemaCompileInner(t.Context(), files, false, writer)

require.NoError(err)
require.Equal(expected, string(buf))
Expand All @@ -213,7 +213,7 @@

files := []string{filepath.Join("preview-test", "nonexistent.zed")}

err := schemaCompileInner(t.Context(), files, io.Discard)
err := schemaCompileInner(t.Context(), files, false, io.Discard)
require.Error(err)
require.ErrorIs(err, fs.ErrNotExist)
}
Expand All @@ -225,7 +225,7 @@
files := []string{filepath.Join("preview-test", "composable-schema-invalid-root.zed")}
var expectedErr compiler.BaseCompilerError

err := schemaCompileInner(t.Context(), files, io.Discard)
err := schemaCompileInner(t.Context(), files, false, io.Discard)
require.Error(err)
require.ErrorAs(err, &expectedErr)
}
Expand All @@ -236,7 +236,7 @@

files := []string{filepath.Join("preview-test", "composable-schema-root.zed")}

err := schemaCompileInner(t.Context(), files, &failingWriter{err: errors.New("simulated write failure")})
err := schemaCompileInner(t.Context(), files,false, &failingWriter{err: errors.New("simulated write failure")})

require.Error(err)
require.ErrorContains(err, "failed to write schema")
Expand All @@ -253,7 +253,7 @@

files := []string{emptySchemaFile}

err = schemaCompileInner(t.Context(), files, io.Discard)
err = schemaCompileInner(t.Context(), files, false, io.Discard)

require.Error(err)
require.ErrorContains(err, "attempted to compile empty schema")
Expand Down
Loading