-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
126 lines (103 loc) · 3.39 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
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
package protocli
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"github.com/yoozoo/protocli/generator/data"
"github.com/yoozoo/protocli/util"
"github.com/spf13/cobra"
)
const (
defaultProtocCmd = "protoc"
protocFlag = "protoc"
langFlag = "lang"
protoPathFlag = "proto_path"
protoCustomParamFlag = "custom_params"
)
type genFlagData struct {
langValue string
protocPath string
protoIncPath string
protoCustomParam string
}
func (g *genFlagData) reset() {
g.langValue = ""
g.protocPath = ""
g.protoIncPath = ""
g.protoCustomParam = ""
}
var KeepDefaultLangOut bool
var genFlagValue genFlagData
// genCmd represents the gen command
var genCmd = &cobra.Command{
Use: "gen <output dir> <proto file>",
Short: "generate code from proto file",
Long: `This command will read the input proto file and generate code of the requested language to the output directory.`,
Args: cobra.ExactArgs(2),
Run: generateCode,
}
func generateCode(cmd *cobra.Command, args []string) {
defer func() {
genFlagValue.reset()
}()
executable, _ := os.Executable()
var params = make(map[string]string)
params[langFlag] = genFlagValue.langValue
if !data.HasCodeGenerator(genFlagValue.langValue) {
err := fmt.Errorf("Output plugin not found for %s\nsupported options: %v",
genFlagValue.langValue, reflect.ValueOf(data.GetCodeGeneratorMap()).MapKeys())
util.Die(err)
}
protoc := genFlagValue.protocPath
if len(protoc) == 0 {
protoc, genFlagValue.protoIncPath = util.GetDefaultProtoc(genFlagValue.protoIncPath)
}
protoc = filepath.FromSlash(protoc)
cmdParam := ""
for name, value := range params {
if len(value) > 0 {
if len(cmdParam) > 0 {
cmdParam += ","
}
cmdParam += name + "=" + value
}
}
if len(genFlagValue.protoCustomParam) > 0 {
cmdParam += "," + genFlagValue.protoCustomParam
}
protoFile := filepath.FromSlash(args[1])
stat, err := os.Stat(protoFile)
if err != nil {
util.Die(fmt.Errorf("Input %s is not accessible : %s", protoFile, err.Error()))
}
if stat.IsDir() {
util.Die(fmt.Errorf("Input %s is not a file", protoFile))
}
outputDir := filepath.FromSlash(args[0])
stat, err = os.Stat(outputDir)
if err != nil || !stat.IsDir() {
util.Die(fmt.Errorf("Output directory %s is not accessible", outputDir))
}
var arglist []string
protoIncPath := GetIncludePath(filepath.FromSlash(genFlagValue.protoIncPath), filepath.Dir(protoFile))
arglist = append(arglist, "--"+protoPathFlag+"="+protoIncPath)
arglist = append(arglist, "--plugin=protoc-gen-custom="+executable)
arglist = append(arglist, "--custom_out="+cmdParam+":"+outputDir)
arglist = append(arglist, protoFile)
if KeepDefaultLangOut {
arglist = append(arglist, "--"+data.GetCodeGenerator(genFlagValue.langValue).GetLang()+"_out=:"+outputDir)
}
protoCmd := exec.Command(protoc, arglist...)
protoCmd.Stderr = os.Stderr
err = protoCmd.Run()
if err != nil {
util.Die(fmt.Errorf("Error to execute protoc: %s", err))
}
}
func init() {
genCmd.Flags().StringVar(&genFlagValue.langValue, langFlag, "go", "language of the generated code, default is go.")
genCmd.Flags().StringVar(&genFlagValue.protoIncPath, protoPathFlag, "", "extra proto file import paths, seperated by ':'(unix) or ';'(windows)")
genCmd.Flags().StringVar(&genFlagValue.protoCustomParam, protoCustomParamFlag, "", "custom parameters to the specific plugin, <key>=<value> separated by ',' ")
}