From 501f2e9031b1c40e2cc4bae5199df867569a49aa Mon Sep 17 00:00:00 2001 From: Changeden Date: Fri, 28 Jan 2022 11:46:53 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=A2=9Ehessian.POJO=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E6=96=B9=E6=B3=95=E4=BD=93=E7=94=9F=E6=88=90=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +- README_CN.md | 7 +- cmd/dubbogo-hessian-registry/README.md | 101 +++++++++ cmd/dubbogo-hessian-registry/constant.go | 23 ++ cmd/dubbogo-hessian-registry/generator.go | 247 ++++++++++++++++++++++ cmd/dubbogo-hessian-registry/logger.go | 21 ++ cmd/dubbogo-hessian-registry/main.go | 72 +++++++ cmd/dubbogo-hessian-registry/pool.go | 38 ++++ 8 files changed, 515 insertions(+), 3 deletions(-) create mode 100644 cmd/dubbogo-hessian-registry/README.md create mode 100644 cmd/dubbogo-hessian-registry/constant.go create mode 100644 cmd/dubbogo-hessian-registry/generator.go create mode 100644 cmd/dubbogo-hessian-registry/logger.go create mode 100644 cmd/dubbogo-hessian-registry/main.go create mode 100644 cmd/dubbogo-hessian-registry/pool.go diff --git a/README.md b/README.md index ac552e9..7f6016e 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,18 @@ The tool should support dubbo protocol. It makes it easy for you to define your ### 3. How to get dubbo `go get -u github.com/dubbogo/tools/cmd/protoc-gen-dubbo` -### 4. How to get dubbo +### 4. How to get dubbo3 `go get -u github.com/dubbogo/tools/cmd/protoc-gen-dubbo3` ### 5. How to get imports-formatter `go get -u github.com/dubbogo/tools/cmd/imports-formatter` -### 6. Quick start:[example](example/README.md) +### 6. How to get hessian-registry-generator +`go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry` + +[example](cmd/dubbogo-hessian-registry/README.md) + +### 7. Quick start:[example](example/README.md) # imports-formatter diff --git a/README_CN.md b/README_CN.md index c8bb8fb..3891106 100644 --- a/README_CN.md +++ b/README_CN.md @@ -17,4 +17,9 @@ ### 5. imports-formatter 工具获取方法 `go get -u github.com/dubbogo/tools/cmd/imports-formatter` -### 6. 使用方法:见[example](example/README_CN.md) +### 6. hessian-registry-generator 工具获取方法 +`go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry` + +[example](cmd/dubbogo-hessian-registry/README.md) + +### 7. 使用方法:见[example](example/README_CN.md) diff --git a/cmd/dubbogo-hessian-registry/README.md b/cmd/dubbogo-hessian-registry/README.md new file mode 100644 index 0000000..ec12fc6 --- /dev/null +++ b/cmd/dubbogo-hessian-registry/README.md @@ -0,0 +1,101 @@ +# dubbogo-hessian-registry + +Auxiliary tools for [dubbo-go](https://github.com/apache/dubbo-go). + +Automatic generate hessian.POJO registry statement. + +## Install + +```shell +go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry +``` + +## Usage + +### main.go + +```go +package main + +//go:generate go run "github.com/dubbogo/tools/cmd/dubbogo-hessian-registry" -include pkg -error -thread 3 +func main() { + +} + +``` + +### pkg/demo.go + +```go +package pkg + +type Demo0 struct { + A string `json:"a"` + B string `json:"b"` +} + +func (*Demo0) JavaClassName() string { + return "org.apache.dubbo.Demo0" +} + +type Demo1 struct { + C string `json:"c"` +} + +func (*Demo1) JavaClassName() string { + return "org.apache.dubbo.Demo1" +} + +``` + +### Execute `go generate` + +```shell +go generate +``` + +### Result + +pkg/demo.go + +```go +package pkg + +import ( + "github.com/apache/dubbo-go-hessian2" +) + +type Demo0 struct { + A string `json:"a"` + B string `json:"b"` +} + +func (*Demo0) JavaClassName() string { + return "org.apache.dubbo.Demo0" +} + +type Demo1 struct { + C string `json:"c"` +} + +func (*Demo1) JavaClassName() string { + return "org.apache.dubbo.Demo1" +} + +func init() { + + hessian.RegisterPOJO(&Demo0{}) + + hessian.RegisterPOJO(&Demo1{}) + +} + +``` + +## Command flags + +| flag | description | default | +|:-------:|:---------------------------------------:|:--------------:| +| include | Preprocess files parent directory path. | ./ | +| error | Only output error message. | false | +| thread | Worker thread limit. | (cpu core) * 2 | diff --git a/cmd/dubbogo-hessian-registry/constant.go b/cmd/dubbogo-hessian-registry/constant.go new file mode 100644 index 0000000..74d9ea7 --- /dev/null +++ b/cmd/dubbogo-hessian-registry/constant.go @@ -0,0 +1,23 @@ +package main + +const ( + PackageRegexp = `^package\s[a-zA-Z_][0-9a-zA-Z_]*$` + + LineCommentRegexp = `\/\/` + MutLineCommentStartRegexp = `\/\*` + MutLineCommentEndRegexp = `\*\/` + + InitFunctionRegexp = `^func\sinit\(\)\s\{$` + + HessianImportRegexp = `"github.com/apache/dubbo-go-hessian2"` + + HessianPOJORegexp = `\*[0-9a-zA-Z_]+\)\sJavaClassName\(\)\sstring\s\{$` + HessianPOJONameRegexp = `\*[0-9a-zA-Z_]+\)` +) + +const ( + newLine byte = '\n' + funcEnd byte = '}' + + targetFileSuffix = ".go" +) diff --git a/cmd/dubbogo-hessian-registry/generator.go b/cmd/dubbogo-hessian-registry/generator.go new file mode 100644 index 0000000..f06621b --- /dev/null +++ b/cmd/dubbogo-hessian-registry/generator.go @@ -0,0 +1,247 @@ +package main + +import ( + "bufio" + "io" + "os" + "regexp" + "strings" +) + +var ( + hessianImport = []byte{newLine, 105, 109, 112, 111, 114, 116, 32, 40, newLine, 9, 34, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 97, 112, 97, 99, 104, 101, 47, 100, 117, 98, 98, 111, 45, 103, 111, 45, 104, 101, 115, 115, 105, 97, 110, 50, 34, newLine, 41, newLine} + + initFunctionPrefix = []byte{newLine, 102, 117, 110, 99, 32, 105, 110, 105, 116, 40, 41, 32, 123, newLine} + initFunctionSuffix = []byte{newLine, funcEnd, newLine} + + hessianRegistryPOJOFunctionPrefix = []byte{9, 104, 101, 115, 115, 105, 97, 110, 46, 82, 101, 103, 105, 115, 116, 101, 114, 80, 79, 74, 79, 40, 38} + hessianRegistryPOJOFunctionSuffix = []byte{123, funcEnd, 41} +) + +type Generator struct { + f string +} + +func NewGenerator(f string) *Generator { + return &Generator{f} +} + +func (g Generator) Execute() { + f := g.f + var file *fileInfo + var err error + showLog(infoLog, "=== Generate start [%s] ===", f) + file, err = scanFile(f) + if err != nil { + showLog(errorLog, "=== Generate error [%s] ===\n%v", f, err) + return + } + err = genRegistryStatement(file) + if err != nil { + showLog(errorLog, "=== Generate error [%s] ===\n%v", f, err) + return + } + showLog(infoLog, "=== Generate completed [%s] ===", f) +} + +// scanFile 扫描文件内容 +func scanFile(filePath string) (file *fileInfo, err error) { + var f *os.File + f, err = os.Open(filePath) + if err != nil { + return + } + defer func(f *os.File) { + _ = f.Close() + }(f) + buf := bufio.NewReader(f) + file = &fileInfo{ + path: filePath, + } + bufferSize := 0 + buffer := make([]byte, bufferSize) + stack := make([][]byte, 0) + var line []byte + var lineSize int + for { + line, _, err = buf.ReadLine() + if err == io.EOF { + err = nil + break + } + + bufferSize = len(buffer) + lineSize = len(line) + + if file.hasInitFunc && lineSize > 0 && line[0] == funcEnd { + file.initFuncEndIndex = bufferSize + 1 // 检测初始化函数结束位 + } + + buffer = append(buffer, line...) + buffer = append(buffer, newLine) + + if passed, _ := regexp.Match(PackageRegexp, line); passed { // 检测package位置 + file.packageStartIndex = bufferSize // 检测初始化函数初始位 + file.packageEndIndex = bufferSize + lineSize + 1 // 检测初始化函数初始位 + continue + } + + if passed, _ := regexp.Match(InitFunctionRegexp, line); passed { // 检测初始化函数 + file.hasInitFunc = true + file.initFuncStartIndex = bufferSize // 检测初始化函数初始位 + file.initFuncStatementStartIndex = bufferSize + lineSize // 初始化函数方法体初始位 + continue + } + + if !file.hasHessianImport { + r, _ := regexp.Compile(HessianImportRegexp) + rIndexList := r.FindIndex(line) + if len(rIndexList) > 0 { // 检测是否已导入hessian2包 + checkStatement := line[:rIndexList[0]] + passed, _ := regexp.Match(LineCommentRegexp, checkStatement) // 是否被行注释 + file.hasHessianImport = !passed + continue + } + } + + if passed, _ := regexp.Match(HessianPOJORegexp, line); !passed { // 校验是否为Hessian.POJO实现类 + continue + } + structName := getStructName(line) + if len(structName) != 0 { + stack = append(stack, structName) + } + } + file.buffer = buffer + file.hessianPOJOList = stack + return +} + +// getStructName 获取Hessian.POJO实现类的类名 +func getStructName(line []byte) []byte { + r, _ := regexp.Compile(HessianPOJONameRegexp) + line = r.Find(line) + if len(line) != 0 { + return line[1 : len(line)-1] + } + return nil +} + +// genRegistryPOJOStatement 生成POJO注册方法体 +func genRegistryPOJOStatement(pojo []byte) []byte { + var buffer []byte + buffer = append(buffer, hessianRegistryPOJOFunctionPrefix...) + buffer = append(buffer, pojo...) + buffer = append(buffer, hessianRegistryPOJOFunctionSuffix...) + return buffer +} + +func lastIndexOf(buf []byte, b byte, s *int) int { + var l int + if s != nil { + l = *s + } else { + l = len(buf) + } + if l < 0 || l >= len(buf) { + return -1 + } + for ; l >= 0; l-- { + if buf[l] == b { + return l + } + } + return -1 +} + +func escape(str string) string { + str = strings.TrimSpace(str) + str = strings.ReplaceAll(str, ".", "\\.") + str = strings.ReplaceAll(str, "(", "\\(") + str = strings.ReplaceAll(str, ")", "\\)") + str = strings.ReplaceAll(str, "{", "\\{") + str = strings.ReplaceAll(str, "}", "\\}") + return str +} + +// genRegistryPOJOStatements 生成POJO注册方法体 +func genRegistryPOJOStatements(file *fileInfo, initFunctionStatement *[]byte) []byte { + f := file.path + hessianPOJOList := file.hessianPOJOList + var buffer []byte + var r *regexp.Regexp + var rIndexList []int + for _, name := range hessianPOJOList { + statement := genRegistryPOJOStatement(name) + if initFunctionStatement != nil { // 检测是否已存在注册方法体 + r, _ = regexp.Compile(escape(string(statement))) + initStatement := *initFunctionStatement + rIndexList = r.FindIndex(initStatement) + if len(rIndexList) > 0 { + i := rIndexList[0] + n := lastIndexOf(initStatement, newLine, &i) + checkStatement := initStatement[lastIndexOf(initStatement, newLine, &n)+1 : i] + if passed, _ := regexp.Match(LineCommentRegexp, checkStatement); !passed { // 是否被行注释 + showLog(infoLog, "=== Ignore POJO [%s].%s ===", f, name) + continue // 忽略相同的注册操作 + } + } + } + + showLog(infoLog, "=== Registry POJO [%s].%s ===", f, name) + + buffer = append(buffer, newLine) + buffer = append(buffer, statement...) + buffer = append(buffer, newLine) + } + return buffer +} + +func genRegistryStatement(file *fileInfo) error { + if file == nil || len(file.hessianPOJOList) == 0 { + return nil + } + buffer := file.buffer + + offset := 0 + + if !file.hasHessianImport { // 追加hessian2导包 + sliceIndex := file.packageEndIndex + offset + var bufferClone []byte + bufferClone = append(bufferClone, buffer[:sliceIndex]...) + bufferClone = append(bufferClone, hessianImport...) + bufferClone = append(bufferClone, buffer[sliceIndex:]...) + buffer = bufferClone + offset += len(hessianImport) + } + + var registryPOJOStatement []byte + + if file.hasInitFunc { + sliceIndex := file.initFuncStatementStartIndex + offset + initFunctionStatement := buffer[sliceIndex : file.initFuncEndIndex+offset-1] + registryPOJOStatement = genRegistryPOJOStatements(file, &initFunctionStatement) + var bufferClone []byte + bufferClone = append(bufferClone, buffer[:sliceIndex]...) + bufferClone = append(bufferClone, registryPOJOStatement...) // 追加POJO注册方法体至init函数 + bufferClone = append(bufferClone, buffer[sliceIndex:]...) + buffer = bufferClone + } else { // 追加初始化函数 + registryPOJOStatement = genRegistryPOJOStatements(file, nil) + buffer = append(buffer, initFunctionPrefix...) // 添加init函数 + buffer = append(buffer, registryPOJOStatement...) // 追加POJO注册方法体至init函数 + buffer = append(buffer, initFunctionSuffix...) + } + + offset += len(registryPOJOStatement) + + f, err := os.OpenFile(file.path, os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + return err + } + defer func(f *os.File) { + _ = f.Close() + }(f) + _, err = f.Write(buffer) + return err +} diff --git a/cmd/dubbogo-hessian-registry/logger.go b/cmd/dubbogo-hessian-registry/logger.go new file mode 100644 index 0000000..8e0ae6a --- /dev/null +++ b/cmd/dubbogo-hessian-registry/logger.go @@ -0,0 +1,21 @@ +package main + +import ( + "log" +) + +type logType int + +const ( + infoLog logType = iota + errorLog +) + +// showLog 输出日志 +func showLog(t logType, format string, v ...interface{}) { + format = format + "\n" + if onlyError == nil || !(*onlyError) || t == errorLog { + log.Printf(format, v...) + return + } +} diff --git a/cmd/dubbogo-hessian-registry/main.go b/cmd/dubbogo-hessian-registry/main.go new file mode 100644 index 0000000..2368e0a --- /dev/null +++ b/cmd/dubbogo-hessian-registry/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "flag" + "fmt" + "io/fs" + "os" + "path/filepath" + "runtime" + "strings" +) + +var ( + include = flag.String("include", "./", "hessian映射关系目录,默认为项目根目录") + onlyError = flag.Bool("error", false, "只输出错误信息,默认全量输出") + maxThread = flag.Int("thread", runtime.NumCPU()*2, "最大工作线程数,默认为CPU核数的2倍") +) + +type fileInfo struct { + path string + + packageStartIndex int + packageEndIndex int + + hasInitFunc bool + initFuncStartIndex int + initFuncEndIndex int + initFuncStatementStartIndex int + + hasHessianImport bool + + buffer []byte + + hessianPOJOList [][]byte +} + +// listFiles 获取目标目录下所有go文件 +func listFiles(dirPath string, suffix string) (fileList []string, err error) { + suffix = strings.ToUpper(suffix) + fileList = make([]string, 0) + _, err = os.Lstat(dirPath) + if err != nil { + return nil, fmt.Errorf("找不到该目录[%s]", dirPath) + } + err = filepath.WalkDir(dirPath, func(path string, d fs.DirEntry, err error) error { // 递归获取目录下所有go文件 + if d == nil || d.IsDir() { + return nil + } + if strings.HasSuffix(strings.ToUpper(d.Name()), suffix) { + fileList = append(fileList, path) + } + return nil + }) + return +} + +//go:generate go fmt +func main() { + flag.Parse() + + fileList, err := listFiles(*include, targetFileSuffix) + if err != nil { + showLog(errorLog, "%v", err) + return + } + + pool := NewPool(*maxThread) + for _, f := range fileList { + pool.Execute(NewGenerator(f)) + } + pool.Wait() +} diff --git a/cmd/dubbogo-hessian-registry/pool.go b/cmd/dubbogo-hessian-registry/pool.go new file mode 100644 index 0000000..37a9a79 --- /dev/null +++ b/cmd/dubbogo-hessian-registry/pool.go @@ -0,0 +1,38 @@ +package main + +import ( + "sync" +) + +// Task 任务 +type Task interface { + Execute() +} + +// Pool 线程池 +type Pool struct { + wg *sync.WaitGroup + + taskQueue chan Task +} + +func NewPool(max int) *Pool { + return &Pool{ + wg: &sync.WaitGroup{}, + taskQueue: make(chan Task, max), + } +} + +func (p Pool) Execute(t Task) { + p.wg.Add(1) + p.taskQueue <- t + go func() { + t.Execute() + <-p.taskQueue + p.wg.Done() + }() +} + +func (p Pool) Wait() { + p.wg.Wait() +} From 9e8a35562c4fab24d9691d99cb0344e2d94060e0 Mon Sep 17 00:00:00 2001 From: Changeden Date: Fri, 28 Jan 2022 12:01:11 +0800 Subject: [PATCH 2/5] Update README --- cmd/dubbogo-hessian-registry/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/dubbogo-hessian-registry/README.md b/cmd/dubbogo-hessian-registry/README.md index ec12fc6..87f986b 100644 --- a/cmd/dubbogo-hessian-registry/README.md +++ b/cmd/dubbogo-hessian-registry/README.md @@ -17,7 +17,7 @@ go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry ```go package main -//go:generate go run "github.com/dubbogo/tools/cmd/dubbogo-hessian-registry" -include pkg -error -thread 3 +//go:generate go run "github.com/dubbogo/tools/cmd/dubbogo-hessian-registry" -include pkg func main() { } @@ -54,6 +54,14 @@ func (*Demo1) JavaClassName() string { go generate ``` +### Console logs +```shell +2022/01/28 11:58:11 === Generate start [pkg\demo.go] === +2022/01/28 11:58:11 === Registry POJO [pkg\demo.go].Demo0 === +2022/01/28 11:58:11 === Registry POJO [pkg\demo.go].Demo1 === +2022/01/28 11:58:11 === Generate completed [pkg\demo.go] === +``` + ### Result pkg/demo.go From 8032e71344d733e88d42fac8361eb8d2b22d1a32 Mon Sep 17 00:00:00 2001 From: Changeden Date: Fri, 28 Jan 2022 14:56:19 +0800 Subject: [PATCH 3/5] Extend dubbogo-cli --- .../README.md | 0 cmd/dubbogo-cli/common/interfaces.go | 16 ++++ .../generator/adapter.go} | 57 +++++++++------ .../generator}/constant.go | 2 +- .../generator}/generator.go | 2 +- .../generator}/logger.go | 7 +- .../generator}/pool.go | 2 +- cmd/dubbogo-cli/main.go | 73 +++++++++++++------ cmd/dubbogo-cli/telnet/adapter.go | 53 ++++++++++++++ 9 files changed, 160 insertions(+), 52 deletions(-) rename cmd/{dubbogo-hessian-registry => dubbogo-cli}/README.md (100%) create mode 100644 cmd/dubbogo-cli/common/interfaces.go rename cmd/{dubbogo-hessian-registry/main.go => dubbogo-cli/generator/adapter.go} (67%) rename cmd/{dubbogo-hessian-registry => dubbogo-cli/generator}/constant.go (96%) rename cmd/{dubbogo-hessian-registry => dubbogo-cli/generator}/generator.go (99%) rename cmd/{dubbogo-hessian-registry => dubbogo-cli/generator}/logger.go (67%) rename cmd/{dubbogo-hessian-registry => dubbogo-cli/generator}/pool.go (96%) create mode 100644 cmd/dubbogo-cli/telnet/adapter.go diff --git a/cmd/dubbogo-hessian-registry/README.md b/cmd/dubbogo-cli/README.md similarity index 100% rename from cmd/dubbogo-hessian-registry/README.md rename to cmd/dubbogo-cli/README.md diff --git a/cmd/dubbogo-cli/common/interfaces.go b/cmd/dubbogo-cli/common/interfaces.go new file mode 100644 index 0000000..2fd7fcd --- /dev/null +++ b/cmd/dubbogo-cli/common/interfaces.go @@ -0,0 +1,16 @@ +package common + +type Adapter interface { + CheckParam() bool + + Execute() + + GetMode() AdapterMode +} + +type AdapterMode int + +const ( + TelnetAdapterMode AdapterMode = iota + GeneratorAdapterMode +) diff --git a/cmd/dubbogo-hessian-registry/main.go b/cmd/dubbogo-cli/generator/adapter.go similarity index 67% rename from cmd/dubbogo-hessian-registry/main.go rename to cmd/dubbogo-cli/generator/adapter.go index 2368e0a..e61b28c 100644 --- a/cmd/dubbogo-hessian-registry/main.go +++ b/cmd/dubbogo-cli/generator/adapter.go @@ -1,21 +1,47 @@ -package main +package generator import ( - "flag" "fmt" "io/fs" "os" "path/filepath" - "runtime" "strings" ) -var ( - include = flag.String("include", "./", "hessian映射关系目录,默认为项目根目录") - onlyError = flag.Bool("error", false, "只输出错误信息,默认全量输出") - maxThread = flag.Int("thread", runtime.NumCPU()*2, "最大工作线程数,默认为CPU核数的2倍") +import ( + "github.com/dubbogo/tools/cmd/dubbogo-cli/common" ) +type GeneratorAdapter struct { + DirPath string // 文件扫描目录 + ThreadLimit int // 最大工作线程数 +} + +func (a *GeneratorAdapter) CheckParam() bool { + if a.ThreadLimit < 1 { + a.ThreadLimit = 1 + } + return true +} + +func (a *GeneratorAdapter) Execute() { + fileList, err := listFiles(a.DirPath, targetFileSuffix) + if err != nil { + showLog(errorLog, "%v", err) + return + } + + pool := NewPool(a.ThreadLimit) + for _, f := range fileList { + pool.Execute(NewGenerator(f)) + } + pool.Wait() +} + +func (a *GeneratorAdapter) GetMode() common.AdapterMode { + return common.GeneratorAdapterMode +} + type fileInfo struct { path string @@ -53,20 +79,3 @@ func listFiles(dirPath string, suffix string) (fileList []string, err error) { }) return } - -//go:generate go fmt -func main() { - flag.Parse() - - fileList, err := listFiles(*include, targetFileSuffix) - if err != nil { - showLog(errorLog, "%v", err) - return - } - - pool := NewPool(*maxThread) - for _, f := range fileList { - pool.Execute(NewGenerator(f)) - } - pool.Wait() -} diff --git a/cmd/dubbogo-hessian-registry/constant.go b/cmd/dubbogo-cli/generator/constant.go similarity index 96% rename from cmd/dubbogo-hessian-registry/constant.go rename to cmd/dubbogo-cli/generator/constant.go index 74d9ea7..ba38a20 100644 --- a/cmd/dubbogo-hessian-registry/constant.go +++ b/cmd/dubbogo-cli/generator/constant.go @@ -1,4 +1,4 @@ -package main +package generator const ( PackageRegexp = `^package\s[a-zA-Z_][0-9a-zA-Z_]*$` diff --git a/cmd/dubbogo-hessian-registry/generator.go b/cmd/dubbogo-cli/generator/generator.go similarity index 99% rename from cmd/dubbogo-hessian-registry/generator.go rename to cmd/dubbogo-cli/generator/generator.go index f06621b..d50cc14 100644 --- a/cmd/dubbogo-hessian-registry/generator.go +++ b/cmd/dubbogo-cli/generator/generator.go @@ -1,4 +1,4 @@ -package main +package generator import ( "bufio" diff --git a/cmd/dubbogo-hessian-registry/logger.go b/cmd/dubbogo-cli/generator/logger.go similarity index 67% rename from cmd/dubbogo-hessian-registry/logger.go rename to cmd/dubbogo-cli/generator/logger.go index 8e0ae6a..5042d88 100644 --- a/cmd/dubbogo-hessian-registry/logger.go +++ b/cmd/dubbogo-cli/generator/logger.go @@ -1,4 +1,4 @@ -package main +package generator import ( "log" @@ -14,8 +14,9 @@ const ( // showLog 输出日志 func showLog(t logType, format string, v ...interface{}) { format = format + "\n" - if onlyError == nil || !(*onlyError) || t == errorLog { - log.Printf(format, v...) + if t == errorLog { + log.Fatalf(format, v...) return } + log.Printf(format, v...) } diff --git a/cmd/dubbogo-hessian-registry/pool.go b/cmd/dubbogo-cli/generator/pool.go similarity index 96% rename from cmd/dubbogo-hessian-registry/pool.go rename to cmd/dubbogo-cli/generator/pool.go index 37a9a79..b3db69a 100644 --- a/cmd/dubbogo-hessian-registry/pool.go +++ b/cmd/dubbogo-cli/generator/pool.go @@ -1,4 +1,4 @@ -package main +package generator import ( "sync" diff --git a/cmd/dubbogo-cli/main.go b/cmd/dubbogo-cli/main.go index ec03db0..8a65409 100644 --- a/cmd/dubbogo-cli/main.go +++ b/cmd/dubbogo-cli/main.go @@ -19,12 +19,10 @@ package main import ( "flag" - "log" -) - -import ( - "github.com/dubbogo/tools/internal/client" - "github.com/dubbogo/tools/internal/json_register" + "github.com/dubbogo/tools/cmd/dubbogo-cli/common" + "github.com/dubbogo/tools/cmd/dubbogo-cli/generator" + "github.com/dubbogo/tools/cmd/dubbogo-cli/telnet" + "runtime" ) var ( @@ -38,6 +36,17 @@ var ( sendObjFilePath string recvObjFilePath string timeout int + + isRegistryGeneratorMode bool // 是否为生成器模式 + registryFileDirectoryPath string // 文件扫描目录 + generatorWorkerThreadLimit int // 最大工作线程数 +) + +var ( + telnetAdapter common.Adapter + generatorAdapter common.Adapter + + adapters []common.Adapter ) func init() { @@ -51,30 +60,50 @@ func init() { flag.StringVar(&sendObjFilePath, "sendObj", "", "json file path to define transfer struct") flag.StringVar(&recvObjFilePath, "recvObj", "", "json file path to define receive struct") flag.IntVar(&timeout, "timeout", 3000, "request timeout (ms)") -} -func checkParam() { - if method == "" { - log.Fatalln("-method value not fond") + telnetAdapter = &telnet.TelnetAdapter{ + Host: host, + Port: port, + ProtocolName: protocolName, + InterfaceID: InterfaceID, + Version: version, + Group: group, + Method: method, + SendObjFilePath: sendObjFilePath, + RecvObjFilePath: recvObjFilePath, + Timeout: timeout, } - if sendObjFilePath == "" { - log.Fatalln("-sendObj value not found") + adapters = append(adapters, telnetAdapter) + + flag.BoolVar(&isRegistryGeneratorMode, "generator", false, "switch to registry statement generator mode, default `false`") + flag.StringVar(®istryFileDirectoryPath, "include", "./", "file scan directory path, default `./`") + flag.IntVar(&generatorWorkerThreadLimit, "thread", runtime.NumCPU()*2, "worker thread limit, default (cpu core) * 2") + + generatorAdapter = &generator.GeneratorAdapter{ + DirPath: registryFileDirectoryPath, + ThreadLimit: generatorWorkerThreadLimit, } - if recvObjFilePath == "" { - log.Fatalln("-recObj value not found") + adapters = append(adapters, generatorAdapter) +} + +func chooseAdapter(mode common.AdapterMode) common.Adapter { + for _, adapter := range adapters { + if adapter.GetMode() == mode { + return adapter + } } + return telnetAdapter } func main() { flag.Parse() - checkParam() - reqPkg := json_register.RegisterStructFromFile(sendObjFilePath) - recvPkg := json_register.RegisterStructFromFile(recvObjFilePath) - t, err := client.NewTelnetClient(host, port, protocolName, InterfaceID, version, group, method, reqPkg, timeout) - if err != nil { - panic(err) + var mode common.AdapterMode + if isRegistryGeneratorMode { + mode = common.GeneratorAdapterMode + } + adapter := chooseAdapter(mode) + if adapter.CheckParam() { + adapter.Execute() } - t.ProcessRequests(recvPkg) - t.Destroy() } diff --git a/cmd/dubbogo-cli/telnet/adapter.go b/cmd/dubbogo-cli/telnet/adapter.go new file mode 100644 index 0000000..d2d9ff2 --- /dev/null +++ b/cmd/dubbogo-cli/telnet/adapter.go @@ -0,0 +1,53 @@ +package telnet + +import ( + "log" +) + +import ( + "github.com/dubbogo/tools/cmd/dubbogo-cli/common" + "github.com/dubbogo/tools/internal/client" + "github.com/dubbogo/tools/internal/json_register" +) + +type TelnetAdapter struct { + Host string + Port int + ProtocolName string + InterfaceID string + Version string + Group string + Method string + SendObjFilePath string + RecvObjFilePath string + Timeout int +} + +func (a *TelnetAdapter) CheckParam() bool { + if a.Method == "" { + log.Fatalln("-method value not fond") + } + if a.SendObjFilePath == "" { + log.Fatalln("-sendObj value not found") + } + if a.RecvObjFilePath == "" { + log.Fatalln("-recObj value not found") + } + return true +} + +func (a *TelnetAdapter) Execute() { + reqPkg := json_register.RegisterStructFromFile(a.SendObjFilePath) + recvPkg := json_register.RegisterStructFromFile(a.RecvObjFilePath) + + t, err := client.NewTelnetClient(a.Host, a.Port, a.ProtocolName, a.InterfaceID, a.Version, a.Group, a.Method, reqPkg, a.Timeout) + if err != nil { + panic(err) + } + t.ProcessRequests(recvPkg) + t.Destroy() +} + +func (a *TelnetAdapter) GetMode() common.AdapterMode { + return common.TelnetAdapterMode +} From e9be14c0411a1eec8dd63e98d6f5f8f656c82d09 Mon Sep 17 00:00:00 2001 From: Changeden Date: Fri, 28 Jan 2022 15:08:14 +0800 Subject: [PATCH 4/5] =?UTF-8?q?dubbogo-cli=E6=96=B0=E5=A2=9E=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +------ README_CN.md | 7 +------ cmd/dubbogo-cli/{ => generator}/README.md | 5 ++--- 3 files changed, 4 insertions(+), 15 deletions(-) rename cmd/dubbogo-cli/{ => generator}/README.md (87%) diff --git a/README.md b/README.md index 7f6016e..cda6cb7 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,7 @@ The tool should support dubbo protocol. It makes it easy for you to define your ### 5. How to get imports-formatter `go get -u github.com/dubbogo/tools/cmd/imports-formatter` -### 6. How to get hessian-registry-generator -`go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry` - -[example](cmd/dubbogo-hessian-registry/README.md) - -### 7. Quick start:[example](example/README.md) +### 6. Quick start:[example](example/README.md) # imports-formatter diff --git a/README_CN.md b/README_CN.md index 3891106..c8bb8fb 100644 --- a/README_CN.md +++ b/README_CN.md @@ -17,9 +17,4 @@ ### 5. imports-formatter 工具获取方法 `go get -u github.com/dubbogo/tools/cmd/imports-formatter` -### 6. hessian-registry-generator 工具获取方法 -`go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry` - -[example](cmd/dubbogo-hessian-registry/README.md) - -### 7. 使用方法:见[example](example/README_CN.md) +### 6. 使用方法:见[example](example/README_CN.md) diff --git a/cmd/dubbogo-cli/README.md b/cmd/dubbogo-cli/generator/README.md similarity index 87% rename from cmd/dubbogo-cli/README.md rename to cmd/dubbogo-cli/generator/README.md index 87f986b..83e4dd3 100644 --- a/cmd/dubbogo-cli/README.md +++ b/cmd/dubbogo-cli/generator/README.md @@ -7,7 +7,7 @@ Automatic generate hessian.POJO registry statement. ## Install ```shell -go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry +go get -u github.com/dubbogo/tools/cmd/dubbogo-cli ``` ## Usage @@ -17,7 +17,7 @@ go get -u github.com/dubbogo/tools/cmd/dubbogo-hessian-registry ```go package main -//go:generate go run "github.com/dubbogo/tools/cmd/dubbogo-hessian-registry" -include pkg +//go:generate go run "github.com/dubbogo/tools/cmd/dubbogo-cli" -generator -include pkg func main() { } @@ -105,5 +105,4 @@ func init() { | flag | description | default | |:-------:|:---------------------------------------:|:--------------:| | include | Preprocess files parent directory path. | ./ | -| error | Only output error message. | false | | thread | Worker thread limit. | (cpu core) * 2 | From 5fce228476bffdaf54b160a1cbe99be0f8da97a7 Mon Sep 17 00:00:00 2001 From: Changeden Date: Fri, 28 Jan 2022 15:10:17 +0800 Subject: [PATCH 5/5] Add apache license. --- cmd/dubbogo-cli/common/interfaces.go | 17 +++++++++++++++++ cmd/dubbogo-cli/generator/adapter.go | 17 +++++++++++++++++ cmd/dubbogo-cli/generator/constant.go | 17 +++++++++++++++++ cmd/dubbogo-cli/generator/generator.go | 17 +++++++++++++++++ cmd/dubbogo-cli/generator/logger.go | 17 +++++++++++++++++ cmd/dubbogo-cli/generator/pool.go | 17 +++++++++++++++++ cmd/dubbogo-cli/telnet/adapter.go | 17 +++++++++++++++++ 7 files changed, 119 insertions(+) diff --git a/cmd/dubbogo-cli/common/interfaces.go b/cmd/dubbogo-cli/common/interfaces.go index 2fd7fcd..98bc821 100644 --- a/cmd/dubbogo-cli/common/interfaces.go +++ b/cmd/dubbogo-cli/common/interfaces.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package common type Adapter interface { diff --git a/cmd/dubbogo-cli/generator/adapter.go b/cmd/dubbogo-cli/generator/adapter.go index e61b28c..fe7fffa 100644 --- a/cmd/dubbogo-cli/generator/adapter.go +++ b/cmd/dubbogo-cli/generator/adapter.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package generator import ( diff --git a/cmd/dubbogo-cli/generator/constant.go b/cmd/dubbogo-cli/generator/constant.go index ba38a20..a4d8c99 100644 --- a/cmd/dubbogo-cli/generator/constant.go +++ b/cmd/dubbogo-cli/generator/constant.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package generator const ( diff --git a/cmd/dubbogo-cli/generator/generator.go b/cmd/dubbogo-cli/generator/generator.go index d50cc14..0bfc374 100644 --- a/cmd/dubbogo-cli/generator/generator.go +++ b/cmd/dubbogo-cli/generator/generator.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package generator import ( diff --git a/cmd/dubbogo-cli/generator/logger.go b/cmd/dubbogo-cli/generator/logger.go index 5042d88..c8ba65f 100644 --- a/cmd/dubbogo-cli/generator/logger.go +++ b/cmd/dubbogo-cli/generator/logger.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package generator import ( diff --git a/cmd/dubbogo-cli/generator/pool.go b/cmd/dubbogo-cli/generator/pool.go index b3db69a..c514804 100644 --- a/cmd/dubbogo-cli/generator/pool.go +++ b/cmd/dubbogo-cli/generator/pool.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package generator import ( diff --git a/cmd/dubbogo-cli/telnet/adapter.go b/cmd/dubbogo-cli/telnet/adapter.go index d2d9ff2..96474da 100644 --- a/cmd/dubbogo-cli/telnet/adapter.go +++ b/cmd/dubbogo-cli/telnet/adapter.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package telnet import (