-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
70 lines (62 loc) · 1.55 KB
/
gen.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
package client
import (
"github.com/juxuny/yc/cmd"
"github.com/juxuny/yc/utils"
"github.com/spf13/cobra"
"io/fs"
"log"
"os"
"path"
"path/filepath"
)
type GenCommand struct {
Go string
ReactTs string
WorkDir string
Internal bool
}
func (t *GenCommand) Prepare(cmd *cobra.Command) {
}
func (t *GenCommand) InitFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&t.Go, "go", "", "go sdk output directory")
cmd.PersistentFlags().StringVar(&t.ReactTs, "react-ts", "", "react ts sdk output directory")
cmd.PersistentFlags().StringVarP(&t.WorkDir, "work-dir", "w", ".", "working directory")
cmd.PersistentFlags().BoolVar(&t.Internal, "internal", false, "auto gen internal RPC method")
}
func (t *GenCommand) BeforeRun(cmd *cobra.Command) {
if t.Go == "" && t.ReactTs == "" {
log.Fatal("missing args: --go, --react-ts")
}
}
func (t *GenCommand) Run() {
if t.Go != "" {
t.genGo()
}
if t.ReactTs != "" {
t.genReactTs()
}
}
func (t *GenCommand) getServiceName() string {
serviceName := ""
if _, err := os.Stat(t.WorkDir); os.IsNotExist(err) {
log.Fatal(err)
}
if err := filepath.Walk(t.WorkDir, func(filePath string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if path.Ext(filePath) == ".proto" {
serviceName = path.Base(filePath)
return nil
}
return nil
}); err != nil {
log.Fatal(err)
}
return utils.StringHelper.TrimSubSequenceRight(serviceName, ".proto")
}
func init() {
genCommand := &GenCommand{}
builder := cmd.NewCommandBuilder("gen", genCommand)
Command.AddCommand(builder.Build())
}