Created
December 9, 2019 14:55
-
-
Save clcollins/41b95c134cd7c94a7fad34328f26080c to your computer and use it in GitHub Desktop.
Print all or an index of $GOPATH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Without an argument, prints the current gopath | |
# With a integer argument, prints the directory at the index of a multi-directory GOPATH | |
gopath () | |
{ | |
if [ -z "$GOPATH" ]; then | |
echo "GOPATH not set..."; | |
exit 1; | |
fi; | |
local gopath="${GOPATH}"; | |
local max_index="$(awk -F: '{ print NF }' <<< \"${gopath}\")"; | |
local re="^[0-9]+$"; | |
if [[ "${1}" =~ ${re} ]]; then | |
local index="${1}"; | |
else | |
printf "${gopath}\n"; | |
return; | |
fi; | |
((index++)); | |
if [ "${index}" -gt "${max_index}" ]; then | |
printf "ERROR: index out of range\n"; | |
fi; | |
awk -v ind=${index} '{split($0, gopaths, ":"); print gopaths[ind]}' <<< "${gopath}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment