-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
131 lines (120 loc) · 3.76 KB
/
grpc.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
package proto
import (
"context"
"embed"
"fmt"
"os"
"time"
basev0 "github.com/codefly-dev/core/generated/go/base/v0"
"github.com/codefly-dev/core/languages"
"github.com/codefly-dev/core/resources"
runners "github.com/codefly-dev/core/runners/base"
"github.com/codefly-dev/core/templates"
"github.com/codefly-dev/core/wool"
)
func GenerateGRPC(ctx context.Context, language languages.Language, destination string, service string, endpoints ...*basev0.Endpoint) error {
w := wool.Get(ctx).In("generateGRPC", wool.Field("destination", destination))
// call the companion
image, err := CompanionImage(ctx)
if err != nil {
return w.Wrapf(err, "cannot get companion image")
}
// Create a tmp dir for the proto
tmpDir, err := os.MkdirTemp("", "proto")
if err != nil {
return w.Wrapf(err, "cannot create tmp dir")
}
defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
w.Error("cannot remove tmp dir", wool.Field("path", path))
}
}(tmpDir)
w.Debug("generating code from buf", wool.DirField(tmpDir), wool.DirField(destination))
// Buf configuration
err = CreateBufConfiguration(ctx, tmpDir, service, language)
if err != nil {
return w.Wrapf(err, "cannot create buf configuration")
}
// Add the proto stuff
for _, endpoint := range endpoints {
if grpc := resources.IsGRPC(ctx, endpoint); grpc != nil {
unique := fmt.Sprintf("%s_%s", endpoint.Module, endpoint.Service)
err = os.WriteFile(fmt.Sprintf("%s/%s_%s.proto", tmpDir, unique, endpoint.Name), grpc.Proto, 0600)
if err != nil {
return w.Wrapf(err, "cannot write proto file")
}
}
}
name := fmt.Sprintf("proto-%s-%d", service, time.Now().UnixMilli())
runner, err := runners.NewDockerEnvironment(ctx, image, tmpDir, name)
if err != nil {
return w.Wrapf(err, "cannot create docker runner")
}
runner.WithMount(destination, "/workspace/output")
runner.WithMount(tmpDir, "/workspace")
runner.WithWorkDir("/workspace")
runner.WithPause()
defer func() {
err = runner.Shutdown(ctx)
if err != nil {
w.Warn("cannot shutdown runner", wool.ErrField(err))
}
}()
err = runner.Init(ctx)
if err != nil {
return w.Wrapf(err, "cannot init runner")
}
proc, err := runner.NewProcess("buf", "mod", "update")
if err != nil {
return w.Wrapf(err, "cannot create process")
}
err = proc.Run(ctx)
if err != nil {
return w.Wrapf(err, "cannot update buf")
}
proc, err = runner.NewProcess("buf", "generate")
if err != nil {
return w.Wrapf(err, "cannot create process")
}
err = proc.Run(ctx)
if err != nil {
return w.Wrapf(err, "cannot generate with buf")
}
return nil
}
type GoConfiguration struct {
Destination string
GoPackagePrefix string
}
func CreateBufConfiguration(ctx context.Context, bufDir string, service string, language languages.Language) error {
w := wool.Get(ctx).In("createBufConfiguration")
switch language {
case languages.GO:
// Templatize
err := templateGoConfiguration(ctx, bufDir, fmt.Sprintf("github.com/codefly-dev/cli/pkg/builder/clients/%s", service))
if err != nil {
return w.Wrapf(err, "cannot templatize")
}
return nil
case languages.NotSupported:
return w.NewError("language not supported")
}
return w.NewError("unknown language")
}
func templateGoConfiguration(ctx context.Context, bufDir string, goPackagePrefix string) error {
w := wool.Get(ctx).In("templateGoConfiguration", wool.Field("bufDir", bufDir), wool.Field("goPackagePrefix", goPackagePrefix))
templator := &templates.Templator{NameReplacer: templates.CutTemplateSuffix{}}
conf := GoConfiguration{
Destination: "output",
GoPackagePrefix: goPackagePrefix,
}
err := templator.CopyAndApply(ctx, goFS, "templates/go", bufDir, conf)
if err != nil {
return w.Wrapf(err, "cannot copy and apply template")
}
return nil
}
// Embed
//go:embed templates/go
var goFS embed.FS