Last active
April 11, 2018 09:16
-
-
Save ukautz/afd970cdac272edcf1bb54cba64a2822 to your computer and use it in GitHub Desktop.
Console helper to set Go(lang) path variables from current directory using `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
#!/bin/bash | |
#--------------- | |
# | |
# Installation: | |
# $ curl -Lo ~/.godev http://tiny.cc/godev | |
# $ echo "source ~/.godev" >> ~/.$(basename $SHELL)rc | |
# | |
# Usage: | |
# # Tries to determine go workspace directory, then | |
# # exports it into GOPATH environment variable | |
# $ gopath | |
# | |
# # Creates a new go workspace, including all sub-directores | |
# $ create-go-project github.com/mee/myapp | |
#--------------- | |
function _find_gopath() { | |
function _errout() { | |
>&2 printf "$1\n" | |
} | |
function _ok() { | |
_errout "\033[1m❤ ❤ ❤ \033[1;32m$1\033[1;37m ❤ ❤ ❤\033[0m" | |
print $1 | |
} | |
function _die() { | |
_errout "\033[1m❢ ❢ ❢ \033[1;31mFailed to detect GOPATH\033[1;37m ❢ ❢ ❢\033[0m" | |
_errout | |
print "NOT_FOUND" | |
} | |
c=$(pwd) | |
for i in $(seq 1 10); do | |
p=$(pwd) | |
if [ -d "$p/src/github.com" ] \ | |
|| [ -d "$p/src/bitbucket.org" ] \ | |
|| [ -d "$p/src/gopkg.in" ] \ | |
|| [ -d "$p/src/${GOCOMPANY:-git.yourcompany.com}" ] | |
then | |
cd $c | |
_ok $p | |
return | |
fi | |
cd .. | |
done | |
cd $c | |
_die | |
} | |
function _create_go_project() { | |
if [ -z $1 ]; then | |
echo "Usage: create-go-project <package>" | |
echo " Eg: create-go-project github.com/your/library" | |
else | |
package=$1 | |
if [[ $(basename $package) == $package ]]; then | |
vendor=${GITHUB_VENDOR:$USER} | |
name=$package | |
package=$(echo $package | tr '[:upper:]' '[:lower:]') | |
package="github.com/$vendor/$package" | |
else | |
name=$(basename $package | sed -r 's/(^|-|_)(\w)/\U\2/g') | |
fi | |
mkdir -p "$name/src/$package" | |
echo "Created $name/src/$package" | |
fi | |
} | |
alias gopath='export GOPATH=$(_find_gopath)' | |
alias create-go-project='_create_go_project' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment