commit 6e1d3638f851a847dbbaf648d18e629ce3df12ee Author: brettlangdon Date: Sat May 9 15:41:25 2015 -0400 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..435a5c0 --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +gopkg +===== + +`gopkg` is a helper wrapper similar to `virtualenvwrapper` for python, making it +easy to keep package third party dependencies isolated. `gopkg.sh` simply +provides some bash functions to help manage your `GOPATH`. + + +## Setup + +Download the latest version of the script. +```bash +mkdir ~/.gopkg +cd ~/.gopkg +curl -o gopkg.sh https://raw.githubusercontent.com/brettlangdon/gopkg/master/gopkg.sh +``` + +Next modify your `~/.bashrc` or `~/.zshrc` files to include the following +```bash +export GOPKG_REPO=github.com/username +export GOPKG_HOME=~/.gopkg +source ~/.gopkg/gopkg.sh +``` + +### Environment variables + +`gopkg` uses the following environment variables + +* `GOPATH` - when creating a new package `gopkg` will create a directory for the +source of your package within your `GOPATH` +* `GOPKG_REPO` - when creating a new package `gopkg` will use `GOPKG_REPO` as +the base repository location for your package (e.g. `github.com/username`) +* `GOPKG_HOME` - when creating a new package `gopkg` will create a new directory +in `GOPKG_HOME` to store all third party packages (installed normally via `go +get`) in this directory + + +## Usage +### Creating a new package + +To create a new package use the command `mkgopkg `. This will create +directories (if they do not already exist) at `$GOPATH/src/$GOPKG_REPO/` +and `$GOPKG_HOME/` and will update your `GOPATH` to be +`$GOPKG_HOME/:$GOPATH`. + +### Removing an existing package + +To remove an existing package run the command `rmgopkg `. `rmgopkg` will +only remove the directory created inside of `$GOPKG_HOME/`, it will not +touch the one in `$GOPATH/src/$GOPKG_REPO/`. + +### Activating a package + +Just like `virtualenv` you have to "activate" a package in order to use +it. Running `gopkg ` will activate an existing package. What it does is +simply update your `GOPATH` to be `$GOPKG_HOME/:$GOPATH` and add `() +` to `PS1`. + +The updated `GOPATH` allows `go get` to install all packages into +`$GOPKG_HOME/` while still being able to successfully find you source +package in `$GOPATH/src/$GOPKG_REPO/`. + +### Deactivating a package + +When you are done and want to reset your `PS1` and `GOPATH` variables simply run +`deactivate`. `deactivate` is a command which is only available once you have +run `gopkg `. diff --git a/gopkg.sh b/gopkg.sh new file mode 100644 index 0000000..80d751c --- /dev/null +++ b/gopkg.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +GOPKG_ORIGINAL_PS1=${PS1} +GOPKG_ORIGINAL_GOPATH=${GOPATH} + +gopkg_verify_env () { + env=$(printenv ${1}) + if [ -z ${env} ]; + then + echo '$'${1}' environment variable must be set.' >&2 + return 1 + fi + + return 0 +} + +gopkg_get_gopath_base () { + # If we have multiple paths on $GOPATH we want the last one + paths=$(echo ${GOPATH} | tr ':' '\n') + go_path='' + echo $paths | while read p; + do + go_path=$p + done + echo $go_path + return 0 +} + +gopkg_get_pkg_dir () { + if [ -z $1 ]; + then + echo 'gopkg_get_pkg_dir requires 1 argument' >&2 + return 1 + fi + + go_path_base=$(gopkg_get_gopath_base) + echo ${go_path_base%%/}'/src/'${GOPKG_REPO%%/}'/'${1} + return 0 +} + +gopkg_get_pkg_home () { + if [ -z $1 ]; + then + echo 'gopkg_get_pkg_home requires 1 argument' >&2 + return 1 + fi + echo ${GOPKG_HOME%%/}'/'${1} + return 0 +} + +mkgopkg () { + if [ -z ${1} ]; + then + echo 'mkgopkg requires at least 1 argument' >&2 + return 1 + fi + + gopkg_verify_env 'GOPKG_HOME' || return 1 + gopkg_verify_env 'GOPKG_REPO' || return 1 + gopkg_verify_env 'GOPATH' || return 1 + + pkg_home=$(gopkg_get_pkg_home $1) + pkg_dir=$(gopkg_get_pkg_dir $1) + + if [ -d ${pkg_dir} ] && [ -d ${pkg_home} ]; + then + echo 'gopkg '${1}' already exists. Activate it with `gopkg '${1}'`' >&2 + return 1 + fi + echo 'Creating package source: '${pkg_dir} + mkdir -p $pkg_dir + echo 'Creating package home: '${pkg_home} + mkdir -p $pkg_home + echo 'Package '${1}' created.' + echo 'Activate with `gopkg '${1}'`' + return 0 +} + +rmgopkg () { + if [ -z ${1} ]; + then + echo 'rmgopkg requires at least 1 argument' >&2 + return 1 + fi + gopkg_verify_env 'GOPKG_HOME' || return 1 + + pkg_home=$(gopkg_get_pkg_home $1) + pkg_dir=$(gopkg_get_pkg_dir $1) + if [ ! -d ${pkg_home} ]; + then + echo 'gopkg '${1}' does not exist. Not removing anything' >&2 + return 1 + fi + + if [ -d ${pkg_dir} ]; + then + echo 'Keeping source directory '${pkg_dir} + fi + + echo 'Removing '${pkg_home} + rm -rf ${pkg_home} + return 0 +} + +gopkg () { + if [ -z ${1} ]; + then + echo 'gopkg requires at least 1 argument' >&2 + return 1 + fi + gopkg_verify_env 'GOPKG_HOME' || return 1 + + pkg_home=$(gopkg_get_pkg_home $1) + if [ ! -d ${pkg_home} ]; + then + echo 'gopkg '${1}' does not exists, create it first with `mkgopkg '${1}'`' >&2 + return 1 + fi + + go_path_base=$(gopkg_get_gopath_base) + export GOPATH=${pkg_home}:${go_path_base} + export PS1='('${1}') '$GOPKG_ORIGINAL_PS1 + + eval 'deactivate () { + export GOPATH=${GOPKG_ORIGINAL_GOPATH} + export PS1=${GOPKG_ORIGINAL_PS1} + unset -f deactivate > /dev/null 2>&1 + }' + echo ${1}' activated. Deactivate with `deactivate`' + + return 0 +}