-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
326 lines (271 loc) · 7.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"os/exec"
"os/signal"
"path"
"syscall"
"github.com/stephenafamo/bob/gen"
helpers "github.com/stephenafamo/bob/gen/bobgen-helpers"
"github.com/stephenafamo/bob/gen/bobgen-prisma/driver"
)
//nolint:gochecknoglobals
var (
writeDebugFile = os.Getenv("PRISMA_CLIENT_GO_WRITE_DMMF_FILE") != ""
prismaCLIPath = os.Getenv("PRISMA_CLI_PATH")
)
func main() {
if os.Getenv("PRISMA_GENERATOR_INVOCATION") == "" {
// prisma CLI
if err := callPrisma(); err != nil {
panic(err)
}
return
}
// exit when signal triggers
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(1)
}()
if err := servePrisma(); err != nil {
log.Fatalf("error occurred when invoking prisma: %s", err)
}
}
func reply(w io.Writer, data any) error {
b, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("could not marshal data %w", err)
}
b = append(b, byte('\n'))
if _, err = w.Write(b); err != nil {
return fmt.Errorf("could not write data %w", err)
}
return nil
}
func servePrisma() error {
reader := bufio.NewReader(os.Stdin)
if writeDebugFile {
dir, _ := os.Getwd()
log.Printf("current working dir: %s", dir)
}
for {
content, err := reader.ReadBytes('\n')
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return fmt.Errorf("could not read bytes from stdin: %w", err)
}
if writeDebugFile {
buf := &bytes.Buffer{}
if err := json.Indent(buf, content, "", " "); err != nil {
log.Print(err)
}
if err := os.WriteFile("dmmf.json", buf.Bytes(), 0o600); err != nil {
log.Print(err)
}
}
var input Request
if err := json.Unmarshal(content, &input); err != nil {
return fmt.Errorf("could not open stdin %w", err)
}
var response any
switch input.Method {
case "getManifest":
response = ManifestResponse{
Manifest: Manifest{
DefaultOutput: path.Join(".", "db"),
PrettyName: "Bob Prisma Go Client",
},
}
case "generate":
response = nil // success
var params root
if err := json.Unmarshal(input.Params, ¶ms); err != nil {
dir, _ := os.Getwd()
return fmt.Errorf("could not unmarshal params into generator.Root type at %s: %w", dir, err)
}
if err := generate(params); err != nil {
return fmt.Errorf("could not generate code. %w", err)
}
default:
return fmt.Errorf("no such method %s", input.Method)
}
if err := reply(os.Stderr, NewResponse(input.ID, response)); err != nil {
return fmt.Errorf("could not reply %w", err)
}
}
}
func generate(root root) error {
var err error
var dialect, driverName, driverPkg string
modelTemplates := []fs.FS{gen.PrismaModelTemplates}
datasource := root.Datasources[0]
switch datasource.Provider {
case ProviderPostgreSQL:
dialect = "psql"
driverName = "pgx"
driverPkg = "github.com/jackc/pgx/v5/stdlib"
case ProviderSQLite:
dialect = "sqlite"
driverName = "sqlite"
driverPkg = "modernc.org/sqlite"
modelTemplates = append(modelTemplates, gen.SQLiteModelTemplates)
case ProviderMySQL:
dialect = "mysql"
driverName = "mysql"
driverPkg = "github.com/go-sql-driver/mysql"
modelTemplates = append(modelTemplates, gen.MySQLModelTemplates)
default:
return fmt.Errorf("Unsupported datasource provider %q", datasource.Provider)
}
configPath := root.Generator.Config.ConfigFile
if configPath == "" {
configPath = helpers.DefaultConfigPath
}
config, driverConfig, err := helpers.GetConfigFromFile[driver.Config](configPath, "prisma")
if err != nil {
return err
}
if driverConfig.Pkgname == "" {
driverConfig.Pkgname = "prisma"
}
outputDir := root.Generator.Output.Value
if outputDir == "" {
return fmt.Errorf("no output folder configured")
}
d := driver.New(
driverConfig,
dialect,
driver.Provider{
DriverName: driverName,
DriverPkg: driverPkg,
DriverSource: datasource.URL.Value,
DriverENVSource: datasource.URL.FromEnvVar,
},
root.DMMF.Datamodel,
)
outputs := helpers.DefaultOutputs(
outputDir, driverConfig.Pkgname, config.NoFactory,
&helpers.Templates{Models: modelTemplates},
)
state := &gen.State{
Config: config,
Outputs: outputs,
}
relAliasPlugin := relAliasPlugin{
models: root.DMMF.Datamodel.Models,
aliases: config.Aliases,
config: driverConfig,
}
if err := gen.Run(context.Background(), state, d, relAliasPlugin); err != nil {
fmt.Println(err) // makes the error print better
return err
}
return nil
}
// Root describes the generator output root.
// overwritten so I can set the config
type root struct {
Generator Generator `json:"generator"`
Datasources []Datasource `json:"datasources"`
DMMF driver.Document `json:"DMMF"`
SchemaPath string `json:"schemaPath"`
}
type config struct {
ConfigFile string `json:"configFile"`
}
// callPrisma the prisma CLI with given arguments
func callPrisma() error {
if prismaCLIPath == "" {
prismaCLIPath = "prisma"
}
cmd := exec.Command(prismaCLIPath, "generate")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "PRISMA_HIDE_UPDATE_MESSAGE=true")
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("could not run %q: %w", "generate", err)
}
return nil
}
// Request sets a generic JSONRPC request, which wraps information and params.
type Request struct {
// JSONRPC describes the version of the JSON RPC protocol. Defaults to `2.0`.
JSONRPC string `json:"jsonrpc"`
// ID identifies a unique request.
ID int `json:"id"`
// Method describes the intention of the request.
Method string `json:"method"`
// Params contains the payload of the request. Usually parsed into a specific struct for further processing.
Params json.RawMessage `json:"params"`
}
// Response sets a generic JSONRPC response, which wraps information and a result.
type Response struct {
// JSONRPC describes the version of the JSON RPC protocol. Defaults to `2.0`.
JSONRPC string `json:"jsonrpc"`
// ID identifies a unique request.
ID int `json:"id"`
// Result contains the payload of the response.
Result any `json:"result"`
}
// NewResponse forms a new JSON RPC response to reply to the Prisma CLI commands
func NewResponse(id int, result any) Response {
return Response{
JSONRPC: "2.0",
ID: id,
Result: result,
}
}
// Manifest describes information for the Prisma Client Go generator for the Prisma CLI.
type Manifest struct {
PrettyName string `json:"prettyName"`
DefaultOutput string `json:"defaultOutput"`
Denylist []string `json:"denylist"`
RequiresGenerators []string `json:"requiresGenerators"`
RequiresEngines []string `json:"requiresEngines"`
}
// ManifestResponse sets the response Prisma Client Go returns when Prisma asks for the Manifest.
type ManifestResponse struct {
Manifest Manifest `json:"manifest"`
}
type Generator struct {
Name string `json:"name"`
Output Value `json:"output"`
Provider Value `json:"provider"`
Config config `json:"config"`
}
// Datasource describes a Prisma data source of any database type.
type Datasource struct {
Name string `json:"name"`
Provider Provider `json:"provider"`
URL Value `json:"url"`
}
type Value struct {
FromEnvVar string `json:"fromEnvVar"`
Value string `json:"value"`
}
// Provider describes the Database of this generator.
type Provider string
// Provider values
const (
ProviderMySQL Provider = "mysql"
ProviderMongo Provider = "mongo"
ProviderSQLite Provider = "sqlite"
ProviderSQLServer Provider = "sqlserver"
ProviderPostgreSQL Provider = "postgresql"
ProviderCockroachDB Provider = "cockroachdb"
)