-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdj.sh
executable file
·401 lines (363 loc) · 13 KB
/
dj.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/bin/bash
# ------------------------------------------------------------------
# Copyleft (C) Ákos Kovács - 2022
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------
## You can set the alias used by the script by defining the environment variable $DIRJUMPER_ALIAS
if [[ $DIRJUMPER_ALIAS = '' ]]; then
DIRJUMPER_ALIAS='j'
fi
alias $DIRJUMPER_ALIAS='dirjumper'
## You can disable coloring by:
#export $DIRJUMPER_COLOR=1
if [[ $DIRJUMPER_COLOR = '' ]]; then
COLOR_RED="\x1b[31m"
COLOR_BLUE="\x1b[34m"
COLOR_BBLUE="\x1b[1;34m"
COLOR_PURPLE="\x1b[35m"
COLOR_BPURPLE="\x1b[1;35m"
COLOR_GREEN="\x1b[32m"
COLOR_BGREEN="\x1b[1;32m"
COLOR_YELLOW="\x1b[33m"
COLOR_GRAY="\x1b[30m"
COLOR_LGRAY="\x1b[37m"
COLOR_END="\x1b[0m"
fi
## Shell init file
SH_RC_FILE=".bashrc"
## Home configuration directory under ~
CONFDIR=".config"
DJEXE="dj.sh"
DJEXE_OLD="dj.sh.old"
DJFILE="dj.list"
DJLOG="/tmp/dj.log"
DJDIR=".dirjumper"
DJPATH="$HOME/$CONFDIR/$DJDIR"
DJLIST="$DJPATH/$DJFILE"
DJBIN="$DJPATH/$DJEXE"
VERSION="0.5.1"
function dirjumper () {
## Stable, main update server
local REPO="https://raw.githubusercontent.com/akoskovacs/dirjumper/master"
## Uncomment for upstream updates
#local REPO="https://raw.githubusercontent.com/akoskovacs/dirjumper/upstream"
## Uncomment for local debugging
#local REPO="http://localhost:8000"
local OPTIND optname
print_examples () {
echo -e "Example usage:"
echo -e "$COLOR_GREEN $ $DIRJUMPER_ALIAS -a ex\t\t\t\t$COLOR_LGRAY# bookmarks the current directory as 'ex'$COLOR_END"
echo -e "$COLOR_GREEN $ $DIRJUMPER_ALIAS ex\t\t\t\t\t$COLOR_LGRAY# changes the directory to the bookmared 'ex' previously$COLOR_END"
echo -e "$COLOR_GREEN $ $DIRJUMPER_ALIAS -a ex /usr/lib/example\t\t$COLOR_LGRAY# bookmarks '/usr/lib/example' as 'ex'$COLOR_END"
echo -e "$COLOR_GREEN $ $DIRJUMPER_ALIAS -r ex e\t\t\t\t$COLOR_LGRAY# renames the alias 'ex' to 'e'$COLOR_END"
echo -e "$COLOR_GREEN $ $DIRJUMPER_ALIAS -d e\t\t\t\t$COLOR_LGRAY# delete 'e' permanently$COLOR_END"
echo
echo -e "${COLOR_LGRAY}You can also directly edit the list of aliases at:"
echo -e "\t'$DJLIST'${COLOR_END}"
echo
}
print_help () {
echo -e "Usage: $COLOR_BBLUE$DIRJUMPER_ALIAS -ardgeliuwhv [<ARGS>]$COLOR_END"
echo -e "\t$COLOR_BLUE-a <ALIAS> [<DIR>]$COLOR_END\t\tAdd an alias, use the working dir. if <DIR> is not set"
echo -e "\t$COLOR_BLUE-r <OLD-ALIAS> <NEW-ALIAS>$COLOR_END\tRename a directory alias"
echo -e "\t$COLOR_BLUE-d <ALIAS>$COLOR_END\t\t\tDelete a directory alias"
echo -e "\t$COLOR_BLUE-g <ALIAS>$COLOR_END\t\t\tGet the full path for an alias"
echo -e "\t$COLOR_BLUE-e$COLOR_END\t\t\t\tEdit alias list file with the default editor"
echo -e "\t$COLOR_BLUE-l$COLOR_END\t\t\t\tList all aliases\n"
echo -e "\t$COLOR_BLUE-u$COLOR_END\t\t\t\tUpgrade the script to the latest version"
echo -e "\t$COLOR_BLUE-w$COLOR_END\t\t\t\tDowngrade the script to an older version\n"
echo -e "\t$COLOR_BLUE-h$COLOR_END\t\t\t\tThis help message"
echo -e "\t$COLOR_BLUE-v$COLOR_END\t\t\t\tThe version of the script"
echo
print_examples
}
## Not all environments have realpath
own_realpath() {
local ourpwd=$PWD
cd "$(dirname "$1")"
local link=$(readlink "$(basename "$1")")
while [ "$link" ]; do
cd "$(dirname "$link")"
link=$(readlink "$(basename "$1")")
done
own_realpath="$PWD/$(basename "$1")"
cd "$ourpwd"
echo "$own_realpath"
}
## Format and color alias/directory list
list_aliases () {
# cat "$DJLIST"
local wd=`pwd`
while read line; do
local al=`echo $line | cut -d " " -f1`
local pth=`echo $line | cut -d " " -f2-`
local pth_color=$COLOR_BBLUE
local pth_sel=" "
# If the current working directory is something we have
# an alias for make it known
if [[ $pth = $wd ]]; then
pth_color=$COLOR_BGREEN
pth_sel="+"
fi
printf "$pth_color\t$pth_sel %s$END_COLOR\t$COLOR_PURPLE%s$END_COLOR\n" $al $pth
done < "$DJLIST"
echo -ne "$COLOR_END"
}
## Get the directory from the given alias
read_alias () {
egrep "^\b$1\b" "$DJLIST" | sort | cut -d " " -f2- | sort
}
find_alias () {
egrep "^\b$1\b" "$DJLIST" | sort | cut -d " " -f1 | sort
}
get_alias() {
local dir=`read_alias $1`
if [[ $dir = "" ]]; then
echo -e "${COLOR_RED}Alias '$1' not found! $COLOR_END" >&2
return 1
else
echo $dir
fi
}
## Validates a string as alias
check_alias () {
if [[ "$1" =~ ^[0-9A-Za-z_@!%=.,]+$ ]]; then
return 0
fi
echo -en $COLOR_RED
echo "Invalid name for an alias!"
echo -en $COLOR_END
return 1
}
## Change the current directory to the bookmarked one (if any)
cd_alias () {
check_alias $1
if [[ $? -ne 0 ]]; then
return 1
fi
local dir=`get_alias $1`
if [[ $dir != "" ]]; then
cd $dir
fi
}
## Bookmark the given or if it is not given the current directory to the list
add_alias () {
local dname=$2
check_alias $1
if [[ $? -ne 0 ]]; then
return 1
fi
if [[ $dname = "" ]]; then
dname=`pwd`
echo -en $COLOR_YELLOW
echo "Bookmarking the current directory '$dname' as '$1'"
echo -en $COLOR_END
fi
dname=`own_realpath $dname`
local odname=`find_alias $1`
if [[ $odname = "" ]]; then
echo "$1 $dname" >> "$DJLIST"
else
echo -en $COLOR_RED
echo "Alias '$1' is already exist for path '$odname'!"
echo -en $COLOR_END
echo -en $COLOR_YELLOW
echo "Use '$DIRJUMPER_ALIAS -r $1 <new-alias>' to rename it"
echo -en $COLOR_END
fi
}
rename_alias () {
if [[ $2 = "" ]]; then
echo -en $COLOR_RED
echo "You must give the alias a new name!"
echo -en $COLOR_END
return 1
fi
check_alias $1
if [[ $? -ne 0 ]]; then
return 1
fi
local oname=`get_alias $1`
if [[ $oname != "" ]]; then
sed -i "s/^\b$1\b/$2/" "$DJLIST"
fi
}
delete_alias () {
check_alias $1
if [[ $? -ne 0 ]]; then
return 1
fi
local oname=`get_alias $1`
if [[ $oname != "" ]]; then
sed -i "/^\b$1\b/d" "$DJLIST"
fi
}
## Updates the current install if a new version is available
update_dirjumper () {
echo -e "${COLOR_YELLOW}[+] Checking for new version (current is v$VERSION)...${COLOR_END}"
cd $DJPATH
new_version=$(wget "$REPO/VERSION" -O- -o $DJLOG)
if [ $? -ne 0 ]; then
cat $DJLOG
echo -e "${COLOR_RED}[!] Error while downloading version information!${COLOR_END}"
return 1
fi
if [ $VERSION = $new_version ]; then
echo -e "${COLOR_GREEN}[+] Your version is up-to-date (v$VERSION)${COLOR_END}"
return 0
fi
echo -e "${COLOR_GREEN}[+] New version (v$new_version) found...${COLOR_END}"
echo -ne "${COLOR_YELLOW}[?] Do you want to upgrade? [y/N]: ${COLOR_END}"
read answ
if [ $answ = "y" -o $answ = "Y" ]; then
mv $DJEXE $DJEXE_OLD
wget -o $DJLOG -O "$DJPATH/$DJEXE" "$REPO/$DJEXE"
if [ $? -ne 0 ]; then
cat $DJLOG
echo -e "${COLOR_RED}[!] Error while downloading upgrades!${COLOR_END}"
rm $DJEXE
fi
chmod +x $DJEXE
echo -e "${COLOR_GREEN}[+] Upgrade done...${COLOR_END}"
echo -e "${COLOR_LGRAY}[*] Open a new shell to apply changes...${COLOR_END}"
else
echo -e "${COLOR_RED}[-] Upgrade aborted.${COLOR_END}"
fi
}
## Restore and older version
downgrade_dirjumper () {
cd $DJPATH
if [ -e $DJEXE_OLD ]; then
old_version=$(./$DJEXE_OLD -v)
mv $DJEXE dj.older
mv $DJEXE_OLD $DJEXE
mv dj.older $DJEXE_OLD
echo -e "${COLOR_GREEN}[+] Sucessfully downgraded from '${old_version}' to '${VERSION}'.${COLOR_END}"
else
echo -e "${COLOR_RED}[!] No older version found!${COLOR_END}"
fi
}
edit_aliases () {
echo -e "Opening $DJLIST for editing..."
if [ ! -z $EDITOR ]; then
$EDITOR $DJLIST
else
echo -e "${COLOR_YELLOW}No \$EDITOR is set. Attempting to use nano${COLOR_END}..."
nano $DJLIST
fi
}
## main entry point
while getopts "a:r:d:g:lieuwsvh" optname; do
case "${optname}" in
"v")
echo $VERSION
;;
"a")
add_alias $OPTARG ${@:$OPTIND:1}
;;
"r")
rename_alias $OPTARG ${@:$OPTIND:1}
;;
"d")
delete_alias $OPTARG
;;
"g")
read_alias $OPTARG
;;
"s")
init_sh
;;
"l")
list_aliases
;;
"e")
edit_aliases
;;
"u")
update_dirjumper
;;
"w")
downgrade_dirjumper
;;
"h")
print_help
;;
"?")
echo -e "${COLOR_RED}Unknown option $OPTARG${COLOR_END}"
;;
":")
echo -e "${COLOR_RED}No argument value for option $OPTARG${COLOR_END}"
;;
*)
echo -e "${COLOR_RED}Unknown error while processing options${COLOR_RED}"
;;
esac
done
shift $(($OPTIND - 1))
## Jump to alias if we still have args untouched by getopts
if [[ $1 != '' ]]; then
cd_alias $1
fi
## No args, so print the whole list
if [[ ( $# -eq 0 ) && ( $OPTIND -eq 1 ) ]]; then
list_aliases
fi
## Remove function names from the global namespace,
# since everything will be sourced from the main init script.
unset -f print_examples
unset -f print_help
unset -f own_realpath
unset -f list_aliases
unset -f read_alias
unset -f find_alias
unset -f check_alias
unset -f get_alias
unset -f cd_alias
unset -f add_alias
unset -f rename_alias
unset -f delete_alias
unset -f update_dirjumper
unset -f downgrade_dirjumper
unset -f edit_aliases
}
## Creates the alias list and appends the init script after downloading
install_dirjumper () {
echo "[+] Creating installation directory '$DJPATH'..."
mkdir -p "$DJPATH"
echo "[+] Installing '$DJBIN'..."
cp "$0" "$DJBIN"
echo "[+] Creating '$DJLIST'..."
touch "$DJLIST"
echo "[+] Appending dirjump to '$SH_RC_FILE'..."
echo -e "# <dirjumper>" >> "$HOME/$SH_RC_FILE"
echo -e "source \"$DJBIN\"" >> "$HOME/$SH_RC_FILE"
echo -e "# </dirjumper>" >> "$HOME/$SH_RC_FILE"
echo -e "$COLOR_GREEN[+] All done. Start a new shell to apply changes...$COLOR_END"
echo -e "$COLOR_LGRAY[*] Now you can use '$DIRJUMPER_ALIAS -a <alias>' to add an alias for this directory."
echo -e "[*] And then use '$DIRJUMPER_ALIAS <alias>' to return here."
echo -e "[*] Use '$DIRJUMPER_ALIAS -h' to find out more... "
echo -e $COLOR_END
}
## Install when executed as a script with the '-i' option
if [[ $1 = 'install' ]]; then
install_dirjumper
fi
unset -f install_dirjumper