/
gows
334 lines (303 loc) · 6.85 KB
/
gows
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- mode: unix-shell-script; -*-
_gows_help() {
echo "Go workspace manager"
echo
echo "Usage:"
echo " gows command [arguments]"
echo
echo "The commands are:"
echo " add add path to front of workspace GOPATH (default current)"
echo " cd switch to directory of package path (default \$GOPATH/src)"
echo " edit open package path in VISUAL editor (default \$GOPATH/src)"
echo " init initialize workspace (default working directory)"
echo " install install default Go packages"
echo " list list all workspace paths"
echo " reset reset to empty workspace"
echo " run detects go path and runs command in that environment"
echo " switch switch workspace to path (default to working directory)"
echo " which show workspace for path (default to working directory)"
echo
echo "gows manages the workspace by setting the GOPATH environment variable"
echo "When setting or adding a workspace, gows will search up the path"
echo "to find the parent with the required GOPATH entries of bin, pkg, and src"
echo "Workspaces are added to the front of an existing GOPATH."
echo
echo "gows init will install all packages listed in the following:"
echo " /usr/etc/goenvrc"
echo " /usr/local/etc/goenvrc"
echo " ~/.goenvrc"
echo " .goenvrc"
}
_gows_commands() {
for c in add cd edit init install list reset run switch which; do
echo $c
done
}
_gows_complete() {
case "$1" in
cd | edit )
# Use current workspace only
(
# go list std doesn't include cmd/internal/rsc.io/* so filter manually
GOPATH=`_gows_find_ws_path`
comm -3 <(go list all) <(go list std) | grep -v "cmd/internal/*"
)
;;
*)
;;
esac
}
_gows_config() {
rcfiles=(/usr/etc/goenvrc /usr/local/etc/goenvrc ~/.goenvrc ./.goenvrc)
packages=()
for rc in ${rcfiles[*]}; do
if [ -e $rc ]; then
while read line; do
packages=("${packages[@]}" $line)
done <$rc
fi
done
}
_gows_exec() {
case "$1" in
add)
if [ -z "$2" ]; then
_gows_add `_gows_find_ws_path`
else
_gows_add `_gows_fullpath $2`
fi
;;
cd)
_gows_cd $2
;;
edit)
_gows_edit $2
;;
init)
if [ -z "$2" ]; then
_gows_init $(pwd)
else
_gows_init `_gows_fullpath $2`
fi
;;
install)
_gows_install
;;
switch)
if [ -z "$2" ]; then
_gows_switch `_gows_find_ws_path`
else
_gows_switch `_gows_fullpath $2`
fi
;;
*)
echo "Invalid command"
esac
}
_gows_find_ws_path() {
local WSPATH=${1:-$(pwd)}
if [ -d "${WSPATH}" ]; then
while [[ "${WSPATH}" != "" ]] && ! [[ -d "${WSPATH}/bin" && -d "${WSPATH}/pkg" && -d "${WSPATH}/src" ]]; do
WSPATH=${WSPATH%/*}
done
echo `_gows_fullpath "${WSPATH}"`
fi
}
_gows_fullpath() {
case "$1" in
/*)
echo "$1"
;;
*)
echo "$PWD/$1"
;;
esac
}
_gows_split() {
OLDIFS="$IFS"
IFS=$2
items=(`echo $1`)
IFS="$OLDIFS"
echo $items
}
# Subcommands
_gows_add() {
if [[ -n "$1" && -d "$1" ]]; then
# Verify target is a Go workspace
if [[ -d "$1/bin" && -d "$1/pkg" && -d "$1/src" ]]; then
if [ -z "$_GOWS_OLD_PATH" ]; then
# Workspace has not been set yet
_gows_switch $1
elif [[ ! "$GOPATH" =~ (^|:)"$1"(:|$) ]]; then
# Add workspace
if [ -n "$GOPATH" ]; then
GOPATH=${1}:${GOPATH}
else
GOPATH=${1}
fi
export GOPATH
PATH=${1}/bin:${PATH}
export PATH
else
echo "Workspace already set"
fi
else
echo "Not a Go workspace"
fi
else
echo "Go workspace missing"
fi
}
_gows_cd() {
# Determine if relative or full path or package name
case "$1" in
/*)
cd `_gows_fullpath $1`
;;
.*)
cd "$1"
;;
*)
cd `_gows_find_ws_path`/src/$1
;;
esac
_gows_add `_gows_find_ws_path`
}
_gows_edit() {
if [[ -n "$GOPATH" ]]; then
if [ -z "$VISUAL" ]; then
echo "VISUAL editor is not set"
return
fi
"$VISUAL" "`_gows_find_ws_path`/src/$1"
else
echo "Go workspace not set"
fi
}
_gows_init() {
if [ -n "$1" ]; then
# Verify that directory is not already a Go workspace
if [[ -n "$(_gows_find_ws_path "${1}")" ]]; then
echo "${1} is already in a workspace"
return
fi
mkdir -p $1/bin $1/pkg $1/src
_gows_add $1
# Install default packages
_gows_install
else
echo "Path does not exist"
fi
}
_gows_install() {
if [[ -n "${GOPATH}" ]]; then
# Read configuration
_gows_config
# go get default packages
for pkg in ${packages[*]}; do
echo "go get ${pkg}"
go get -u "${pkg}"
done
else
echo "Go workspace not set"
fi
}
_gows_list() {
if [[ -n "$GOPATH" ]]; then
items=(`_gows_split $GOPATH ":"`)
for item in "${items[@]}"; do
echo "$item"
done
else
echo "Go workspace not set"
fi
}
_gows_reset() {
if [ -n "$GOPATH" ]; then
if [ -n "$_GOWS_OLD_PATH" ]; then
PATH=$_GOWS_OLD_PATH
export PATH
unset _GOWS_OLD_PATH
fi
if [ -n "$_GOWS_OLD_GOPATH" ]; then
GOPATH=$_GOWS_OLD_GOPATH
export GOPATH
unset _GOWS_OLD_GOPATH
else
unset GOPATH
fi
else
echo "Go workspace not set"
fi
}
_gows_switch() {
# Reset workspace if already set
if [ -n "$_GOWS_OLD_PATH" ]; then
_gows_reset
fi
# Cache original values before switching to allow reset
if [[ -n "$1" && -d "$1" ]]; then
_GOWS_OLD_PATH=$PATH
export _GOWS_OLD_PATH
if [ -n "$GOPATH" ]; then
_GOWS_OLD_GOPATH=$GOPATH
export _GOWS_OLD_GOPATH
fi
_gows_add $1
else
echo "Go workspace missing"
fi
}
_gows_which() {
echo `_gows_find_ws_path "${1}"`
}
# Command handler
gows() {
cmd=$1
case "$cmd" in
cd | edit | install | switch)
_gows_exec $@
;;
# Commands for list
add | init)
shift
if [ $# -gt 0 ]; then
while (($#)); do
_gows_exec $cmd $1
shift
done
else
_gows_exec $cmd
fi
;;
run)
shift
(
_gows_exec "switch"
if [ 0 -eq $? ]; then
$@
fi
)
;;
reset ) _gows_reset;;
list ) _gows_list;;
which )
shift
_gows_which $@
;;
--commands) _gows_commands;;
--complete)
shift
if [ $# -gt 0 ]; then
_gows_complete $*
fi
;;
help | *) _gows_help;;
esac
}
# Setup completion
if [ -n "$BASH_VERSION" ]; then
source "$(dirname "${BASH_SOURCE[0]}")/gows.bash"
elif [ -n "$ZSH_VERSION" ]; then
source "$(dirname "$0")/gows.zsh"
fi