-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_go.sh
executable file
·105 lines (86 loc) · 2.77 KB
/
generate_go.sh
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
#!/usr/bin/env bash
set -ex
check_protoc_version() {
version=$(protoc --version)
major=$(echo ${version} | sed -n -e 's/.*\([0-9]\{1,\}\)\.[0-9]\{1,\}\.[0-9]\{1,\}.*/\1/p')
minor=$(echo ${version} | sed -n -e 's/.*[0-9]\{1,\}\.\([0-9]\{1,\}\)\.[0-9]\{1,\}.*/\1/p')
if [ "$major" -gt 3 ]; then
return 0
fi
if [ "$major" -eq 3 ] && [ "$minor" -ge 1 ]; then
return 0
fi
echo "protoc version not match, version 3.1.x is needed, current version: ${version}"
return 1
}
push () {
pushd $1 >/dev/null 2>&1
}
pop () {
popd $1 >/dev/null 2>&1
}
cmd_exists () {
which "$1" 1>/dev/null 2>&1
}
if ! check_protoc_version; then
exit 1
fi
PROGRAM=$(basename "$0")
if [ -z $GOPATH ]; then
printf "Error: the environment variable GOPATH is not set, please set it before running %s\n" $PROGRAM > /dev/stderr
exit 1
fi
GO_PREFIX_PATH=github.com/zhihu/zetta-proto/pkg
export PATH=$(pwd)/_tools/bin:$GOPATH/bin:$PATH
echo "install tools..."
# GO111MODULE=off go get github.com/twitchtv/retool
# GO111MODULE=off retool sync || exit 1
function collect() {
# file=$(basename $1)
# base_name=$(basename $file ".proto")
base_name="tablestore"
mkdir -p ../pkg/$base_name
if [ -z $GO_OUT_M ]; then
GO_OUT_M="M$file=$GO_PREFIX_PATH/$base_name"
else
GO_OUT_M="$GO_OUT_M,M$file=$GO_PREFIX_PATH/$base_name"
fi
}
# Although eraftpb.proto is copying from raft-rs, however there is no
# official go code ship with the crate, so we need to generate it manually.
# collect include/eraftpb.proto
# collect include/rustproto.proto
cd proto
for file in `ls *.proto`
do
collect $file
done
echo "generate go code..."
ret=0
GO_OUT_M="Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/empty.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/wrappers.proto=github.com/gogo/protobuf/types,\
$GO_OUT_M"
function gen() {
# base_name=$(basename $1 ".proto")
base_name="tablestore"
protoc -I.:../include --gofast_out=plugins=grpc,$GO_OUT_M:../pkg/$base_name $1 || ret=$?
# protoc -I.:$GOPATH/src/github.com/gogo/protobuf --gofast_out=plugins=grpc,$GO_OUT_M:../pkg/$base_name $1 || ret=$?
cd ../pkg/$base_name
# sed -i.bak -E 's/import _ \"gogoproto\"//g' *.pb.go
# sed -i.bak -E 's/import fmt \"fmt\"//g' *.pb.go
# sed -i.bak -E 's/import io \"io\"//g' *.pb.go
# sed -i.bak -E 's/import math \"math\"//g' *.pb.go
# sed -i.bak -E 's/import ' *.pb.go
rm -f *.bak
goimports -w *.pb.go
cd ../../proto
}
for file in `ls *.proto`
do
gen $file
done
exit $ret