-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoproj.sh
67 lines (60 loc) · 1.71 KB
/
goproj.sh
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
#!/bin/bash
declare -A GOPROJECTS
# ------------------------------------------------------------------------
# the main goproj command
# ------------------------------------------------------------------------
function goproj() {
VIM_OPT=false
while getopts ":v" opt; do
case $opt in
v)
VIM_OPT=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
shift $(($OPTIND-1))
done
cd ${GOPROJECTS[$1]}
_setGOPATH
if [ "$VIM_OPT" = true ]; then
vim
fi
}
# ------------------------------------------------------------------------
# autocomplition for the goproj command line
# ------------------------------------------------------------------------
_goproj() {
cur=${COMP_WORDS[COMP_CWORD]}
for project in "${!GOPROJECTS[@]}"
do
if [[ $project == $cur* ]]; then
use="$use $project"
fi
done
COMPREPLY=( $( compgen -W "$use" -- $cur ) )
}
complete -o default -o nospace -F _goproj goproj
# ------------------------------------------------------------------------
# Sets the GOPATH variable to the directory above src, by going up the
# filesystem
# ------------------------------------------------------------------------
function _setGOPATH() {
local originalPath=`pwd`
local current=
while true; do
current=$(basename "$PWD")
if [[ "$current" = "src" ]]; then
cd ..
echo "Setting GOPATH to: `pwd`"
export GOPATH=`pwd`
break
elif [[ "$current" = "/" ]]; then
echo "GOPATH not found"
break
fi
cd ..
done
cd $originalPath
}