-
Notifications
You must be signed in to change notification settings - Fork 3
/
thimble.sh
107 lines (94 loc) · 2.6 KB
/
thimble.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
#!/bin/bash
# thimble.sh
# to use: source thimble.sh
# Define command-script pairs
command_scripts=""
command_scripts="$command_scripts start:start_server"
command_scripts="$command_scripts upgrade:upgrade_from_repo"
command_scripts="$command_scripts fix:fix_line_endings"
command_scripts="$command_scripts commit:commit_files"
# Add new commands here in the format:
# command_scripts="$command_scripts command_name:script_name"
# Function to find and run a script
run_script() {
script_name="$1"
shift # Remove the first argument (script_name)
# Find all scripts matching the name
scripts=$(find template -name "${script_name}.*")
if [ -z "$scripts" ]; then
echo "No ${script_name} scripts found."
return 1
fi
# Filter scripts based on available interpreters
available_scripts=""
for script in $scripts; do
case "${script##*.}" in
py)
command -v python3 >/dev/null 2>&1 && available_scripts="$available_scripts $script"
;;
pl)
command -v perl >/dev/null 2>&1 && available_scripts="$available_scripts $script"
;;
rb)
command -v ruby >/dev/null 2>&1 && available_scripts="$available_scripts $script"
;;
js)
command -v node >/dev/null 2>&1 && available_scripts="$available_scripts $script"
;;
sh)
available_scripts="$available_scripts $script"
;;
php)
command -v php >/dev/null 2>&1 && available_scripts="$available_scripts $script"
;;
esac
done
if [ -z "$available_scripts" ]; then
echo "No compatible ${script_name} scripts found for the current environment."
return 1
fi
# Choose a random script from available ones
random_script=$(echo $available_scripts | tr ' ' '\n' | sort -R | head -n 1)
echo "Running: $random_script"
case "${random_script##*.}" in
py)
python3 "$random_script" "$@"
;;
pl)
perl "$random_script" "$@"
;;
rb)
ruby "$random_script" "$@"
;;
js)
node "$random_script" "$@"
;;
sh)
bash "$random_script" "$@"
;;
php)
php "$random_script" "$@"
;;
esac
}
# Main function to handle subcommands
t() {
if [ -z "$1" ]; then
echo "Usage: t <command> [arguments...]"
echo "Available commands:"
echo "$command_scripts" | tr ' ' '\n' | cut -d':' -f1
return
fi
script_name=$(echo "$command_scripts" | tr ' ' '\n' | grep "^$1:" | cut -d':' -f2)
if [ -n "$script_name" ]; then
shift # Remove the first argument (command name)
run_script "$script_name" "$@"
else
echo "Unknown command: $1"
echo "Use 't' without arguments to see available commands."
fi
}
# Create the alias
alias t=t
echo "Thimble script loaded. Use 't <command> [arguments...]' to run commands."
# end of thimble.sh