-
Notifications
You must be signed in to change notification settings - Fork 3
/
goto
executable file
·110 lines (85 loc) · 2.96 KB
/
goto
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
#!/usr/bin/env bash
# Checking if we are sourced
# see: https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced
([[ -n $ZSH_EVAL_CONTEXT && $ZSH_EVAL_CONTEXT =~ :file$ ]] ||
[[ -n $BASH_VERSION ]] && (return 0 2>/dev/null)) && _GOTO_IS_SOURCED=0 || _GOTO_IS_SOURCED=1
. _gotoutils
load_gotopath
function _goto_main {
local PROJECT URI
# "Neo, sooner or later you're going to realize, just as I did,
# that there's a difference between knowing the
# path and walking the path." - Morpheus
if [[ ! "$1" =~ ^(--check-migrate|--migrate)$ ]] && detect_unmigrated_data; then
goto --check-migrate
return $?
fi
# If goto is run in sourced mode, contiune.
# Else: print warning. NO EXIT statement.
if [[ $_GOTO_IS_SOURCED -eq 0 ]]; then
PROJECT=$(cat "${GOTOPATH}/active-project")
# Catching deactivated state
if [ -z "$PROJECT" ]; then
_no_project
return 1
fi
# Special case 1 (goto cd <magicword>)
if [ "$1" = "cd" ]; then
# hack to cd in this shell to the ouput of goto show <magicword>
URI=$(goto show "$2")
cd "$URI" || echo "Ah hoy - tried to cd to a non-existent path: $URI" >&2
return 0
fi
# Special case 2 (goto <magicword>)
if [[ "$#" -eq 1 \
&& ! "$1" =~ ^(list|help|add|rm|mv|rename|update|copy|subl|vscode|intelij|idea|show|cd|open)$ \
&& ! "$1" =~ ^"--" \
&& ! "$1" =~ ^"-" \
]]; then
# if run like: goto <magicword>
URI=$(goto show "$1")
# if path is folder, cd to folder
if [ -d "$URI" ]; then
cd "$URI" || echo "Ah hoy - tried to cd to a non-existent path: $URI" >&2
return 0
# if path is file, open file
elif [ -f "$URI" ]; then
goto open "$1"
return 0
fi
fi
# General case
if [ -n "$(command -v the_real_goto.py)" ]; then
the_real_goto.py "$PROJECT" "$@"
else
echo "Error the_real_goto.py not found" >&2
return 1
fi
else
_not_installed
exit 1
fi
}
function _no_project {
echo "Ah hoy!" >&2
echo >&2
echo "Goto has no project context set." >&2
echo >&2
echo "An example project is bundled with goto, it is called goto." >&2
echo "You may try it by typing:" >&2
echo >&2
echo " project goto" >&2
echo >&2
echo "Other usefull commands to work with projects:" >&2
echo " create project: project add <project-name>" >&2
echo " more help: project help" >&2
}
function _not_installed {
echo "Ah hoy!" >&2
echo >&2
echo "In order to make goto work properly, run this command now:" >&2
echo >&2
echo " install_goto" >&2
echo >&2
}
_goto_main "$@"