MCPcopy
hub / github.com/sqlc-dev/sqlc / remoteGenerate

Function remoteGenerate

internal/cmd/generate.go:233–294  ·  view source on GitHub ↗
(ctx context.Context, configPath string, conf *config.Config, dir string, stderr io.Writer)

Source from the content-addressed store, hash-verified

231}
232
233func remoteGenerate(ctx context.Context, configPath string, conf *config.Config, dir string, stderr io.Writer) (map[string]string, error) {
234 rpcClient, err := remote.NewClient(conf.Cloud)
235 if err != nil {
236 fmt.Fprintf(stderr, "error creating rpc client: %s\n", err)
237 return nil, err
238 }
239
240 configBytes, err := os.ReadFile(configPath)
241 if err != nil {
242 fmt.Fprintf(stderr, "error reading config file %s: %s\n", configPath, err)
243 return nil, err
244 }
245
246 rpcReq := remote.GenerateRequest{
247 Version: info.Version,
248 Inputs: []*remote.File{{Path: filepath.Base(configPath), Bytes: configBytes}},
249 }
250
251 for _, pkg := range conf.SQL {
252 for _, paths := range []config.Paths{pkg.Schema, pkg.Queries} {
253 for i, relFilePath := range paths {
254 paths[i] = filepath.Join(dir, relFilePath)
255 }
256 files, err := sqlpath.Glob(paths)
257 if err != nil {
258 fmt.Fprintf(stderr, "error globbing paths: %s\n", err)
259 return nil, err
260 }
261 for _, filePath := range files {
262 fileBytes, err := os.ReadFile(filePath)
263 if err != nil {
264 fmt.Fprintf(stderr, "error reading file %s: %s\n", filePath, err)
265 return nil, err
266 }
267 fileRelPath, _ := filepath.Rel(dir, filePath)
268 rpcReq.Inputs = append(rpcReq.Inputs, &remote.File{Path: fileRelPath, Bytes: fileBytes})
269 }
270 }
271 }
272
273 rpcResp, err := rpcClient.Generate(ctx, &rpcReq)
274 if err != nil {
275 rpcStatus, ok := status.FromError(err)
276 if !ok {
277 return nil, err
278 }
279 fmt.Fprintf(stderr, "rpc error: %s", rpcStatus.Message())
280 return nil, rpcStatus.Err()
281 }
282
283 if rpcResp.ExitCode != 0 {
284 fmt.Fprintf(stderr, "%s", rpcResp.Stderr)
285 return nil, errors.New("remote execution returned with non-zero exit code")
286 }
287
288 output := map[string]string{}
289 for _, file := range rpcResp.Outputs {
290 output[filepath.Join(dir, file.Path)] = string(file.Bytes)

Callers 1

GenerateFunction · 0.85

Calls 5

NewClientFunction · 0.92
GlobFunction · 0.92
GenerateMethod · 0.65
JoinMethod · 0.45
ErrMethod · 0.45

Tested by

no test coverage detected