-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-go
executable file
·203 lines (152 loc) · 3.56 KB
/
generate-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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env sh
set -e
show_help() {
cat << EOF >&2
generate-go v1.0.0
Compile all .proto definitions into importable .go files
Usage: generate-go [options] LND_VERSION
Where LND_VERSION is in a form: [v]MAJOR.MINOR.PATCH (ex: v0.9.0), or "all" to generate for all versions
Options:
-h, --help, help Show this help message
-S, --strip-version Don't include lnd version in the path (only works if LND_VERSION != "all")
-o, --output Save generated files to a specified dir (created, if doesn't exist)
Examples:
./generate-go all
./generate-go -o /tmp/last/ v0.9.0
github: github.com/lncm/lnd-rpc/
EOF
}
# If no arguments passed, show help and exit
if [ "$#" -le 0 ]; then
show_help
exit 1
fi
#
## UTILS; General purpose utility fns
#
## Define logging utility fns as early as possible
log() {
# shellcheck disable=SC2059
>&2 printf "$*\n"
}
log_pad() {
log "\t$*"
}
log_arrow() {
log_pad "-> $*"
}
log_err() {
log
log_pad "ERR: $*"
log
}
log_ok() {
log_arrow "${*:-ok}" # if nothing passed, write "ok"
}
log_err_input() {
log_err "$*"
show_help
}
check_dependencies() {
log "Checking dependencies…"
for cmd in "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
log_err "This script requires \"$cmd\" to be installed"
exit 1
fi
done
log_ok
}
#
## DEFINITIONS; Globally-scoped variables
#
DIR=
STRIP_VER=false
#
## LOGIC; `generate-go` logic
#
generate_go_for_tag() {
tag=$1
tmp_dir="$(mktemp -d)"
log "Generating .go for ${tag}…"
for proto in "$tag"/*/*.proto; do
log_arrow "${proto}…"
protoc --go_out=plugins=grpc,paths=source_relative:"$tmp_dir" -I. -I"./$tag" "$proto"
done
log_pad "Moving files to final destination…"
(
cd "$tmp_dir"
if [ "$STRIP_VER" = "true" ]; then
cd "$tag"
fi
find . -name '*.go' | cpio -pdmu "$DIR" 2>/dev/null
)
log_ok "done"
}
#
## MAIN; This is where the execution begins
#
while :; do
opt="$1"
# Handle fmt: `--output=/tmp/` or `-o=/tmp/`; Split `opt` on `=` into `opt` & `value`
# NOTE: prepended with `=`, because if `-d` isn't found, `cut` ignores `-f`, and returns the whole string regardless…
value="$(echo "=$opt" | cut -d= -f3)"
if [ -n "$value" ]; then
opt="$(echo "=$opt" | cut -d= -f2)"
fi
case "$opt" in
-h|--help|help)
show_help
exit 0
;;
-S|--strip-version) STRIP_VER=true ;;
-o|--output)
if [ -z "$value" ] && [ "$2" = "${2#-}" ]; then
value="$2"
shift
fi
if [ -z "$value" ]; then
log_err_input "$opt requires a non-empty argument"
exit 1
fi
DIR="$value"
;;
--) shift ; break ;;
-?*)
log_err_input "Unknown option: $1"
exit 1
;;
*) break
esac
shift
done
# If DIR is already set, use it
# If not, use `pwd`
DIR="${DIR:-$(pwd)}"
if [ -z "$1" ]; then
log_err_input 'LND_VERSION has to be provided (ex: v0.9.0), or "all"'
exit 1
fi
TAG="$1"
shift
if [ "$TAG" = "all" ] && [ "$STRIP_VER" = "true" ]; then
log_err_input '--strip-version cannot be set together with "all"'
exit 1
fi
if [ "$TAG" != "all" ]; then
# Make sure version always starts with v
TAG="v${TAG#v}"
# Remove possible`-beta` suffix
TAG="${TAG%-beta}"
else
TAG=
fi
# Parsing args done, log start
log "Generating ${TAG:-all} into ${DIR}…"
check_dependencies protoc
# Make sure output directory exists
mkdir -p "$DIR"
# Use TAG, if provided, otherwise loop through all ./v*/ directories
for dir in ./${TAG:-v**}; do
generate_go_for_tag "${dir#./}"
done