-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_paramter.go
81 lines (73 loc) · 2.17 KB
/
plugin_paramter.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
package protokit
import (
"encoding/json"
"io/ioutil"
"os"
"time"
"github.com/mattn/go-colorable"
"github.com/sandwich-go/boost/xpanic"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// Parameter protokito传递过来的参数数据
type Parameter struct {
NameSpaces map[string]string // namespace到根路径的映射,需要手动加载
WorkingDir string
// 输出目录
Outpath string
OutpathLua string
OutpathGolang string
OutpathJS string
OutpathCSharp string
OutpathPython string
// raw data属性
RawDataPackageName string
OutpathRawdataServer string
OutpathRawdataClient string
// golang 基础配置
GolangBasePackagePath string
GolangRelative bool
// cs基础配置
CSBaseNamespace string
CSConfNamespace string
CSRawDataNamespace string
// 日志配置
LogLevel int
LogColor bool
Raw []byte
}
type Plugin struct {
Parameter *Parameter // 自动解析得到的参数数据,由protokitgo传递而来
Namespaces map[string]*Namespace
}
// NewPlugin 返回插件
func MustNewPlugin(opts ...Option) *Plugin {
var p Plugin
p.Parameter = &Parameter{}
p.Namespaces = make(map[string]*Namespace)
content, err := ioutil.ReadAll(os.Stdin)
xpanic.WhenErrorAsFmtFirst(err, "got err:%w while read stdin")
err = json.Unmarshal(content, p.Parameter)
xpanic.WhenErrorAsFmtFirst(err, "got err:%w while unmarshal to Parameter")
xpanic.WhenTrue(p.Parameter.WorkingDir == "", "WorkingDir should not empty")
zerolog.SetGlobalLevel(zerolog.Level(p.Parameter.LogLevel))
log.Logger = log.Output(zerolog.ConsoleWriter{Out: colorable.NewColorableStdout(), TimeFormat: time.RFC3339, NoColor: !p.Parameter.LogColor})
// 解析namespaces
var nsList []*Namespace
for k, v := range p.Parameter.NameSpaces {
nsList = append(nsList, NewNamespace(k, v))
}
if len(nsList) > 0 {
optList := []Option{
WithProtoFileAccessor(MustGetFileAccessorWithNamespace(nsList...)),
WithGolangBasePackagePath(p.Parameter.GolangBasePackagePath),
WithGolangRelative(p.Parameter.GolangRelative),
}
parser := NewParser(append(optList, opts...)...)
parser.Parse(nsList...)
}
for _, v := range nsList {
p.Namespaces[v.Name] = v
}
return &p
}