This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
export-proto.go
80 lines (72 loc) · 1.99 KB
/
export-proto.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
package cmd
import (
"context"
"fmt"
"github.com/gobuffalo/genny/v2"
"github.com/ronaksoft/rony/config"
"github.com/ronaksoft/rony/log"
"github.com/spf13/cobra"
"os"
"os/exec"
"path/filepath"
)
var ExportProtoCmd = &cobra.Command{
Use: "export-proto",
RunE: func(cmd *cobra.Command, args []string) error {
r := genny.WetRunner(context.Background())
if config.GetBool("dry-run") {
r = genny.DryRunner(context.Background())
}
r.Logger = log.DefaultLogger.Sugared()
if len(args) == 0 {
args = append(args, "rpc", "model")
}
g := genny.New()
collectProto(g, args)
// Create a Runner with the Generator customized by command's arguments
err := r.With(g)
if err != nil {
return err
}
return r.Run()
},
}
func collectProto(g *genny.Generator, folders []string) {
var (
files []string
projectPathAbs, _ = filepath.Abs(".")
dstTempFolder = "_exported-proto"
)
f, _ := Skeleton.Open("skel/proto/msg.proto")
g.File(genny.NewFile(filepath.Join(dstTempFolder, "msg.proto"), f))
fmt.Println("run in: ", projectPathAbs)
for _, folder := range folders {
files = files[:0]
_ = filepath.Walk(filepath.Join(".", folder), func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) == ".proto" {
files = append(files, path)
cmd0 := exec.Command("mkdir", "-p", filepath.Join(dstTempFolder, filepath.Dir(path)))
cmd0.Dir = projectPathAbs
g.Command(cmd0)
cmd1 := exec.Command("cp", path, filepath.Join(dstTempFolder, filepath.Dir(path)))
cmd1.Dir = projectPathAbs
g.Command(cmd1)
}
return nil
})
}
cmd3 := exec.Command("tar", "-czf", fmt.Sprintf("proto.tar.gz"), dstTempFolder)
cmd3.Env = os.Environ()
cmd3.Dir = projectPathAbs
cmd3.Stderr = os.Stderr
g.Command(cmd3)
fmt.Println(cmd3.String())
cmd4 := exec.Command("rm", "-r", dstTempFolder)
cmd4.Dir = projectPathAbs
cmd4.Stderr = os.Stderr
g.Command(cmd4)
fmt.Println(cmd4.String())
}