Skip to content

Commit

Permalink
Force replace go_package
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jekiapp committed Jun 13, 2022
1 parent 806becc commit 46d1457
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ gripmock
.idea
.DS_Store
protogen
temp
3 changes: 2 additions & 1 deletion example/multi-package/foo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ syntax = "proto3";
// but different package
package foo;

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

message Response {
string response = 1;
Expand Down
28 changes: 19 additions & 9 deletions fix_gopackage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,39 @@ protos=("$@")

for proto in "${protos[@]}"
do
if grep '^option go_package' $proto;then
echo "option go_package detected in $proto, no need to append"
exit 1
fi
done

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%/*}

# 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" "$_"
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
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/kr/pretty v0.2.0 // indirect
github.com/lithammer/fuzzysearch v1.1.1
github.com/stretchr/testify v1.6.1
github.com/tokopedia/gripmock/protogen v0.0.0 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd // indirect
golang.org/x/text v0.3.4 // indirect
Expand All @@ -19,3 +20,5 @@ require (
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)

replace github.com/tokopedia/gripmock/protogen v0.0.0 => ./protogen

0 comments on commit 46d1457

Please sign in to comment.