-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.go
65 lines (55 loc) · 1.27 KB
/
codegen.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
package codegen
import (
"context"
"io/fs"
"os"
"path/filepath"
"github.com/crdflow/crdflow/pkg/util"
)
type GenerateServerOptions struct {
RepoName string
ProtoPath string
OutputPath string
ProtoFile string
}
// GenerateServer gRPC server.
func GenerateServer(ctx context.Context, opts GenerateServerOptions) error {
outputGen := filepath.Join(opts.OutputPath, "gen")
if err := os.MkdirAll(outputGen, fs.ModePerm); err != nil {
return err
}
if err := generateProto(ctx, opts.ProtoPath, outputGen, opts.ProtoFile); err != nil {
return err
}
if err := goModInit(ctx, opts.OutputPath, opts.RepoName); err != nil {
return err
}
return goModTidy(ctx, opts.OutputPath)
}
func generateProto(ctx context.Context, protoPath, outputGen, protoFile string) error {
return util.Exec(
ctx,
"",
"protoc",
"--proto_path", protoPath,
"--go_out", outputGen,
"--go_opt=paths=source_relative",
"--go-grpc_out", outputGen,
"--go-grpc_opt=paths=source_relative",
protoFile,
)
}
func goModInit(ctx context.Context, outputPath, repoName string) error {
return util.Exec(
ctx,
outputPath+"/",
"go", "mod", "init", repoName,
)
}
func goModTidy(ctx context.Context, outputPath string) error {
return util.Exec(
ctx,
outputPath+"/",
"go", "mod", "tidy",
)
}