|
| 1 | +// Copyright 2025 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package cli |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "io" |
| 16 | + "os" |
| 17 | + "strings" |
| 18 | + |
| 19 | + "github.com/spf13/cobra" |
| 20 | + |
| 21 | + "github.com/sourcenetwork/defradb/client" |
| 22 | + "github.com/sourcenetwork/defradb/errors" |
| 23 | + "github.com/sourcenetwork/defradb/internal/db/description" |
| 24 | + "github.com/sourcenetwork/defradb/internal/request/graphql/schema" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + defaultOutputPath = "schema.gen.graphql" |
| 29 | + fileLineSeperator = "\n\n" |
| 30 | +) |
| 31 | + |
| 32 | +func MakeSDLGenerateCommand(ctx context.Context) *cobra.Command { |
| 33 | + var outputFile string |
| 34 | + var yesOverwrite bool |
| 35 | + var searchableEncryption bool |
| 36 | + var cmd = &cobra.Command{ |
| 37 | + Use: "generate --output schema.graphql <input schema files...>", |
| 38 | + Short: "Generate full GraphQL formatted schema.", |
| 39 | + Long: `Generates the fully formatted GraphQL schema from a given user type definition(s). |
| 40 | +
|
| 41 | + Accepts multiple input files as well as "-" to use stdin. |
| 42 | + `, |
| 43 | + Args: cobra.MinimumNArgs(1), |
| 44 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 45 | + var sdlBuf string |
| 46 | + |
| 47 | + // Either we use stdin or we concat all the file |
| 48 | + // arguments |
| 49 | + if len(args) == 1 && args[0] == "-" { |
| 50 | + sdlByteBuf, err := io.ReadAll(cmd.InOrStdin()) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + sdlBuf = string(sdlByteBuf) |
| 55 | + } else { |
| 56 | + var fileInputBuf strings.Builder |
| 57 | + for i, arg := range args { |
| 58 | + if arg == "-" { |
| 59 | + return ErrStdinSingleInputOnly |
| 60 | + } |
| 61 | + fileBuf, err := os.ReadFile(arg) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + if i != 0 { |
| 67 | + fileInputBuf.WriteString(fileLineSeperator) |
| 68 | + } |
| 69 | + fileInputBuf.Write(fileBuf) |
| 70 | + } |
| 71 | + sdlBuf = fileInputBuf.String() |
| 72 | + } |
| 73 | + |
| 74 | + var outWriter io.Writer |
| 75 | + if outputFile == "-" { |
| 76 | + outWriter = cmd.OutOrStdout() |
| 77 | + } else { |
| 78 | + // check if the file exists, if so check for the overwrite |
| 79 | + // flag |
| 80 | + ofinfo, err := os.Stat(outputFile) |
| 81 | + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 82 | + return err |
| 83 | + } |
| 84 | + if ofinfo != nil && !yesOverwrite { |
| 85 | + return errors.New("output file path already exists. If you want to overwrite use -y") |
| 86 | + } |
| 87 | + |
| 88 | + f, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + defer f.Close() //nolint:errcheck |
| 93 | + outWriter = f |
| 94 | + } |
| 95 | + |
| 96 | + schemaManager, err := schema.NewSchemaManager(searchableEncryption) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + cols, err := schemaManager.ParseSDL(sdlBuf) |
| 102 | + if err != nil { |
| 103 | + return errors.Join(ErrParsingSDL, err) |
| 104 | + } |
| 105 | + |
| 106 | + collections := make([]client.CollectionVersion, len(cols)) |
| 107 | + for i, c := range cols { |
| 108 | + collections[i] = c.Definition |
| 109 | + } |
| 110 | + |
| 111 | + cache := description.NewCollectionCache() |
| 112 | + cache.AddAll(collections) |
| 113 | + ctx := description.ContextWithCollectionCache(ctx, cache) |
| 114 | + |
| 115 | + _, err = schemaManager.Generator.Generate(ctx, collections) |
| 116 | + if err != nil { |
| 117 | + return errors.Join(ErrGeneratingSDL, err) |
| 118 | + } |
| 119 | + |
| 120 | + return schemaManager.WriteSDL(outWriter) |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + EmbedCLIExample(ctx, cmd, "Generate SDL", |
| 125 | + `defradb sdl generate foo.graphql`) |
| 126 | + |
| 127 | + EmbedCLIExample(ctx, cmd, "Generate Multiple SDLs", |
| 128 | + `defradb sdl generate foo.graphql bar.graphql`) |
| 129 | + |
| 130 | + EmbedCLIExample(ctx, cmd, "Generate SDL and overwrite output", |
| 131 | + `defradb sdl generate foo.graphql bar.graphql --output schema.graphql -y`) |
| 132 | + |
| 133 | + cmd.PersistentFlags().StringVarP(&outputFile, "output", "o", defaultOutputPath, |
| 134 | + "The output file to write the generated schema. Accepts '-' to write to stdout") |
| 135 | + |
| 136 | + EmbedCLIExample(ctx, cmd, "Generate SDL with Searchable Encryption type definitions", |
| 137 | + `defradb sdl generate foo.graphql -s`) |
| 138 | + |
| 139 | + cmd.PersistentFlags().BoolVarP(&yesOverwrite, "overwrite", "y", false, |
| 140 | + "Overwrite any existing matching output file paths") |
| 141 | + |
| 142 | + cmd.PersistentFlags().BoolVarP(&searchableEncryption, "include-searchable-encryption", "s", |
| 143 | + false, "Include the schema type definitions to support Searchable Encryption") |
| 144 | + |
| 145 | + return cmd |
| 146 | +} |
0 commit comments