|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit |
| 4 | +set -o nounset |
| 5 | + |
| 6 | +# Conditionally create a temporary GOPATH for codegen |
| 7 | +# and openapigen to execute in. This is only done if |
| 8 | +# the current repo directory is not within GOPATH. |
| 9 | +function shim_gopath() { |
| 10 | + local REPO_DIR=$(git rev-parse --show-toplevel) |
| 11 | + local TEMP_GOPATH="${REPO_DIR}/.gopath" |
| 12 | + local TEMP_TEKTONCD="${TEMP_GOPATH}/src/github.com/tektoncd" |
| 13 | + local TEMP_PIPELINE="${TEMP_TEKTONCD}/pipeline" |
| 14 | + local NEEDS_MOVE=1 |
| 15 | + |
| 16 | + # Check if repo is in GOPATH already and return early if so. |
| 17 | + # Unfortunately this doesn't respect a repo that's symlinked into |
| 18 | + # GOPATH and will create a temporary anyway. I couldn't figure out |
| 19 | + # a way to get the absolute path to the symlinked repo root. |
| 20 | + if [ ! -z $GOPATH ] ; then |
| 21 | + case $REPO_DIR/ in |
| 22 | + $GOPATH/*) NEEDS_MOVE=0;; |
| 23 | + *) NEEDS_MOVE=1;; |
| 24 | + esac |
| 25 | + fi |
| 26 | + |
| 27 | + if [ $NEEDS_MOVE -eq 0 ]; then |
| 28 | + return |
| 29 | + fi |
| 30 | + |
| 31 | + echo "You appear to be running from outside of GOPATH." |
| 32 | + echo "This script will create a temporary GOPATH at $TEMP_GOPATH for code generation." |
| 33 | + |
| 34 | + # Ensure that the temporary pipelines symlink doesn't exist |
| 35 | + # before proceeding. |
| 36 | + delete_pipeline_repo_symlink |
| 37 | + |
| 38 | + mkdir -p "$TEMP_TEKTONCD" |
| 39 | + # This will create a symlink from |
| 40 | + # (repo-root)/.gopath/src/github.com/tektoncd/pipeline |
| 41 | + # to the user's pipeline checkout. |
| 42 | + ln -s "$REPO_DIR" "$TEMP_TEKTONCD" |
| 43 | + echo "Moving to $TEMP_PIPELINE" |
| 44 | + cd "$TEMP_PIPELINE" |
| 45 | + export GOPATH="$TEMP_GOPATH" |
| 46 | +} |
| 47 | + |
| 48 | +# Helper that wraps deleting the temp pipelines repo symlink |
| 49 | +# and prints a message about deleting the temp GOPATH. |
| 50 | +# |
| 51 | +# Why doesn't this func just delete the temp GOPATH outright? |
| 52 | +# Because it might be reused across multiple hack scripts and many |
| 53 | +# packages seem to be installed into GOPATH with read-only |
| 54 | +# permissions, requiring sudo to delete the directory. Rather |
| 55 | +# than surprise the dev with a password entry at the end of the |
| 56 | +# script's execution we just print a message to let them know. |
| 57 | +function shim_gopath_clean() { |
| 58 | + local REPO_DIR=$(git rev-parse --show-toplevel) |
| 59 | + local TEMP_GOPATH="${REPO_DIR}/.gopath" |
| 60 | + if [ -d "$TEMP_GOPATH" ] ; then |
| 61 | + # Put the user back at the root of the pipelines repo |
| 62 | + # after all the symlink shenanigans. |
| 63 | + echo "Moving to $REPO_DIR" |
| 64 | + cd "$REPO_DIR" |
| 65 | + delete_pipeline_repo_symlink |
| 66 | + echo "When you are finished with codegen you can safely run the following:" |
| 67 | + echo "sudo rm -rf \".gopath\"" |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +# Delete the temp symlink to pipelines repo from the temp GOPATH dir. |
| 72 | +function delete_pipeline_repo_symlink() { |
| 73 | + local REPO_DIR=$(git rev-parse --show-toplevel) |
| 74 | + local TEMP_GOPATH="${REPO_DIR}/.gopath" |
| 75 | + if [ -d "$TEMP_GOPATH" ] ; then |
| 76 | + local REPO_SYMLINK="${TEMP_GOPATH}/src/github.com/tektoncd/pipeline" |
| 77 | + if [ -L $REPO_SYMLINK ] ; then |
| 78 | + echo "Deleting symlink to pipelines repo $REPO_SYMLINK" |
| 79 | + rm -f "${REPO_SYMLINK}" |
| 80 | + fi |
| 81 | + fi |
| 82 | +} |
0 commit comments