Skip to content

Commit

Permalink
Add script to manage go.mod and modules.txt
Browse files Browse the repository at this point in the history
**What**
- Add scripting that handles copying the function's go.mod to the
  function root. This requires several mutations to handle renaming the
  module and dealig with local replacements of the function
- Add scriptin to handle the vendor/modules.txt. It also handles
  mutations to deal with the local replacement of the function code

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored and alexellis committed May 5, 2021
1 parent 71255a6 commit 067382d
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions template/golang-http/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,43 @@ ARG GOFLAGS=""
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
RUN if [ -d ./function/vendor ]; then \
echo "moving handler vendor" && \
mv -f ./function/vendor .; \
mv -f ./function/vendor .;\
else \
echo "using modules or vendor not found "; \
echo "vendor not found "; \
fi

RUN if [ "${GO111MODULE}" == 'on' ]; then \
# copy the user's go.mod
mv -f ./function/go.mod . && \
mv -f ./function/go.sum . && \
# clean up the go.mod,
# remove any local require for the handler/function, this can exist _if_ the
# developer has subpackages in their handler. It is ok to just remove it
# because we will replace it later
grep -v "handler/function" go.mod > gomod2; mv gomod2 go.mod && \
# now update the go.mod
# first, replace handler/function to point at the local code
# second, we need to rename the module to handler to match our main
go mod edit \
-replace=handler/function=./function \
-module handler && \
cat go.mod && \
if [ -d ./vendor ]; then \
# when vendored, we need to do similar edits to the vendor/modules.txt
# first we need to replace any possible copy of the handler code
rm -rf vendor/handler && \
# in modules.txt, we remove existing references to the handler/function
# these are replaced later with the new structure
grep -v "handler/function" ./vendor/modules.txt> modulestext; mv modulestext ./vendor/modules.txt && \
# add the mising replace to the vendor/modules.txt
echo "## explicit" >> ./vendor/modules.txt && \
echo "# handler/function => ./function" >> ./vendor/modules.txt && \
cat ./vendor/modules.txt; \
else \
echo "skip adding replace to ' ./vendor/modules.txt'"; \
fi \
else \
echo "skip go.mod handling"; \
fi

# Run a gofmt and exclude all vendored code.
Expand Down

0 comments on commit 067382d

Please sign in to comment.