├── Makefile ├── README.md └── install_as /Makefile: -------------------------------------------------------------------------------- 1 | GOTOOLDIR=$(shell go env GOTOOLDIR) 2 | 3 | install: 4 | cp install_as $(GOTOOLDIR) 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### go-install-as 2 | 3 | `go install` a package under a specific import path 4 | 5 | ### Installation 6 | 7 | $ make 8 | 9 | ### Why 10 | 11 | When building code for production you often want to have finer grain control over dependencies. 12 | 13 | This tool allows you to `go install` packages, regardless of their source (private, etc.), into whatever import path you specify. 14 | 15 | ### Example 16 | 17 | We have a private-repository on github, say `github.com/bitly/go-simplejson` and we want the import path to be `bitly/simplejson` for our Go client applications... 18 | 19 | $ git clone git@github.com:bitly/go-simplejson.git 20 | $ cd go-simplejson 21 | $ go tool install_as --import-as=bitly/simplejson 22 | -------------------------------------------------------------------------------- /install_as: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | VERSION="0.1.2" 5 | TMP_DIR="/tmp" 6 | SOURCE_DIR=`pwd` 7 | IMPORT_AS=`basename $SOURCE_DIR` 8 | DESTINATION_DIR=`echo $GOPATH | awk -F: '{print $1}'` 9 | 10 | function version() 11 | { 12 | echo -e "go-install-as v$VERSION" 13 | } 14 | 15 | function usage() 16 | { 17 | version 18 | echo -e "" 19 | echo -e "'go install' a package under a specific import path" 20 | echo -e "" 21 | echo -e "\t--tmp-dir=$TMP_DIR" 22 | echo -e "\t--source-dir=$SOURCE_DIR" 23 | echo -e "\t--import-as=$IMPORT_AS" 24 | echo -e "\t--destination-dir=$DESTINATION_DIR" 25 | echo -e "\t--version" 26 | echo -e "\t--help" 27 | } 28 | 29 | while [ "$1" != "" ]; do 30 | PARAM=`echo $1 | awk -F= '{print $1}'` 31 | VALUE=`echo $1 | awk -F= '{print $2}'` 32 | case $PARAM in 33 | -h | --help) 34 | usage 35 | exit 36 | ;; 37 | --version) 38 | version 39 | exit 40 | ;; 41 | --tmp-dir) 42 | TMP_DIR=$VALUE 43 | ;; 44 | --source-dir) 45 | SOURCE_DIR=$VALUE 46 | ;; 47 | --import-as) 48 | IMPORT_AS=$VALUE 49 | ;; 50 | --destination-dir) 51 | DESTINATION_DIR=$VALUE 52 | ;; 53 | esac 54 | shift 55 | done 56 | 57 | if [ -z $TMP_DIR ] || [ ! -d $TMP_DIR ]; then 58 | echo "--tmp-dir must exist" 59 | exit 1 60 | fi 61 | 62 | if [ -z $SOURCE_DIR ] || [ ! -d $SOURCE_DIR ]; then 63 | echo "--source-dir must exist" 64 | exit 1 65 | fi 66 | 67 | if [ -z $DESTINATION_DIR ] || [ ! -d $DESTINATION_DIR ]; then 68 | echo "--destination-dir must exist" 69 | exit 1 70 | fi 71 | 72 | rand_suffix=`cat /dev/urandom | LC_CTYPE=C tr -dc _A-Z-a-z-0-9 | head -c8` 73 | working_dir="$TMP_DIR/go-install-as_$rand_suffix" 74 | echo "working dir: $working_dir" 75 | mkdir -p $working_dir 76 | 77 | build_dir="$working_dir/src/$IMPORT_AS" 78 | echo "build dir: $build_dir" 79 | mkdir -p $build_dir 80 | 81 | cp -R $SOURCE_DIR/* $build_dir 82 | cd $build_dir 83 | echo "go install'ing" 84 | env GOPATH="$working_dir:$GOPATH" $GOROOT/bin/go install -x 85 | 86 | goos=`go env GOOS` 87 | goarch=`go env GOARCH` 88 | pkg_dir="${goos}_${goarch}" 89 | pkg_dest="$DESTINATION_DIR/pkg/$pkg_dir/" 90 | echo "copying PKG to $pkg_dest" 91 | cp -R $working_dir/pkg/$pkg_dir/* $pkg_dest 92 | 93 | src_dest="$DESTINATION_DIR/src/" 94 | echo "copying SRC to $src_dest" 95 | cp -R $working_dir/src/* $src_dest 96 | 97 | if [ -d $working_dir/bin ]; then 98 | bin_dest="$DESTINATION_DIR/bin/" 99 | echo "copying BIN to $bin_dest" 100 | cp -R $working_dir/bin/* $bin_dest 101 | fi 102 | 103 | rm -rf "$working_dir" 104 | --------------------------------------------------------------------------------