Skip to content

Commit

Permalink
Add temporary GOPATH script for codegen
Browse files Browse the repository at this point in the history
This commit copies the "setup-temporary-gopath.sh" script from Pipelines,
to allow running codegen scripts when your GOPATH isn't set or is different
from the repo root.
  • Loading branch information
lbernick authored and tekton-robot committed Sep 1, 2022
1 parent 766c0b3 commit 27f8a33
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
85 changes: 85 additions & 0 deletions hack/setup-temporary-gopath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset

# Conditionally create a temporary GOPATH for codegen
# and openapigen to execute in. This is only done if
# the current repo directory is not within GOPATH.
function shim_gopath() {
local REPO_DIR=$(git rev-parse --show-toplevel)
local TEMP_GOPATH="${REPO_DIR}/.gopath"
local TEMP_TEKTONCD="${TEMP_GOPATH}/src/github.com/tektoncd"
local TEMP_TRIGGERS="${TEMP_TEKTONCD}/triggers"
local NEEDS_MOVE=1

# Checks if GOPATH exists without triggering nounset panic.
EXISTING_GOPATH=${GOPATH:-}

# Check if repo is in GOPATH already and return early if so.
# Unfortunately this doesn't respect a repo that's symlinked into
# GOPATH and will create a temporary anyway. I couldn't figure out
# a way to get the absolute path to the symlinked repo root.
if [ ! -z $EXISTING_GOPATH ] ; then
case $REPO_DIR/ in
$EXISTING_GOPATH/*) NEEDS_MOVE=0;;
*) NEEDS_MOVE=1;;
esac
fi

if [ $NEEDS_MOVE -eq 0 ]; then
return
fi

echo "You appear to be running from outside of GOPATH."
echo "This script will create a temporary GOPATH at $TEMP_GOPATH for code generation."

# Ensure that the temporary triggers symlink doesn't exist
# before proceeding.
delete_triggers_repo_symlink

mkdir -p "$TEMP_TEKTONCD"
# This will create a symlink from
# (repo-root)/.gopath/src/github.com/tektoncd/triggers
# to the user's triggers checkout.
ln -s "$REPO_DIR" "$TEMP_TRIGGERS"
echo "Moving to $TEMP_TRIGGERS"
cd "$TEMP_TRIGGERS"
export GOPATH="$TEMP_GOPATH"
}

# Helper that wraps deleting the temp triggers repo symlink
# and prints a message about deleting the temp GOPATH.
#
# Why doesn't this func just delete the temp GOPATH outright?
# Because it might be reused across multiple hack scripts and many
# packages seem to be installed into GOPATH with read-only
# permissions, requiring sudo to delete the directory. Rather
# than surprise the dev with a password entry at the end of the
# script's execution we just print a message to let them know.
function shim_gopath_clean() {
local REPO_DIR=$(git rev-parse --show-toplevel)
local TEMP_GOPATH="${REPO_DIR}/.gopath"
if [ -d "$TEMP_GOPATH" ] ; then
# Put the user back at the root of the triggers repo
# after all the symlink shenanigans.
echo "Moving to $REPO_DIR"
cd "$REPO_DIR"
delete_triggers_repo_symlink
echo "When you are finished with codegen you can safely run the following:"
echo "sudo rm -rf \".gopath\""
fi
}

# Delete the temp symlink to triggers repo from the temp GOPATH dir.
function delete_triggers_repo_symlink() {
local REPO_DIR=$(git rev-parse --show-toplevel)
local TEMP_GOPATH="${REPO_DIR}/.gopath"
if [ -d "$TEMP_GOPATH" ] ; then
local REPO_SYMLINK="${TEMP_GOPATH}/src/github.com/tektoncd/triggers"
if [ -L $REPO_SYMLINK ] ; then
echo "Deleting symlink to triggers repo $REPO_SYMLINK"
rm -f "${REPO_SYMLINK}"
fi
fi
}
4 changes: 4 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ set -o errexit
set -o nounset
set -o pipefail

source $(git rev-parse --show-toplevel)/hack/setup-temporary-gopath.sh
shim_gopath
trap shim_gopath_clean EXIT

source $(git rev-parse --show-toplevel)/vendor/github.com/tektoncd/plumbing/scripts/library.sh

PREFIX=${GOBIN:-${GOPATH}/bin}
Expand Down
4 changes: 4 additions & 0 deletions hack/update-openapigen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
set -o errexit
set -o nounset

source $(git rev-parse --show-toplevel)/hack/setup-temporary-gopath.sh
shim_gopath
trap shim_gopath_clean EXIT

source $(git rev-parse --show-toplevel)/vendor/github.com/tektoncd/plumbing/scripts/library.sh

cd "${REPO_ROOT_DIR}"
Expand Down

0 comments on commit 27f8a33

Please sign in to comment.