Skip to content

Commit 46d1457

Browse files
committed
Force replace go_package
removing declaration of go_package and replace it with our own go_package declaration. add replace statement in go.mod in order to make go module looking for local resource instead of remote.
1 parent 806becc commit 46d1457

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ gripmock
33
.idea
44
.DS_Store
55
protogen
6+
temp

example/multi-package/foo.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ syntax = "proto3";
44
// but different package
55
package foo;
66

7-
option go_package = "github.com/tokopedia/gripmock/example/multi-package";
7+
// simulating dummy private repo
8+
option go_package = "github.com/my/private/repo/multi-package";
89

910
message Response {
1011
string response = 1;

fix_gopackage.sh

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,39 @@ protos=("$@")
44

55
for proto in "${protos[@]}"
66
do
7-
if grep '^option go_package' $proto;then
8-
echo "option go_package detected in $proto, no need to append"
9-
exit 1
10-
fi
11-
done
12-
13-
for proto in "${protos[@]}"
14-
do
7+
# if it's a directory then skip
158
if [[ -d $proto ]]; then
169
continue
1710
fi
1811

12+
# example $proto: example/foo/bar/hello.proto
13+
14+
# get string from left until the last /
15+
# example value: example/foo/bar/
1916
dir=${proto%/*}
17+
18+
# get string from right until the first /
19+
# example value: hello.proto
2020
file=${proto##*/}
2121

2222
newdir="protogen/$dir"
2323
newfile="$newdir/$file"
24+
2425
# copy to protogen directory
25-
mkdir -p "$newdir" && cp "$proto" "$_"
26+
mkdir -p "$newdir" && \
27+
cp "$proto" "$_" && \
28+
29+
# Force remove any declaration of go_package
30+
# then replace it with our own declaration below
31+
sed -i 's/^option go_package.*$//g' $newfile
2632

33+
34+
# get the line number of "syntax" declaration
2735
syntaxLineNum="$(grep -n "syntax" "$newfile" | head -n 1 | cut -d: -f1)"
2836

2937
goPackageString="option go_package = \"github.com/tokopedia/gripmock/protogen/$dir\";"
38+
39+
# append our own go_package delcaration just below "syntax" declaration
3040
sed -i "${syntaxLineNum}s~$~\n$goPackageString~" $newfile
3141
echo $newfile
3242
done

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/kr/pretty v0.2.0 // indirect
1010
github.com/lithammer/fuzzysearch v1.1.1
1111
github.com/stretchr/testify v1.6.1
12+
github.com/tokopedia/gripmock/protogen v0.0.0 // indirect
1213
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
1314
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd // indirect
1415
golang.org/x/text v0.3.4 // indirect
@@ -19,3 +20,5 @@ require (
1920
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2021
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
2122
)
23+
24+
replace github.com/tokopedia/gripmock/protogen v0.0.0 => ./protogen

0 commit comments

Comments
 (0)