-
Notifications
You must be signed in to change notification settings - Fork 4
/
goto.bash
71 lines (56 loc) · 1.07 KB
/
goto.bash
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
# To be sourced in your .bashrc or .zshrc
if [ -z "$_goto_tool_path" ]; then
if [ -n "$ZSH_NAME" ]; then
_goto_tool_path=$(dirname $0)
elif [ -n "$BASH" ]; then
_goto_tool_path=$(dirname ${BASH_SOURCE[0]})
else
echo 'Unknown shell, cannot infer goto-tool path automatically.'
echo 'Please set your PATH so that the goto-tool script is accessible.'
_goto_tool_path=$(dirname `which goto-tool`)
fi
export _goto_tool_path
fi
alias goto-tool=goto_tool_fn
goto_tool_fn() {
${_goto_tool_path}/goto-tool $@
}
# $1 godir
# [$2 directory]
goto() {
local dir
local ret
if [ "$#" -eq 0 ]; then
echo "Usage: goto <dirname> [<subpath>]"
return 1
fi
dir=$(goto-tool get "$1")
ret=$?
if [ "$ret" -eq 0 ]; then
goto-tool use "$1" >/dev/null
if [ "$#" -gt 1 ]; then
cd "$dir"/"$2"
else
cd "$dir"
fi
fi
return $ret
}
# $1 godir
# [$2 file]
goget() {
local DIR
if DIR="$(goto-tool get $1)"; then
goto-tool use $1 >/dev/null
echo "$DIR${2:+/$2}"
fi
}
# $1 cmd
# $2 godir
# [$3 file]
gorun() {
local dir
if dir="$(goget $2 $3)"; then
$1 $dir
fi
}