-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto.go
108 lines (100 loc) · 2.39 KB
/
proto.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
/*
* @Author: kjcx
* @Date: 2020-08-04 09:46:21
* @LastEditTime: 2020-08-04 14:38:10
* @Description: microv1 to microv2 将micro v1 升级到v2 代码批量替换proto生成方法
* @FilePath:
*/
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
)
func Run() {
var Dir = "/ckp"
GenerateProto(Dir)
}
/**
* 生成v2 proto
*/
func GenerateProto(dir string) {
files := ReadFileSuffix(dir, ".proto")
fmt.Println("files:", files)
for _, v := range files {
dir, file := path.Split(v)
fmt.Println("dddddd", dir, file, v)
//删除v1生成的proto 文件后缀.pb.go .micro.go
file_strs := strings.Split(file, ".")
// fmt.Println("file_names", file_names, v, dir)
Cmd("rm", []string{"-rf", dir + file_strs[0] + ".pb.go", dir + file_strs[0] + ".micro.go"})
// 生成新的proto文件
s, err := CmdAndChangeDir(dir, "protoc", []string{"--proto_path=.", "--micro_out=.", "--go_out=.", file})
fmt.Println("res:", s, err)
}
}
/**
* 进入目录执行命令
*/
func CmdAndChangeDir(dir string, commandName string, params []string) (string, error) {
cmd := exec.Command(commandName, params...)
fmt.Println("CmdAndChangeDir", dir, cmd.Args)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
cmd.Dir = dir
err := cmd.Start()
if err != nil {
return "", err
}
err = cmd.Wait()
return out.String(), err
}
/**
* 执行cmd命令
*/
func Cmd(commandName string, params []string) (string, error) {
cmd := exec.Command(commandName, params...)
fmt.Println("Cmd", cmd.Args)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return "", err
}
err = cmd.Wait()
return out.String(), err
}
/**
* 执行cmd命令字符串
*/
func ExecShell(cmdstring string) string {
cmd := exec.Command("/bin/bash", "-c", cmdstring)
//创建获取命令输出管道
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Printf("Error:can not obtain stdout pipe for command:%s\n", err)
return err.Error()
}
//执行命令
if err := cmd.Start(); err != nil {
fmt.Println("Error:The command is err,", err)
return err.Error()
}
//读取所有输出
bytes, err := ioutil.ReadAll(stdout)
if err != nil {
fmt.Println("ReadAll Stdout:", err.Error())
return string(bytes)
}
if err := cmd.Wait(); err != nil {
fmt.Println("wait:", err.Error(), cmdstring, string(bytes))
return err.Error()
}
return string(bytes)
}