commit e923d852410403eeff91850033b8400b46d15c79 Author: david Date: Sun Feb 11 01:36:12 2024 +0800 init diff --git a/README.md b/README.md new file mode 100644 index 0000000..aad4235 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# merge + +merge mult protot file. \ No newline at end of file diff --git a/gen.proto b/gen.proto new file mode 100644 index 0000000..fc2c414 --- /dev/null +++ b/gen.proto @@ -0,0 +1,134 @@ +syntax = "proto3"; +package passport; +option go_package = "./passport"; + +// D:\work\bsm\bsm-tools\merge\proto\a.proto START + + +// Passport-通行证模块-登录 +service Login{ + // 通过密码登录 + rpc Pwd(LoginByPwdRequest) returns (LoginReply) {} + + // 通过验证码登录 + rpc Code(LoginByCodeRequest) returns (LoginReply) {} + + // 通过Wechat登录 + rpc WeChatByCode(WeChatByCodeRequest) returns (WeChatByCodeReply) {} + rpc WeChatByPhone(WeChatByPhoneRequest) returns (WeChatByPhoneReply) {} + rpc WeChatGetUserinfo(WeChatByPhoneRequest) returns (WeChatGetUserinfoReply) {} + + // 通过AppleID登录 + rpc AppleID(LoginByAppleIDRequest) returns (LoginReply) {} + + // 通过验证码登录并注册 + rpc CodeAndRegister(LoginByCodeRequest) returns (LoginReply) {} +} + + +message LoginByPwdRequest { + string account = 1; // 账号 必填 + string password = 2; // 密码 必填 + string device = 3; // 设备 必填 +} + + +message LoginByCodeRequest { + string country = 1; // 国家 + string phone = 2; // 手机号码 + string code = 3; // 验证码 + string device = 4;// 设备 +} + +message WeChatByCodeRequest { + string code = 1; // WeChat code + int64 agency_id=2; // 代理ID + int64 staff_id=3; // 工作人员ID +} + +message WeChatByPhoneReply { + string account = 1; // WeChat code +} + +message WeChatByCodeReply { + string session_key = 1; // WeChat session key + string open_id = 2; // WeChat open id + string identity = 3; // identity + string account = 4; // account +} + +message WeChatByPhoneRequest { + string session_key = 1; // WeChat session key + string open_id = 2; // open_id + string data = 3; // data + string iv = 4; // iv + string identity = 5; // identiy +} + +message WeChatGetUserinfoReply { + int64 id=1; + string account=2; + string identity=3; + string nickname=4; + string avatar=5; + string rights=6; + string token=7; +} + +message LoginByAppleIDRequest { + string apple_id = 1; // apple id + string desc = 2; // 描述 + string device = 3;// 设备 +} + +message LoginReply { + string identity = 1; //用户唯一码 + string data_bind = 2; // 数据绑定的相关说明,PASS通过,NOPHONE没有绑定手机号,NOPWD没有设置密码,NOBIND没有手机号同时没有密码 + string token = 3; //用户凭证 + map extend = 4; //扩展字段 +} + +// END + + +// D:\work\bsm\bsm-tools\merge\proto\b.proto START + +// Passport-通行证模块-注册 +service Register{ + // 帐号密码注册 + rpc Pwd(RegisterRequest) returns (RegisterReply) {} + + // 手机验证码注册 + rpc Code(RegisterRequest) returns (RegisterReply) {} + + // Wechat注册 + rpc WeChat(RegisterRequest) returns (RegisterReply) {} + + // AppleID注册 + rpc AppleID(RegisterRequest) returns (RegisterReply) {} +} + + +message RegisterRequest { + string origin = 1; //来源,归属 + string account = 2; //帐号 + string phone = 3; //手机号 + string password = 4; //密码 + string code = 5; //验证码 + string wechat_union_id = 6; //微信UnionID + string wechat_open_id = 7; //微信OpenID + string apple_id = 8; //Apple ID + string nickname = 9; //昵称 + string type = 10; //类型 + string device = 11; //设备 + int64 agency_id=12; // 代理ID + int64 staff_id=13; // 工作人员ID +} + +message RegisterReply { + string identity = 1; //用户唯一码 + string data_bind = 2; // 数据绑定的相关说明,PASS通过,NOPHONE没有绑定手机号,NOPWD没有设置密码,NOBIND没有手机号同时没有密码 + string token = 3; //用户Header所需Token + map extend = 4; //扩展字段 +} +// END diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..07f0b99 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.apinb.com/bsm-tools/proto-merge + +go 1.21.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..8b989f1 --- /dev/null +++ b/main.go @@ -0,0 +1,185 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "strings" +) + +// merge ./*.proto auto_gen.proto +func main() { + if len(os.Args) < 2 { + fmt.Println("Args error, eg: proto-merge ./*.proto auto_gen.proto") + os.Exit(0) + } else { + in := os.Args[1] + out := os.Args[2] + fmt.Println("Merge In:", in, "Out:", out) + + files, err := FindFilesByPattern(in) + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + files = IsPorotFile(files) + + // 合并 + var mergeLins []string + genHeader := make(map[string]string) + + var protoHeader []string + protoHeader = append(protoHeader, "syntax = \"proto3\";") + + for _, file := range files { + mergeLins = append(mergeLins, "") + mergeLins = append(mergeLins, "// "+file+" START") + + header, lines := scanProtoFile(file) + mergeLins = append(mergeLins, lines...) + + mergeLins = append(mergeLins, "// END") + mergeLins = append(mergeLins, "") + + for key, val := range header { + genHeader[key] = val + } + } + + for k, v := range genHeader { + + if k == "package" { + protoHeader = append(protoHeader, v) + } + } + + for k, v := range genHeader { + if k == "option" { + protoHeader = append(protoHeader, v) + } + } + + var body []string + body = append(body, protoHeader...) + body = append(body, mergeLins...) + + // 输出 + err = StringToFile(out, strings.Join(body, "\n")) + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } else { + fmt.Println("Merge OK!") + } + + } +} + +// FindFilesByPattern takes a combined string of directory and pattern and returns a slice of matching file names. +func FindFilesByPattern(pathWithPattern string) ([]string, error) { + // 使用Glob函数查找匹配的文件 + files, err := filepath.Glob(pathWithPattern) + if err != nil { + return nil, err // 如果发生错误,返回错误信息 + } + + // 返回文件名切片 + baseFileName := filepath.Base(pathWithPattern) + + absPath, err := filepath.Abs(pathWithPattern) + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + + dirPath := strings.ReplaceAll(absPath, baseFileName, "") + + for idx, v := range files { + bfn := filepath.Base(v) + files[idx] = dirPath + bfn + } + + return files, nil +} + +func IsPorotFile(in []string) []string { + var ok []string + + for _, v := range in { + body, err := os.ReadFile(v) + if err != nil { + fmt.Println(err.Error()) + } + if strings.Contains(string(body), "proto3") { + ok = append(ok, v) + } + + } + return ok +} + +func scanProtoFile(filePath string) (map[string]string, []string) { + file, err := os.Open(filePath) + if err != nil { + panic("无法打开文件") + } + defer file.Close() + + scanner := bufio.NewScanner(file) + var bodys []string + var header map[string]string = make(map[string]string) + + for scanner.Scan() { + line := scanner.Text() + + /* + line = strings.Trim(line, "") + line = strings.ReplaceAll(line, "\r\n", "") + line = strings.ReplaceAll(line, "\n", "") + */ + + //if line != "" { + key, isSysTag := containsSysTag(line) + if isSysTag { + header[key] = line + } else { + bodys = append(bodys, line) + } + //} + } + + return header, bodys +} + +func containsSysTag(line string) (string, bool) { + var tags []string = []string{"syntax", "package", "option", "import"} + + for _, v := range tags { + if len(line) >= len(v) { + if line[0:len(v)] == v { + + return v, true + } + + } + } + + return "", false +} + +func StringToFile(path, content string) error { + startF, err := os.Create(path) + if err != nil { + return errors.New("os.Create create file " + path + " error:" + err.Error()) + } + defer startF.Close() + _, err = io.WriteString(startF, content) + if err != nil { + return errors.New("io.WriteString to " + path + " error:" + err.Error()) + } + return nil +} diff --git a/proto/a.proto b/proto/a.proto new file mode 100644 index 0000000..b8386ac --- /dev/null +++ b/proto/a.proto @@ -0,0 +1,88 @@ +syntax = "proto3"; +package passport; +option go_package = "./passport"; + + +// Passport-通行证模块-登录 +service Login{ + // 通过密码登录 + rpc Pwd(LoginByPwdRequest) returns (LoginReply) {} + + // 通过验证码登录 + rpc Code(LoginByCodeRequest) returns (LoginReply) {} + + // 通过Wechat登录 + rpc WeChatByCode(WeChatByCodeRequest) returns (WeChatByCodeReply) {} + rpc WeChatByPhone(WeChatByPhoneRequest) returns (WeChatByPhoneReply) {} + rpc WeChatGetUserinfo(WeChatByPhoneRequest) returns (WeChatGetUserinfoReply) {} + + // 通过AppleID登录 + rpc AppleID(LoginByAppleIDRequest) returns (LoginReply) {} + + // 通过验证码登录并注册 + rpc CodeAndRegister(LoginByCodeRequest) returns (LoginReply) {} +} + + +message LoginByPwdRequest { + string account = 1; // 账号 必填 + string password = 2; // 密码 必填 + string device = 3; // 设备 必填 +} + + +message LoginByCodeRequest { + string country = 1; // 国家 + string phone = 2; // 手机号码 + string code = 3; // 验证码 + string device = 4;// 设备 +} + +message WeChatByCodeRequest { + string code = 1; // WeChat code + int64 agency_id=2; // 代理ID + int64 staff_id=3; // 工作人员ID +} + +message WeChatByPhoneReply { + string account = 1; // WeChat code +} + +message WeChatByCodeReply { + string session_key = 1; // WeChat session key + string open_id = 2; // WeChat open id + string identity = 3; // identity + string account = 4; // account +} + +message WeChatByPhoneRequest { + string session_key = 1; // WeChat session key + string open_id = 2; // open_id + string data = 3; // data + string iv = 4; // iv + string identity = 5; // identiy +} + +message WeChatGetUserinfoReply { + int64 id=1; + string account=2; + string identity=3; + string nickname=4; + string avatar=5; + string rights=6; + string token=7; +} + +message LoginByAppleIDRequest { + string apple_id = 1; // apple id + string desc = 2; // 描述 + string device = 3;// 设备 +} + +message LoginReply { + string identity = 1; //用户唯一码 + string data_bind = 2; // 数据绑定的相关说明,PASS通过,NOPHONE没有绑定手机号,NOPWD没有设置密码,NOBIND没有手机号同时没有密码 + string token = 3; //用户凭证 + map extend = 4; //扩展字段 +} + diff --git a/proto/b.proto b/proto/b.proto new file mode 100644 index 0000000..33f05a4 --- /dev/null +++ b/proto/b.proto @@ -0,0 +1,42 @@ +syntax = "proto3"; +package passport; +option go_package = "./passport"; + +// Passport-通行证模块-注册 +service Register{ + // 帐号密码注册 + rpc Pwd(RegisterRequest) returns (RegisterReply) {} + + // 手机验证码注册 + rpc Code(RegisterRequest) returns (RegisterReply) {} + + // Wechat注册 + rpc WeChat(RegisterRequest) returns (RegisterReply) {} + + // AppleID注册 + rpc AppleID(RegisterRequest) returns (RegisterReply) {} +} + + +message RegisterRequest { + string origin = 1; //来源,归属 + string account = 2; //帐号 + string phone = 3; //手机号 + string password = 4; //密码 + string code = 5; //验证码 + string wechat_union_id = 6; //微信UnionID + string wechat_open_id = 7; //微信OpenID + string apple_id = 8; //Apple ID + string nickname = 9; //昵称 + string type = 10; //类型 + string device = 11; //设备 + int64 agency_id=12; // 代理ID + int64 staff_id=13; // 工作人员ID +} + +message RegisterReply { + string identity = 1; //用户唯一码 + string data_bind = 2; // 数据绑定的相关说明,PASS通过,NOPHONE没有绑定手机号,NOPWD没有设置密码,NOBIND没有手机号同时没有密码 + string token = 3; //用户Header所需Token + map extend = 4; //扩展字段 +} diff --git a/proto/c.txt b/proto/c.txt new file mode 100644 index 0000000..2989fde --- /dev/null +++ b/proto/c.txt @@ -0,0 +1 @@ +this is test file \ No newline at end of file