├── bin └── gobrew ├── .gitignore ├── libexec ├── gobrew---version ├── gobrew-workspace-file ├── gobrew-versions ├── gobrew-shims ├── gobrew-workspace-file-read ├── gobrew-uninstall ├── gobrew-use ├── gobrew-version-file-read ├── gobrew-version-file ├── gobrew-list ├── gobrew-version ├── gobrew-workspace ├── gobrew-exec ├── gobrew ├── gobrew-init ├── gobrew-install ├── gobrew-rehash └── gobrew-help ├── tools ├── uninstall.sh ├── upgrade.sh └── install.sh ├── LICENSE └── README.md /bin/gobrew: -------------------------------------------------------------------------------- 1 | ../libexec/gobrew -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | versions/* 3 | version 4 | workspace 5 | shims/* 6 | cache/* 7 | -------------------------------------------------------------------------------- /libexec/gobrew---version: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | version="0.0.5" 4 | echo "version $version" 5 | -------------------------------------------------------------------------------- /tools/uninstall.sh: -------------------------------------------------------------------------------- 1 | echo "Removing ~/.gobrew" 2 | if [ -d ~/.gobrew ] 3 | then 4 | rm -rf ~/.gobrew 5 | fi 6 | 7 | echo "\n\033[0;32mThanks for using gobrew. It's been uninstalled.\033[0m" 8 | -------------------------------------------------------------------------------- /libexec/gobrew-workspace-file: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | global_workspace_file="${GOBREW_ROOT}/workspace" 6 | 7 | if [ -e "$global_workspace_file" ]; then 8 | echo "$global_workspace_file" 9 | fi 10 | -------------------------------------------------------------------------------- /tools/upgrade.sh: -------------------------------------------------------------------------------- 1 | cd "`dirname $0`" 2 | 3 | if git pull --rebase --stat origin master 4 | then 5 | printf '\033[0;34m%s\033[0m' 'Hooray! Gobrew has been updated and/or is at the current version.' 6 | else 7 | printf '\033[0;31m%s\033[0m' 'There was an error updating. Try again later?' 8 | fi 9 | -------------------------------------------------------------------------------- /libexec/gobrew-versions: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Display all versions of Go installed. 3 | 4 | # Bomb out if we hit an error, ever 5 | set -e 6 | 7 | # Iterate over installed versions and write to output 8 | if [ -d "/usr/local/go" ]; then 9 | echo "system" 10 | fi 11 | for path in "${GOBREW_ROOT}/versions/"*; do 12 | if [ -d "$path" ]; then 13 | echo "${path##*/}" 14 | fi 15 | done 16 | -------------------------------------------------------------------------------- /libexec/gobrew-shims: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: List existing gobrew shims 3 | # Usage: gobrew shims [--short] 4 | 5 | set -e 6 | 7 | # Provide gobrew completions 8 | if [ "$1" = "--complete" ]; then 9 | echo --short 10 | exit 11 | fi 12 | 13 | shopt -s nullglob 14 | 15 | for command in "${GOBREW_ROOT}/shims/"*; do 16 | if [ "$1" = "--short" ]; then 17 | echo "${command##*/}" 18 | else 19 | echo "$command" 20 | fi 21 | done | sort 22 | -------------------------------------------------------------------------------- /libexec/gobrew-workspace-file-read: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | WORKSPACE_FILE="$1" 6 | 7 | if [ -e "$WORKSPACE_FILE" ]; then 8 | workspace="" 9 | while read -a words; do 10 | words="${words[0]}" 11 | if [ -z "$workspace" ] && [ -n "$words" ]; then 12 | workspace="$words" 13 | fi 14 | done < <( cat "$WORKSPACE_FILE" && echo ) 15 | 16 | if [ -n "$workspace" ]; then 17 | echo "$workspace" 18 | exit 19 | fi 20 | fi 21 | 22 | exit 1 23 | -------------------------------------------------------------------------------- /libexec/gobrew-uninstall: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Uninstall a version of Go. 3 | # 4 | # Usage: gobrew uninstall 5 | # 6 | # Versions should be in the form of N.N.N 7 | 8 | # Bomb out if we hit an error, ever 9 | set -e 10 | 11 | # Pull the desired version out of ARGV 12 | version="$1" 13 | version_dir="$GOBREW_ROOT/versions/$version" 14 | 15 | if [ -d "$version_dir" ]; then 16 | rm -rf "$version_dir" 17 | echo "Uninstalled $version" 18 | else 19 | echo "gobrew: version \`$version' isn't installed" >&2 20 | exit 1 21 | fi 22 | -------------------------------------------------------------------------------- /libexec/gobrew-use: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Specify which go version to use. 3 | 4 | set -e 5 | 6 | version="$1" 7 | VERSIONS=`ls -l $GOBREW_ROOT/versions | egrep '^d' | awk '{print $9}'` 8 | 9 | for v in $VERSIONS 10 | do 11 | if [ -z $version ]; then 12 | echo "gobrew: please specifiy version to use." 13 | exit 1 14 | elif [ "$v" = $version ]; then 15 | echo $version > "$GOBREW_ROOT/version" 16 | exit 0 17 | fi 18 | done 19 | 20 | if [ "system" = $version ] && [ -d "/usr/local/go" ]; then 21 | echo $version > "$GOBREW_ROOT/version" 22 | exit 0 23 | fi 24 | 25 | echo "gobrew: $version not installed." 26 | exit 1 27 | -------------------------------------------------------------------------------- /libexec/gobrew-version-file-read: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Usage: gobrew version-file-read 3 | 4 | # Bomb out if we hit an error, ever 5 | set -e 6 | 7 | VERSION_FILE="$1" 8 | 9 | if [ -e "$VERSION_FILE" ]; then 10 | # Read the first non-whitespace word from the specified version file. 11 | # Be careful not to load it whole in case there's something crazy in it. 12 | version="" 13 | while read -a words; do 14 | word="${words[0]}" 15 | if [ -z "$version" ] && [ -n "$word" ]; then 16 | version="$word" 17 | fi 18 | done < <( cat "$VERSION_FILE" && echo ) 19 | 20 | if [ -n "$version" ]; then 21 | echo "$version" 22 | exit 23 | fi 24 | fi 25 | 26 | exit 1 27 | -------------------------------------------------------------------------------- /libexec/gobrew-version-file: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Detect the file that sets the current gobrew version 3 | 4 | # Bomb out if we hit an error, ever 5 | set -e 6 | 7 | find_local_version_file() { 8 | local root="$1" 9 | while [ -n "$root" ]; do 10 | if [ ! -d "${root}/gobrew_version" ] && [ -e "${root}/gobrew_version" ]; then 11 | echo "${root}/gobrew_version" 12 | exit 13 | fi 14 | root="${root%/*}" 15 | done 16 | } 17 | 18 | find_local_version_file "$GOBREW_DIR" 19 | [ "$GOBREW_DIR" = "$PWD" ] || find_local_version_file "$PWD" 20 | 21 | global_version_file="${GOBREW_ROOT}/version" 22 | 23 | if [ -e "$global_version_file" ]; then 24 | echo "$global_version_file" 25 | fi 26 | -------------------------------------------------------------------------------- /libexec/gobrew-list: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | # Summary: List available versions of go. example: gobrew list 3 | 4 | 5 | set -e 6 | 7 | platform="$(uname -s | tr '[:upper:]' '[:lower:]')" 8 | 9 | if [ "$(uname -m)" = "x86_64" ]; then 10 | arch="amd64" 11 | else 12 | arch="386" 13 | fi 14 | 15 | echo "finding Go versions for $platform-$arch" 16 | 17 | # Allow the GO_DOWNLOADS_URL_LIST environment variable override the location of the 18 | # go installation files 19 | url=${GO_DOWNLOADS_LIST_URL:=http://golang.org/dl/} 20 | go_downloads_list=$url 21 | curl -sL $go_downloads_list \ 22 | | grep -o "&2 24 | exit 1 25 | elif version_exists "$GOBREW_VERSION"; then 26 | echo "$GOBREW_VERSION" 27 | elif no_versions; then 28 | echo "gobrew: no versions of Go are currently installed" >&2 29 | exit 1 30 | else 31 | echo "gobrew: version \`$GOBREW_VERSION' is not installed" >&2 32 | exit 1 33 | fi 34 | -------------------------------------------------------------------------------- /tools/install.sh: -------------------------------------------------------------------------------- 1 | if [ -d "$HOME/.gobrew" ] 2 | then 3 | echo -e "\033[0;33mYou already have gobrew installed.\033[0m You'll need to remove ~/.gobrew if you want to install" 4 | exit 5 | fi 6 | 7 | echo -e "\n\033[0;34mCloning gobrew...\033[0m" 8 | 9 | hash git >/dev/null && /usr/bin/env git clone https://github.com/cryptojuice/gobrew.git ~/.gobrew || { 10 | echo "You have to install git first" 11 | exit 12 | } 13 | 14 | shell="$1" 15 | if [ -z "$shell" ]; then 16 | shell="$(basename "$SHELL")" 17 | fi 18 | 19 | export_command='export PATH="$HOME/.gobrew/bin:$PATH"' 20 | eval_command='eval "$(gobrew init -)"' 21 | 22 | case "$shell" in 23 | bash ) 24 | profile=bash_profile 25 | ;; 26 | zsh ) 27 | profile=zshrc 28 | ;; 29 | ksh ) 30 | profile=profile 31 | ;; 32 | esac 33 | 34 | echo "${export_command}" >> "$HOME/.$profile" 35 | echo "${eval_command}" >> "$HOME/.$profile" 36 | 37 | source "$HOME/.$profile" 38 | 39 | echo "Gobrew is now installed." 40 | echo "Run 'gobrew' command for information." 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Gilbert Robinson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /libexec/gobrew-workspace: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Run 'gobrew workspace set' from your workspace folder to set as $GOPATH. 3 | # 4 | # Usage: 'gobrew workspace' 'gobrew workspace set' 'gobrew workspace unset' 5 | 6 | set -e 7 | 8 | if [ -z "${GOBREW_WORKSPACE}" ]; then 9 | GOBREW_WORKSPACE_FILE="$(gobrew-workspace-file)" 10 | GOBREW_WORKSPACE="$(gobrew-workspace-file-read "$GOBREW_WORKSPACE_FILE" || true)" 11 | fi 12 | 13 | 14 | if [ -n "$1" ]; then 15 | if [ "$1" = "set" ]; then 16 | 17 | $(cat /dev/null > $GOBREW_WORKSPACE_FILE) 18 | echo "$(pwd)" >> "$GOBREW_WORKSPACE_FILE" 19 | echo 'export GOPATH="'$(pwd)'"' 20 | echo 'export PATH="'$(pwd)'/bin:$PATH"' 21 | echo "gobrew: Please close and repoen terminal window." 22 | exit 1 23 | elif [ "$1" = "unset" ];then 24 | 25 | $(cat /dev/null > $GOBREW_WORKSPACE_FILE) 26 | echo "gobrew: Go workspace cleared." 27 | unset GOPATH 28 | exit 1 29 | fi 30 | fi 31 | 32 | workspace_exists() { 33 | local workspace="$1" 34 | [ -d "${workspace}" ] 35 | } 36 | 37 | if workspace_exists "$GOBREW_WORKSPACE"; then 38 | echo "$GOBREW_WORKSPACE" 39 | fi 40 | -------------------------------------------------------------------------------- /libexec/gobrew-exec: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Summary: Execute a command from a particular Go version. 4 | # 5 | # Usage: gobrew exec [] 6 | # 7 | # Puts the current Go version onto PATH and runs the specified command. 8 | 9 | # Bomb out if we hit an error, ever 10 | set -e 11 | 12 | # Set the current Go version 13 | if [ -z "$GOBREW_VERSION" ]; then 14 | GOBREW_VERSION="$(gobrew version)" 15 | 16 | # Couldn't infer from a file either 17 | if [ -z "$GOBREW_VERSION" ]; then 18 | echo "gobrew: no \`GOBREW_VERSION' configured" >&2 19 | exit 1 20 | fi 21 | fi 22 | 23 | # Set the current Go workspace 24 | if [ -z "$GOBREW_WORKSPACE" ]; then 25 | GOBREW_WORKSPACE="$(gobrew workspace)" 26 | fi 27 | 28 | GOROOT="$GOBREW_ROOT/versions/$GOBREW_VERSION" 29 | 30 | if [ -n "$GOBREW_WORKSPACE" ]; then 31 | GOPATH="$GOBREW_WORKSPACE" 32 | export GOPATH 33 | export PATH="$GOPATH/bin:$PATH" 34 | fi 35 | 36 | # if the specified Go version lacks this bin, bail 37 | if [ ! -x "$GOROOT/bin/$1" ]; then 38 | echo "gobrew: \`$1' does not exist for $GOBREW_VERSION" >&2 39 | 40 | for version in "$(gobrew versions)"; do 41 | if [ -x "$GOBREW_ROOT/versions/$version/bin/$1" ]; then 42 | echo " $version" >&2 43 | fi 44 | done 45 | 46 | exit 1 47 | fi 48 | 49 | export GOBREW_VERSION GOROOT 50 | 51 | # Put our bindir onto PATH 52 | export PATH="$GOROOT/bin:$PATH" 53 | 54 | exec "$@" 55 | -------------------------------------------------------------------------------- /libexec/gobrew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | resolve_link() { 5 | $(type -p greadlink readlink | head -1) "$1" 6 | } 7 | 8 | abs_dirname() { 9 | local cwd="$(pwd)" 10 | local path="$1" 11 | 12 | while [ -n "$path" ]; do 13 | cd "${path%/*}" 14 | local name="${path##*/}" 15 | path="$(resolve_link "$name" || true)" 16 | done 17 | 18 | pwd 19 | cd "$cwd" 20 | } 21 | 22 | if [ -z "${GOBREW_ROOT}" ]; then 23 | GOBREW_ROOT="${HOME}/.gobrew" 24 | else 25 | GOBREW_ROOT="${GOBREW_ROOT%/}" 26 | fi 27 | export GOBREW_ROOT 28 | 29 | if [ -z "${GOBREW_DIR}" ]; then 30 | GOBREW_DIR="$(pwd)" 31 | else 32 | cd "$GOBREW_DIR" 2>/dev/null || { 33 | echo "gobrew: cannot change working directory to \`$GOBREW_DIR'" 34 | exit 1 35 | } >&2 36 | GOBREW_DIR="$(pwd)" 37 | cd "$OLDPWD" 38 | fi 39 | export GOBREW_DIR 40 | 41 | 42 | shopt -s nullglob 43 | 44 | bin_path="$(abs_dirname "$0")" 45 | for plugin_bin in "${GOBREW_ROOT}/plugins/"*/bin; do 46 | bin_path="${bin_path}:${plugin_bin}" 47 | done 48 | export PATH="${bin_path}:${PATH}" 49 | 50 | hook_path="${GOBREW_HOOK_PATH}:${GOBREW_ROOT}/gobrew.d:/usr/local/etc/gobrew.d:/etc/gobrew.d:/usr/lib/gobrew/hooks" 51 | for plugin_hook in "${GOBREW_ROOT}/plugins/"*/etc/gobrew.d; do 52 | hook_path="${hook_path}:${plugin_hook}" 53 | done 54 | export GOBREW_HOOK_PATH="$hook_path" 55 | 56 | shopt -u nullglob 57 | 58 | 59 | command="$1" 60 | case "$command" in 61 | "" | "-h" | "--help" ) 62 | echo -e "$(gobrew---version)\n$(gobrew-help)" >&2 63 | ;; 64 | "-v" ) 65 | exec gobrew---version 66 | ;; 67 | * ) 68 | command_path="$(command -v "gobrew-$command" || true)" 69 | if [ -z "$command_path" ]; then 70 | echo "gobrew: no such command \`$command'" >&2 71 | exit 1 72 | fi 73 | 74 | shift 1 75 | exec "$command_path" "$@" 76 | ;; 77 | esac 78 | -------------------------------------------------------------------------------- /libexec/gobrew-init: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Configure the shell environment for gobrew 3 | # Usage: eval "$(gobrew init - [])" 4 | 5 | # Bomb out if we hit an error, ever 6 | set -e 7 | 8 | # Figure out what we're gonna print 9 | print="" 10 | for args in "$@"; do 11 | if [ "$args" = "-" ]; then 12 | print=1 13 | shift 14 | fi 15 | done 16 | 17 | # Check for a shell 18 | shell="$1" 19 | if [ -z "$shell" ]; then 20 | shell="$(basename "$SHELL")" 21 | fi 22 | 23 | # Helpers to get full path 24 | resolve_link() { 25 | $(type -p greadlink readlink | head -1) $1 26 | } 27 | 28 | abs_dirname() { 29 | local cwd="$(pwd)" 30 | local path="$1" 31 | 32 | while [ -n "$path" ]; do 33 | cd "${path%/*}" 34 | local name="${path##*/}" 35 | path="$(resolve_link "$name" || true)" 36 | done 37 | 38 | pwd 39 | cd "$cwd" 40 | } 41 | 42 | # Set the root of our gobrew install 43 | root="$(abs_dirname "$0")/.." 44 | 45 | if [ -z "$print" ]; then 46 | case "$shell" in 47 | bash ) 48 | profile='~/.bash_profile' 49 | ;; 50 | zsh ) 51 | profile='~/.zshrc' 52 | ;; 53 | ksh ) 54 | profile='~/.profile' 55 | ;; 56 | * ) 57 | profile='your profile' 58 | ;; 59 | esac 60 | 61 | { echo "# Load gobrew automatically by adding" 62 | echo "# the following to ${profile}:" 63 | echo 64 | echo 'eval "$(gobrew init -)"' 65 | echo 66 | } >&2 67 | 68 | exit 1 69 | fi 70 | 71 | # Set push workspace into PATH on init 72 | if [ -z "${GOBREW_WORKSPACE}" ]; then 73 | GOBREW_WORKSPACE_FILE="$(gobrew-workspace-file)" 74 | GOBREW_WORKSPACE="$(gobrew-workspace-file-read "$GOBREW_WORKSPACE_FILE" || true)" 75 | if [[ ":${PATH}:" != *:"${GOPATH}":* ]]; then 76 | echo 'export GOPATH="'${GOBREW_WORKSPACE}'"' 77 | echo 'export PATH="'${GOBREW_WORKSPACE}'/bin:$PATH"' 78 | fi 79 | fi 80 | 81 | 82 | 83 | mkdir -p "${GOBREW_ROOT}/"{shims,versions,cache} 84 | touch "${GOBREW_ROOT}/"{version,workspace} 85 | 86 | if [[ ":${PATH}:" != *:"${GOBREW_ROOT}/shims":* ]]; then 87 | echo 'export PATH="'${GOBREW_ROOT}'/shims:${PATH}"' 88 | fi 89 | 90 | echo 'gobrew rehash 2>/dev/null' 91 | -------------------------------------------------------------------------------- /libexec/gobrew-install: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | # Summary: Install a version of go. example: gobrew install 1.5 3 | 4 | 5 | set -e 6 | 7 | version="$1" 8 | version_dir="$GOBREW_ROOT/versions/$version" 9 | 10 | OLDPWD=$(pwd) 11 | 12 | mkdir -p "$version_dir" 13 | cd "$version_dir" 14 | 15 | platform="$(uname -s | tr '[:upper:]' '[:lower:]')" 16 | 17 | 18 | 19 | if [ "$(uname -m)" = "x86_64" ]; then 20 | arch="amd64" 21 | else 22 | arch="386" 23 | fi 24 | 25 | if [ -z "$version" ]; then 26 | echo "Must specify version" 27 | exit 1 28 | fi 29 | 30 | # Allow the GO_FILES_URL environment variable override the location of the 31 | # go installation files 32 | files_url=${GO_FILES_URL:=https://storage.googleapis.com/golang/} 33 | 34 | # Check for Mac OS X. 35 | version_major="$(echo $version | cut -d . -f 1)" 36 | version_minor="$(echo $version | cut -d . -f 2)" 37 | 38 | if [ $platform = "darwin" -a $version_minor -gt 1 ]; then 39 | if [ $version_minor -gt 4 ]; then 40 | file_name="go${version}.${platform}-${arch}.tar.gz" 41 | else 42 | darwin_major="$(uname -r | cut -d . -f 1)" 43 | 44 | if [ $darwin_major -lt 11 ]; then 45 | file_name="go${version}.${platform}-${arch}-osx10.6.tar.gz" 46 | else 47 | file_name="go${version}.${platform}-${arch}-osx10.8.tar.gz" 48 | fi 49 | fi 50 | else 51 | file_name="go${version}.${platform}-${arch}.tar.gz" 52 | fi 53 | 54 | download=$files_url$file_name 55 | 56 | set +e 57 | 58 | ( 59 | if type curl &>/dev/null; then 60 | echo "Installing $file_name" 61 | curl -s -f "$download" > "$GOBREW_ROOT/cache/$file_name" 62 | tar -zxf "$GOBREW_ROOT/cache/$file_name" --strip-components 1 63 | elif type wget &>/dev/null; then 64 | echo "Installing $file_name" 65 | wget -P "$GOBREW_ROOT/cache" "${download}" 66 | tar -zxf "$GOBREW_ROOT/cache/$file_name" --strip-components 1 67 | else 68 | echo "error: please install \`curl\` or \`wget\` and try again" >&2 69 | exit 1 70 | fi 71 | rm "$GOBREW_ROOT/cache/$file_name" 72 | ) || \ 73 | { 74 | cd $OLDPWD 75 | rm -rf "$version_dir" 76 | 77 | echo "gobrew: unable to install ${version} from binary, download not available" 78 | exit 1 79 | } 80 | 81 | echo "Installed ${version}" 82 | cd $OLDPWD 83 | 84 | exec "${GOBREW_ROOT}/bin/gobrew" rehash 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gobrew 2 | ====== 3 | 4 | gobrew lets you easily switch between multiple versions of go. It is based on [rbenv](https://github.com/sstephenson/rbenv) and [pyenv](https://github.com/yyuu/pyenv). 5 | 6 | 7 | 8 | ### Installation 9 | --------------- 10 | 11 | #### The automatic installer 12 | ---------------------------- 13 | 14 | You can install this via the command line with either `curl` or `wget`. 15 | 16 | __via `curl`__ 17 | 18 | curl -L https://raw.github.com/grobins2/gobrew/master/tools/install.sh | sh 19 | 20 | __via `wget`__ 21 | 22 | wget --no-check-certificate https://raw.github.com/grobins2/gobrew/master/tools/install.sh -O - | sh 23 | 24 | 25 | #### The manual way 26 | ------------------ 27 | 28 | 1. Check out gobrew where you want it installed. 29 | 30 | $ git clone git://github.com/cryptojuice/gobrew.git ~/.gobrew 31 | 32 | 2. Add the following to your shell config. 33 | 34 | export PATH="$HOME/.gobrew/bin:$PATH" 35 | eval "$(gobrew init -)" 36 | Note: 37 | * BASH: Add this to ~/.bashrc (~/.bash_profile for Ubuntu users). 38 | 39 | * ZSH: Add this to ~/.zshenv 40 | 41 | 3. Source your shell config file (or reopen shell session). 42 | 43 | ### Commands 44 | ----------- 45 | 46 | ##### : gobrew install 47 | Install a specified version of Go. 48 | 49 | $ gobrew install 1.5 50 | 51 | ##### : gobrew uninstall 52 | $ gobrew uninstall 1.5 53 | 54 | ##### : gobrew use 55 | Sets which version of Go to use globally. 56 | 57 | $ gobrew use 1.5 58 | 59 | ##### : gobrew workspace 60 | Note: 'gobrew workspace' echos the currently set workspace ($GOPATH). Use 'gobrew workspace set' to set your $GOPATH to the current working directory. Use 'gobrew workspace unset' to remove this setting. 61 | 62 | $ cd /path/to/workspace 63 | $ gobrew workspace set 64 | $ gobrew workspace unset 65 | 66 | Visit [http://golang.org/doc/code.html#Workspaces](http://golang.org/doc/code.html#Workspaces) for more on workspaces. 67 | 68 | ### Useful 69 | 70 | #### Updates 71 | 72 | To upgrade run update script from .gobrew source with: 73 | $ cd ~ 74 | $ ./.gobrew/tools/upgrade.sh 75 | 76 | #### Uninstalling 77 | 78 | If you want to uninstall it, just run 79 | 80 | $ cd ~ 81 | $ ./.gobrew/tools/uninstall.sh 82 | 83 | from the command line and it’ll remove itself. 84 | 85 | -------------------------------------------------------------------------------- /libexec/gobrew-rehash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Rehash gobrew shims (run this after installing executables) 3 | 4 | # Bomb out if we hit an error, ever 5 | set -e 6 | 7 | # Create the shims directory if it doesn't already exist. 8 | SHIM_PATH="${GOBREW_ROOT}/shims" 9 | mkdir -p "$SHIM_PATH" 10 | 11 | # Ensure only one rehash is running at a time 12 | PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.gobrew-shim" 13 | 14 | set -o noclobber 15 | { echo > "$PROTOTYPE_SHIM_PATH" 16 | } 2>/dev/null || 17 | { echo "gobrew: cannot rehash: $PROTOTYPE_SHIM_PATH exists" 18 | exit 1 19 | } >&2 20 | set +o noclobber 21 | 22 | # Clean up our lockfile if we fail 23 | trap remove_prototype_shim EXIT 24 | 25 | remove_prototype_shim() { 26 | rm -f "$PROTOTYPE_SHIM_PATH" 27 | } 28 | 29 | # Helper to create shim bins 30 | create_prototype_shim() { 31 | cat > "$PROTOTYPE_SHIM_PATH" </dev/null 2>&1; then 65 | for shim in *; do rm -f "$shim"; done 66 | fi 67 | break 68 | done 69 | } 70 | 71 | make_shims() { 72 | local shims="$@" 73 | 74 | for file in $shims; do 75 | local shim="${file##*/}" 76 | register_shim "$shim" 77 | done 78 | } 79 | 80 | # Track registered shims 81 | registered_shims=() 82 | registered_shims_index="" 83 | 84 | # Helper to register a shim 85 | register_shim() { 86 | local shim="$@" 87 | registered_shims["${#registered_shims[@]}"]="$shim" 88 | registered_shims_index="$registered_shims_index/$shim/" 89 | } 90 | 91 | # Helper to install registered shims 92 | install_registered_shims() { 93 | local shim 94 | for shim in "${registered_shims[@]}"; do 95 | [ -e "$shim" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$shim" 96 | done 97 | } 98 | 99 | # Helper to remove stale shims 100 | remove_stale_shims() { 101 | local shim 102 | for shim in *; do 103 | if [[ "$registered_shims_index" != *"/$shim/"* ]]; then 104 | rm -f "$shim" 105 | fi 106 | done 107 | } 108 | 109 | # Change to the shims directory. 110 | cd "$SHIM_PATH" 111 | shopt -s nullglob 112 | 113 | # Create the prototype shim, then register shims for all known 114 | # executables. 115 | create_prototype_shim 116 | remove_outdated_shims 117 | make_shims ../versions/*/bin/* 118 | 119 | # Restore the previous working directory. 120 | cd "$OLDPWD" 121 | 122 | # Change back to the shims directory to install the registered shims 123 | # and remove stale shims. 124 | cd "$SHIM_PATH" 125 | install_registered_shims 126 | remove_stale_shims 127 | -------------------------------------------------------------------------------- /libexec/gobrew-help: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Summary: Display help for a command 4 | # 5 | # Usage: gobrew help [--usage] COMMAND 6 | # 7 | # Parses and displays help contents from a command's source file. 8 | # 9 | # A command is considered documented if it starts with a comment block 10 | # that has a `Summary:' or `Usage:' section. Usage instructions can 11 | # span multiple lines as long as subsequent lines are indented. 12 | # The remainder of the comment block is displayed as extended 13 | # documentation. 14 | 15 | command_path() { 16 | local command="$1" 17 | command -v gobrew-"$command" || command -v gobrew-sh-"$command" || true 18 | } 19 | 20 | extract_initial_comment_block() { 21 | sed -ne " 22 | /^#/ !{ 23 | q 24 | } 25 | 26 | s/^#$/# / 27 | 28 | /^# / { 29 | s/^# // 30 | p 31 | } 32 | " 33 | } 34 | 35 | collect_documentation() { 36 | awk ' 37 | /^Summary:/ { 38 | summary = substr($0, 10) 39 | next 40 | } 41 | 42 | /^Usage:/ { 43 | reading_usage = 1 44 | usage = usage "\n" $0 45 | next 46 | } 47 | 48 | /^( *$| )/ && reading_usage { 49 | usage = usage "\n" $0 50 | next 51 | } 52 | 53 | { 54 | reading_usage = 0 55 | help = help "\n" $0 56 | } 57 | 58 | function escape(str) { 59 | gsub(/[`\\$"]/, "\\\\&", str) 60 | return str 61 | } 62 | 63 | function trim(str) { 64 | sub(/^\n*/, "", str) 65 | sub(/\n*$/, "", str) 66 | return str 67 | } 68 | 69 | END { 70 | if (usage || summary) { 71 | print "summary=\"" escape(summary) "\"" 72 | print "usage=\"" escape(trim(usage)) "\"" 73 | print "help=\"" escape(trim(help)) "\"" 74 | } 75 | } 76 | ' 77 | } 78 | 79 | documentation_for() { 80 | local filename="$(command_path "$1")" 81 | if [ -n "$filename" ]; then 82 | extract_initial_comment_block < "$filename" | collect_documentation 83 | fi 84 | } 85 | 86 | print_summary() { 87 | local command="$1" 88 | local summary usage help 89 | eval "$(documentation_for "$command")" 90 | 91 | if [ -n "$summary" ]; then 92 | printf " %-9s %s\n" "$command" "$summary" 93 | fi 94 | } 95 | 96 | print_summaries() { 97 | for command; do 98 | print_summary "$command" 99 | done 100 | } 101 | 102 | print_help() { 103 | local command="$1" 104 | local summary usage help 105 | eval "$(documentation_for "$command")" 106 | [ -n "$help" ] || help="$summary" 107 | 108 | if [ -n "$usage" -o -n "$summary" ]; then 109 | if [ -n "$usage" ]; then 110 | echo "$usage" 111 | else 112 | echo "Usage: gobrew ${command}" 113 | fi 114 | if [ -n "$help" ]; then 115 | echo 116 | echo "$help" 117 | echo 118 | fi 119 | else 120 | echo "Sorry, this command isn't documented yet." >&2 121 | return 1 122 | fi 123 | } 124 | 125 | print_usage() { 126 | local command="$1" 127 | local summary usage help 128 | eval "$(documentation_for "$command")" 129 | [ -z "$usage" ] || echo "$usage" 130 | } 131 | 132 | unset usage 133 | if [ "$1" = "--usage" ]; then 134 | usage="1" 135 | shift 136 | fi 137 | 138 | if [ -z "$1" ] || [ "$1" == "gobrew" ]; then 139 | echo "Usage: gobrew []" 140 | [ -z "$usage" ] || exit 141 | echo 142 | echo "Some useful gobrew commands are:" 143 | print_summaries commands install uninstall use list version versions rehash workspace 144 | echo 145 | echo "See \`gobrew help ' for information on a specific command." 146 | echo "For full documentation, see: https://github.com/grobins2/gobrew#readme" 147 | else 148 | command="$1" 149 | if [ -n "$(command_path "$command")" ]; then 150 | if [ -n "$usage" ]; then 151 | print_usage "$command" 152 | else 153 | print_help "$command" 154 | fi 155 | else 156 | echo "gobrew: no such command \`$command'" >&2 157 | exit 1 158 | fi 159 | fi 160 | --------------------------------------------------------------------------------