This repository has been archived by the owner on Jan 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
goenv.plugin.zsh
89 lines (72 loc) · 1.86 KB
/
goenv.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function _golang_normalize_env {
if [ "${${:-$GO_ENV}[-1]}" = "/" ]; then
export GO_ENV=${GO_ENV:0:${#GO_ENV}-1}
elif [ -z "$GO_ENV" ]; then
export GO_ENV="$HOME/.goenvs";
fi
if [ ! -d "$GO_ENV" ]; then
mkdir "$GO_ENV"
fi
}
function _golang_workon {
if [ -z "$1" ]; then
printf "Specify a workspace to work with\n";
return 1
fi
_golang_normalize_env;
GOPATH=$(printf "$GO_ENV/$1\n")
if [ -z "$_GOENV_OLD_PATH" ]; then
# We have no old path - we can undonditionally set it
_GOENV_OLD_PATH="$PATH"
fi
PATH=$_GOENV_OLD_PATH:$GOPATH/bin
if [ -d "$GOPATH" ]; then
export GOPATH
export PATH
if [ -n "$GOENV_VERBOSE" ]; then
printf "GOPATH => %s\nPATH => %s\n" $GOPATH $PATH
fi
else
printf 'No directory found for $GOPATH. Has it been created with 'mk_go_workspace'?\n'
return 1
fi
}
alias go_workon=_golang_workon
function mk_go_workspace {
if [ -z $1 ]; then
printf "Specify a workspace name\n";
return 1
fi
_golang_normalize_env;
if [ ! -d $GO_ENV/$1 ]; then
ws_name=$1;
pushd $GO_ENV > /dev/null;
mkdir -p $ws_name/{bin,pkg,src}
_golang_workon $1
popd > /dev/null;
else
printf "Workspace '$1' already exists!\n"
return 1
fi
}
function go_deactivate {
if [ -n "$_GOENV_OLD_PATH" ]; then
PATH="$_GOENV_OLD_PATH"
export PATH
fi
unset GOPATH
}
function rm_go_workspace {
if [ -z $1 ]; then
printf "Specify a workspace to delete\n";
return 1
fi
_golang_normalize_env;
if [ ! -d $GO_ENV/$1 ]; then
# This workspace doesn't exist?
printf "Workspace \"$1\" doesn't exist.\n"
return 1
fi
rm -rf $GO_ENV/$1 > /dev/null
go_deactivate;
}