-
Notifications
You must be signed in to change notification settings - Fork 1
/
goenv
executable file
·205 lines (189 loc) · 4.48 KB
/
goenv
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- mode: unix-shell-script; -*-
_goenv_help() {
print "Go workspace manager\n"
print "Usage:\n"
print "\tgoenv command [arguments]\n"
print "The commands are:\n"
print "\tinit initialize path as workspace (default working directory)"
print "\tset set workspace to path (default to working directory)"
print "\tadd add path to workspace (default working directory)"
print "\trm remove path from workspace (default working directory)"
print "\treset reset to workspace to empty"
print "\tlist list all workspace paths"
print "\twhich show current Go workspace"
print "\ngoenv manages the workspace by setting the GOPATH environment variable"
print "When setting or adding a workspace, goenv will search up the path"
print "to find the parent with the required GOPATH entries of bin, pkg, and"
print "src"
}
_goenv_fullpath() {
case "$1" in
/*)
echo "$1"
;;
*)
echo "$PWD/$1"
;;
esac
}
_goenv_find_ws_path() {
local WSPATH=$(pwd)
while [[ "$WSPATH" != "" && ! -e "$WSPATH/bin" && ! -e "$WSPATH/pkg" && ! -e "$WSPATH/src" ]]; do
WSPATH=${WSPATH%/*}
done
echo "$WSPATH"
}
_goenv_split() {
OLDIFS="$IFS"
IFS=$2
items=(`echo $1`)
IFS="$OLDIFS"
echo $items
}
_goenv_init() {
if [ -n "$1" ]; then
mkdir -p $1/bin
mkdir -p $1/pkg
mkdir -p $1/src
_goenv_add $1
else
print "Path does not exist"
fi
}
_goenv_set() {
if [[ -n "$1" && -d "$1" ]]; then
if [ -n "$GOPATH" ]; then
_goenv_unset
fi
# Cache the original path
_GOENV_OLD_PATH=$PATH
export _GOENV_OLD_PATH
# Set GOPATH
GOPATH=$1
export GOPATH
PATH=$PATH:$GOPATH/bin
export PATH
else
print "Go workspace missing"
fi
}
_goenv_add() {
if [[ -n "$1" && -d "$1" ]]; then
if [[ -n "$GOPATH" && "$GOPATH" =~ (^|:)"$1"(:|$) ]]; then
print "Workspace already added"
elif [ -z "$_GOENV_OLD_PATH" ]; then
_goenv_set $1
else
GOPATH=$GOPATH:$1
export GOPATH
PATH=$_GOENV_OLD_PATH:${GOPATH//://bin:}/bin
fi
else
print "Go workspace missing"
fi
}
_goenv_rm() {
if [[ -n "$1" && -d "$1" && -n "$GOPATH" && "$GOPATH" =~ (^|:)"$1"(:|$) ]]; then
local items=$GOPATH
_goenv_reset
items=(`_goenv_split $items ":"`)
for item in "${items[@]}"; do
if [ "$item" != "$1" ]; then
_goenv_add $item
fi
done
else
print "Go workspace missing"
fi
}
_goenv_reset() {
if [ -n "$GOPATH" ]; then
if [ -n "$_GOENV_OLD_PATH" ]; then
PATH=$_GOENV_OLD_PATH
export PATH
unset _GOENV_OLD_PATH
fi
unset GOPATH
else
print "Go workspace not set"
fi
}
_goenv_list() {
if [[ -n "$GOPATH" ]]; then
print "Go workspace paths:"
items=(`_goenv_split $GOPATH ":"`)
for item in "${items[@]}"; do
print "\t$item"
done
else
print "Go workspace not set"
fi
}
_goenv_which() {
if [[ -n "$GOPATH" ]]; then
print "Go workspace environment:"
print "GOPATH=$GOPATH"
print "GOROOT=$GOROOT"
print "PATH=$PATH"
else
print "Go workspace not set"
fi
}
_goenv_exec() {
case "$1" in
init)
if [ -z "$2" ]; then
_goenv_init $(pwd)
else
_goenv_init `_goenv_fullpath $2`
fi
;;
set)
if [ -z "$2" ]; then
_goenv_set `_goenv_find_ws_path`
else
_goenv_set `_goenv_fullpath $2`
fi
;;
add)
if [ -z "$2" ]; then
_goenv_add `_goenv_find_ws_path`
else
_goenv_add `_goenv_fullpath $2`
fi
;;
rm)
if [ -z "$2" ]; then
_goenv_rm `_goenv_find_ws_path`
else
_goenv_rm `_goenv_fullpath $2`
fi
;;
*)
print "Invalid command"
esac
}
goenv() {
cmd=$1
shift
case "$cmd" in
init | set | add | rm)
while (($#)); do
_goenv_exec $cmd $1
shift
done
;;
reset)
_goenv_reset
;;
list)
_goenv_list
;;
which)
_goenv_which
;;
help | *)
_goenv_help
;;
esac
}