This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandstool
517 lines (502 loc) · 26.6 KB
/
commandstool
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/bin/bash
#==============================================================================
# Copyright 2016 Adrian Vovk
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#==============================================================================
# This tool allows you to download commands (be it .sh files or specially-made extentions to this file)
# Feel free to contribute your own commands at https://github.com/AdrianVovk/BashCommands. I will approve them myself.
#=========================================================================================================================
# Read configuration
INSTALLED_SCRIPTS_LOCATION=~/.commandstool/runner
COMMANDS_TOOL_FILE=$(cat ~/.commandstool/config/script_location)
UPDATE_SCRIPT=~/.commandstool/update_script
PLUGINS_DATA_STORE=~/.commandstool/plugins_storage
CT_COMMAND=$1
if [ -z $CT_COMMAND ]; then
echo "Usage: 'commandstool command [options]'"
echo "Run 'commandstool commands' to see a list of commands and how to use them"
return
fi
shift
case $CT_COMMAND in
'try')
# If the user overrided anything, use it
# Adapted from http://bsdpants.blogspot.com/2007/02/option-ize-your-shell-scripts.html
unset FILENAME
unset TYPE
if [[ ! ${1:0:1} = '-' ]] && [[ ! -z $1 ]]; then
IN=$1
shift
else
commandstool man try
return
fi
if [[ ! ${1:0:1} = '-' ]] && [[ ! -z $1 ]]; then
commandstool man try
return
fi
while [[ ${1:0:1} = '-' ]]; do
N=1
L=${#1}
while [[ $N -lt $L ]]; do
case ${1:$N:1} in
'p')
TYPE="pack"
;;
'c')
TYPE="command"
;;
'n')
if [[ $N -ne $(($L-1)) || ! -n ${2} ]]; then
commandstool man try
return
fi
FILENAME=$2
shift
;;
*)
commandstool man try
return
;;
esac
N=$(($N+1))
done
shift
done
PACK_REPO="https://raw.githubusercontent.com/AdrianVovk/CommandsTool/master/packs/${IN##*/}"
COMMAND_REPO="https://raw.githubusercontent.com/AdrianVovk/CommandsTool/master/commands/${IN##*/}"
if curl --output /dev/null --silent --head --fail "$PACK_REPO"; then
URL=$PACK_REPO
if [ -z $FILENAME ]; then
FILENAME=$IN
fi
if [ "$TYPE" = "pack" ]; then
read -n1 -p "$FILENAME is a CommandPack and was found in the repo. Try it? [y/n]:" permission && echo
elif [ "$TYPE" = "command" ]; then
read -n1 -p "$FILENAME is a CommandPack (that will be installed as a command by your request) and was found in the repo. Try it? [y/n]:" permission && echo
else
TYPE="pack"
read -n1 -p "$FILENAME is a CommandPack and was found in the repo. Try it? [y/n]:" permission && echo
fi
elif curl --output /dev/null --silent --head --fail "$COMMAND_REPO"; then
URL=$COMMAND_REPO
if [ -z $FILENAME ]; then
FILENAME=$IN
fi
if [ $TYPE = "pack" ]; then
read -n1 -p "$FILENAME is a CommandPack and was found in the repo. Try it? [y/n]:" permission && echo
elif [ $TYPE = "command" ]; then
read -n1 -p "$FILENAME is a command (that will be installed as a CommandPack by your request) and was found in the repo. Try it? [y/n]:" permission && echo
else
TYPE="command"
read -n1 -p "$FILENAME is a command and was found in the repo. Try it? [y/n]:" permission && echo
fi
elif curl --output /dev/null --silent --head --fail "$IN"; then
if [ -z $FILENAME ]; then
FILENAME=${IN##*/}
fi
read -n1 -p "$FILENAME may damage your computer. Make sure you know what this script does. Try it? [y/n]:" permission && echo
URL=$IN
if [ $TYPE = "pack" ]; then
echo "Trying as CommandPack"
elif [ $TYPE = "command" ]; then
echo "Trying as command"
else
TYPE="command"
echo "Trying as command"
fi
else
echo "No such URL or command exists."
echo "Abort"
return
fi
if [ "$permission" = "y" ]; then
mkdir ~/.commandstool/tmp &> /dev/null
wget -O ~/.commandstool/tmp/$FILENAME $URL &> /dev/null
chmod +x ~/.commandstool/tmp/$FILENAME
echo "There is a small chance the command failed to download. Please try to run the command to make sure it worked."
echo "The command will be removed from your computer the next time you log into bash. All of the functions of this command should work."
# Execute the script as needed
if [ "$TYPE" = "pack" ]; then
echo
source ~/.commandstool/tmp/$FILENAME
$FILENAME 2> /dev/null # Try to run the pack's splash if it has one
rm -rf ~/.commandstool/tmp/ # Delete the file now because we can.
echo
else
alias $FILENAME="source ~/.commandstool/tmp/$FILENAME"
# Cleanup is done by init so we don't delete any files now.
fi
else
echo Abort
fi
;;
'install')
# If the user overrided anything, use it
# Adapted from http://bsdpants.blogspot.com/2007/02/option-ize-your-shell-scripts.html
unset FILENAME
unset TYPE
if [[ ! ${1:0:1} = '-' ]] && [[ ! -z $1 ]]; then
IN=$1
shift
else
commandstool man install
return
fi
if [[ ! ${1:0:1} = '-' ]] && [[ ! -z $1 ]]; then
commandstool man install
return
fi
while [[ ${1:0:1} = '-' ]]; do
N=1
L=${#1}
while [[ $N -lt $L ]]; do
case ${1:$N:1} in
'p')
TYPE="pack"
;;
'c')
TYPE="command"
;;
'n')
if [[ $N -ne $(($L-1)) || ! -n ${2} ]]; then
commandstool man install
return
fi
FILENAME=$2
shift
;;
*)
commandstool man install
return
;;
esac
N=$(($N+1))
done
shift
done
PACK_REPO="https://raw.githubusercontent.com/AdrianVovk/CommandsTool/master/packs/${IN##*/}"
COMMAND_REPO="https://raw.githubusercontent.com/AdrianVovk/CommandsTool/master/commands/${IN##*/}"
if curl --output /dev/null --silent --head --fail "$PACK_REPO"; then
URL=$PACK_REPO
if [ -z $FILENAME ]; then
FILENAME=$IN
fi
if [ -f ~/.commandstool/plugins/$FILENAME ]; then
echo "$FILENAME is already installed."
return
fi
if [ "$TYPE" = "pack" ]; then
read -n1 -p "$FILENAME is a CommandPack and was found in the repo. Install it? [y/n]:" permission && echo
elif [ "$TYPE" = "command" ]; then
read -n1 -p "$FILENAME is a CommandPack (that will be installed as a command by your request) and was found in the repo. Install it? [y/n]:" permission && echo
else
TYPE="pack"
read -n1 -p "$FILENAME is a CommandPack and was found in the repo. Install it? [y/n]:" permission && echo
fi
elif curl --output /dev/null --silent --head --fail "$COMMAND_REPO"; then
URL=$COMMAND_REPO
if [ -z $FILENAME ]; then
FILENAME=$IN
fi
if [ -f ~/.commandstool/plugins/$FILENAME ]; then
echo "$FILENAME is already installed."
return
fi
if [ $TYPE = "pack" ]; then
read -n1 -p "$FILENAME is a CommandPack and was found in the repo. Install it? [y/n]:" permission && echo
elif [ $TYPE = "command" ]; then
read -n1 -p "$FILENAME is a command (that will be installed as a CommandPack by your request) and was found in the repo. Install it? [y/n]:" permission && echo
else
TYPE="command"
read -n1 -p "$FILENAME is a command and was found in the repo. Install it? [y/n]:" permission && echo
fi
elif curl --output /dev/null --silent --head --fail "$IN"; then
if [ -z $FILENAME ]; then
FILENAME=${IN##*/}
fi
if [ -f ~/.commandstool/plugins/$FILENAME ]; then
echo "$FILENAME is already installed."
return
fi
read -n1 -p "$FILENAME may damage your computer. Make sure you know what this script does. Install it? [y/n]:" permission && echo
URL=$IN
if [ ! $TYPE = "pack" ]; then
TYPE="command"
echo "Installing as command"
else
echo "Installing as CommandPack"
fi
if [ $TYPE = "pack" ]; then
echo "Installing as CommandPack"
elif [ $TYPE = "command" ]; then
echo "Installing as command"
else
TYPE="command"
echo "Installing as command"
fi
else
echo "No such URL or command exists."
echo "Abort"
return
fi
if [ "$permission" = "y" ]; then
wget -O ~/.commandstool/plugins/$FILENAME $URL &> /dev/null
echo "echo \"Updating $FILENAME\"" >> $UPDATE_SCRIPT
echo "rm $HOME/.commandstool/plugins/$FILENAME" >> $UPDATE_SCRIPT
echo "wget -O ~/.commandstool/plugins/$FILENAME $URL &> /dev/null" >> $UPDATE_SCRIPT
echo "chmod +rx ~/.commandstool/plugins/$FILENAME" >> $UPDATE_SCRIPT
chmod +rx ~/.commandstool/plugins/$FILENAME
mkdir -p $PLUGINS_DATA_STORE/$FILENAME/
if [ "$TYPE" = "pack" ]; then
echo "source $HOME/.commandstool/plugins/$FILENAME" >> $INSTALLED_SCRIPTS_LOCATION # It is a CommandPack
source $INSTALLED_SCRIPTS_LOCATION # Update the environment
$FILENAME 2> /dev/null # Try to run the pack's splash if it has one
else
echo "alias $FILENAME='source $HOME/.commandstool/plugins/$FILENAME'" >> $INSTALLED_SCRIPTS_LOCATION # It is a command
source $INSTALLED_SCRIPTS_LOCATION # Update the environment
fi
else
echo Abort
fi
;;
'uninstall')
if [ -f ~/.commandstool/plugins/$1 ]; then
FILENAME=$1
else
FILENAME=${1##*/}
if [ ! -f ~/.commandstool/plugins/$FILENAME ]; then
echo "Plugin not found. Abort"
return
fi
fi
read -n1 -p "Are you sure you want to uninstall $FILENAME? [y/n]:" permission && echo
if [ "$permission" = "y" ]; then
rm ~/.commandstool/plugins/$FILENAME
rm -rf $PLUGINS_DATA_STORE/$FILENAME/
sed -n '/'$FILENAME'/!p' $UPDATE_SCRIPT >> ~/.commandstool/tmp_update_script && mv ~/.commandstool/tmp_update_script $UPDATE_SCRIPT
sed -n '/'$FILENAME'/!p' $INSTALLED_SCRIPTS_LOCATION >> ~/.commandstool/tmp_installed_s && mv ~/.commandstool/tmp_installed_s $INSTALLED_SCRIPTS_LOCATION
update_bash
else
echo Abort
fi
;;
'update')
source $UPDATE_SCRIPT
if [ ! "$1" = "-c" ]; then
echo "Updating Commands Tool"
commandstool update_ct
fi
update_bash
;;
'update_ct')
mkdir ~/.commandstool/tmp/ &> /dev/null
wget -O ~/.commandstool/tmp/patch https://raw.githubusercontent.com/AdrianVovk/CommandsTool/master/ct_update_patch &> /dev/null
commandstool patch_ct ~/.commandstool/tmp/patch
rm -rf ~/.commandstool/tmp/ &> /dev/null
update_bash
;;
'patch_ct')
# Replace the file
rm $COMMANDS_TOOL_FILE &> /dev/null
wget -O $COMMANDS_TOOL_FILE https://raw.githubusercontent.com/AdrianVovk/CommandsTool/master/commandstool &> /dev/null
chmod +rx $COMMANDS_TOOL_FILE
source $1 #Code for patches
# Manually notify bash
;;
'reset')
read -n1 -p "Are you sure you want to do this? [y/n]:" permission && echo
if [ "$permission" = "y" ]; then
mv $COMMANDS_TOOL_FILE ~/.ct_backup # Just in case commandstool was installed inside of ~/.commandstool (Which might even be the default eventually)
rm -rf ~/.commandstool # Delete the install folder.
mkdir -p `dirname $COMMANDS_TOOL_FILE` &> /dev/null
mv ~/.ct_backup $COMMANDS_TOOL_FILE # Move the backup back
source $COMMANDS_TOOL_FILE init_ct
echo "$COMMANDS_TOOL_FILE" > ~/.commandstool/config/script_location # After creating the proper direcories again, save the install location back into the config.
update_bash
else
echo Abort
fi
;;
'uninstall_ct')
read -n1 -p "Are you sure you want to do this? [y/n]:" permission && echo
if [ "$permission" = "y" ]; then
sed -n '/'1495246'/!p' ~/.bashrc >> ~/.commandstool/tmp_bashrc && mv ~/.commandstool/tmp_bashrc ~/.bashrc
rm $COMMANDS_TOOL_FILE
if [ ! "$1" = "-k" ]; then
rm -rf ~/.commandstool
fi
update_bash
else
echo Abort
fi
;;
'edit')
nano $INSTALLED_SCRIPTS_LOCATION
update_bash
;;
'edit_ct')
nano $COMMANDS_TOOL_FILE
update_bash
;;
'edit_update_script')
nano $UPDATE_SCRIPT
update_bash
;;
'test_install')
echo "Commands Tool was successfully installed."
;;
'man' | 'help')
if [ -z $1 ]; then
commandstool man man
return
fi
case $1 in
'try')
echo "- try: Allows you to try out commands without installing them"
echo " Usage: 'commandstool try PLUGIN [-n name] [-p or -c]'"
echo " -n: Use the provided name instead of the automatically generated one. You will use this to call the command (if it is not a CommandPack. If it is, this does nothing)"
echo " -p: Treat this plugin as a CommandPack"
echo " -c: Treat this plugin as a command"
;;
'install')
echo "- install: Installs the provided script or CommandPack"
echo " Usage: 'commandstool install PLUGIN [-n name] [-p or -c]'"
echo " -n: Use the provided name instead of the automatically generated one. You will use this to call the command and reference it within commandstool"
echo " -p: Treat this plugin as a CommandPack"
echo " -c: Treat this plugin as a command"
;;
'uninstall')
echo "- uninstall: Uninstalls the provided command or CommandPack"
echo " Usage: 'commandstool uninstall PLUGIN'"
;;
'update')
echo "- update: Update all of the installed commands and CommandPacks"
echo " Usage: 'commandstool update [-c]"
echo " -c: Do not update CommandsTool while updating all of the plugins"
;;
'update_ct')
echo "- update_ct: Allowing you to update CommandsTool without touching installed scripts"
;;
'uninstall_ct')
echo "- uninstall_ct: Uninstall CommandsTool from your system. If you installed using the installer script that will be kept."
echo " -k: Keep installed plugins and data."
;;
'reset')
echo "- reset: Resets CommandsTool removing all installed scripts"
;;
'edit')
echo "- edit: Allows you to edit the runner script. Recommended for advanced users only."
;;
'edit_ct')
echo "- edit_ct: Allows you to edit CommandsTool itself. Recommended for advanced users only."
update_bash
;;
'edit_update_script')
echo "- edit_update_script: Allows you to edit the update script. Recommended for advanced users only."
;;
'man' | 'help')
echo "- man (or help): Gives you what each command does and their usage."
echo " Usage: 'commandstool man COMMAND'"
;;
'about')
echo "- about: Explains what CommandsTool can do"
;;
'commands')
echo "- commands: Show the list of commands that can be used"
;;
*)
echo "No such command"
commandstool man man
echo
esac
;;
'about')
echo "======================================================================"
echo "CommandsTool"
echo "Created by Adrian Vovk"
echo "======================================================================"
echo
echo "CommandsTool: A script that can be used to install other scripts onto your system."
echo "Definitions:"
echo "- Command: A script that CommandsTool allows you to execute as a command (example: 'memtest test -a')"
echo "- CommandPack: A plugin designed for CommandsTool that will add a set of commands into your environment"
echo "Version: 1.0"
;;
'commands')
echo "List of possible commands:"
commandstool man try
commandstool man install
commandstool man uninstall
commandstool man update
commandstool man update_ct
commandstool man uninstall_ct
commandstool man reset
commandstool man edit
commandstool man edit_ct
commandstool man edit_update_script
commandstool man man
commandstool man about
commandstool man commands
echo
echo "List of preinstalled Commands:"
echo "- e: Shorthand for 'nano'"
echo "- ct: Shorthand for 'commandstool'"
echo "- edit_bashrc: Shortcut for editing the bashrc file."
echo "- update_bash: Reset your environment. This allows you to emulate restarting your terminall application with a command"
;;
init_ct)
# If things have not been configured yet, configure them
mkdir ~/.commandstool &> /dev/null
mkdir ~/.commandstool/plugins &> /dev/null
mkdir ~/.commandstool/plugins_storage &> /dev/null
mkdir ~/.commandstool/config &> /dev/null
rm -rf ~/.commandstool/tmp # Delete the temp folder. This will delete any commands downloaded to try
if [ ! -f "$HOME/.commandstool/runner" ]; then
touch ~/.commandstool/runner
fi
if [ ! -f "$HOME/.commandstool/config/script_location" ]; then
echo "$HOME/.bash_aliases" > ~/.commandstool/config/script_location
fi
if [ ! -f "$HOME/.commandstool/update_script" ]; then
touch ~/.commandstool/update_script
fi
# Print a header message if this is the first run
if [ ! -f ~/.commandstool/config/didrun ]; then
touch ~/.commandstool/config/didrun
commandstool about
fi
# This goes through and loads in all of the scripts properly
source ~/.commandstool/runner
# Used to call commandstool
alias commandstool='source $(cat ~/.commandstool/config/script_location)'
alias ct="commandstool"
# Nano shortcut
# Will remove appon request
alias e='nano'
# Bash
alias edit_bashrc='nano ~/.bashrc && update_bash'
alias update_bash='exec bash'
# Enable this when developing on a PC
#alias backup="cp $COMMANDS_TOOL_FILE ~/.ctf_backup"
;;
*)
echo "Invalid command"
echo "Run 'commandstool commands' to see a list of commands and how to use them"
;;
esac
#make_command() {
# echo "alias $1='$2'" >> $INSTALLED_SCRIPTS_LOCATION
# update_bash
#}