forked from tokopedia/gripmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_gopackage.sh
executable file
·46 lines (32 loc) · 1.09 KB
/
fix_gopackage.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
#!/bin/bash
protos=("$@")
for proto in "${protos[@]}"
do
# if it's a directory then skip
if [[ -d $proto ]]; then
continue
fi
# example $proto: example/foo/bar/hello.proto
# get string from left until the last /
# example value: example/foo/bar/
dir=${proto%/*}
# remove prefix / if any
dir=$(echo $dir | sed -n 's:^/*\(.*\)$:\1:p')
# get string from right until the first /
# example value: hello.proto
file=${proto##*/}
newdir="protogen/$dir"
newfile="$newdir/$file"
# copy to protogen directory
mkdir -p "$newdir" && \
cp "$proto" "$_" && \
# Force remove any declaration of go_package
# then replace it with our own declaration below
sed -i 's/^option go_package.*$//g' $newfile
# get the line number of "syntax" declaration
syntaxLineNum="$(grep -n "syntax" "$newfile" | head -n 1 | cut -d: -f1)"
goPackageString="option go_package = \"github.com/tokopedia/gripmock/protogen/$dir\";"
# append our own go_package delcaration just below "syntax" declaration
sed -i "${syntaxLineNum}s~$~\n$goPackageString~" $newfile
echo $newfile
done