-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrun
executable file
·200 lines (174 loc) · 5.34 KB
/
wrun
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
#!/bin/bash
# Saves a little typing on the command line when running welbornprod tools.
# Searches ./scripts/ directories for pattern matches, and runs the file.
# I made this instead of creating symlinks or aliases for all of the scripts.
# Example:
# Instead of: ./scripts/restart.sh [args]...
# ..do this: ./wrun restart [args]...
# as of right now, even: ./wrun re [args]...
#
# The main reason for this:
# ./apps/paste/scripts/pastetool.py == ./wrun pastetool
# -Christopher Welborn (rewrite: 1-27-15)
appname="WRun"
appversion="0.3.0"
# script directories to use (relative to cwd)
scriptdirs=("scripts" "apps/paste/scripts")
function indent_text () {
# Indent some text by a predetermined amount.
# Arguments:
# $1 : (number) the amount of spaces.
# $2 : (string) the text to indent.
local length=$1 i spaces=""
for ((i=0; i<length; i++))
do
spaces=" $spaces"
done
printf "%s%s" "$spaces" "$2"
}
function find_script () {
# Locate a script by regex pattern in the script directories.
# Arguments:
# $1 : The regex pattern to use.
# Skip tests unless explicitly called.
local skiptest=1
[[ $1 =~ test ]] && skiptest=""
for scriptdir in "${scriptdirs[@]}"
do
for scriptpath in ${scriptdir}/*
do
scriptname="${scriptpath##*/}"
if [[ ! -x $scriptpath ]] || [[ ! -f $scriptpath ]]; then
# Non-executable scripts are skipped.
continue
elif [[ $skiptest ]] && [[ $scriptname =~ test_ ]]; then
# So are tests unless explicitly called.
continue
elif [[ ${scriptname%.*} =~ $1 ]] || [[ "$scriptname" == "$1" ]]; then
# Found a regex match, or an exact match.
printf "%s" "$(readlink -f "$scriptpath")"
return 0
fi
done
done
return 1
}
function list_script_type () {
# list a certain file extension in the scripts dir.
# ex: list_script_type ".py"
# With no arguments, all types are listed.
local lastdir=""
local scripts=""
local indent=0
for scriptdir in "${scriptdirs[@]}"
do
scripts=(${scriptdir}/*$1)
for scriptname in "${scripts[@]}"
do
if [[ ! -x $scriptname ]] || [[ ! -f $scriptname ]]; then
# Non-executable scripts are skipped.
continue
fi
if [[ "$scriptdir" == "$lastdir" ]]; then
# print without directory.
scriptname="${scriptname#$scriptdir*/}"
fmtname="$(indent_text $indent "$scriptname")"
printf " %s\n" "$fmtname"
else
# print with directory.
scriptname="$scriptname"
lastdir="$scriptdir"
indent=$((${#scriptdir} + 1))
printf "\n %s\n" "$scriptname"
fi
done
done
}
function list_scripts () {
# list command. shows python and bash scripts in scripts dir.
printf "\nCurrent Scripts:\n"
list_script_type ""
printf "\n"
}
function parse_args () {
if [[ -z $1 ]]; then
# No args.
print_usage
exit 1
elif [[ $1 =~ ^-[-]?h(elp)? ]]; then
# -h,--help , or no arguments.
print_full_usage
exit 0
elif [[ $1 =~ ^-[-]?l(ist)? ]]; then
# -l,--list
list_scripts
exit 0
elif [[ $1 =~ ^-[-]?v(ersion)? ]]; then
# -v,--version
printf "\n%s v. %s\n\n" "$appname" "$appversion"
exit 0
fi
}
function print_fail () {
# prints a message and fails with exit code 1
print_status "$@"
exit 1
}
function print_status () {
# Print status that clearly shows the msg coming from wrun.
if [[ -n $2 ]]; then
# Use custom format.
# shellcheck disable=SC2059
printf "[wrun] $1" "$2"
else
printf "[wrun] %s\n" "$1"
fi
}
function print_full_usage () {
echo "
$appname v. $appversion
Runs executable scripts without typing their full name or path.
Instead of doing './scripts/mything.py', './wrun my' works.
"
print_usage
exit 0
}
function print_usage () {
echo "
Usage: $0 <scriptname> [arguments]...
Options:
<scriptname> : Name or regex pattern for script.
-h,--help : Show this message.
-l,--list : List known scripts.
-v,--version : Show $appname version and exit.
"
}
function valid_scriptdirs () {
# test scriptdirs
local index=0
for scriptdir in "${scriptdirs[@]}"; do
if [[ ! -d $scriptdir ]]; then
# Running from scripts dir already?
newscriptdir="../$scriptdir"
[[ -d "$newscriptdir" ]] || print_fail "script dir not found!: $scriptdir"
scriptdirs[$index]=$newscriptdir
fi
let index+=1
done
}
# Make sure all script directories are valid.
valid_scriptdirs
# Test for wrun args.
parse_args "$@"
# Locate the script by pattern/name.
scriptname="$1"
scriptpath="$(find_script "$scriptname")"
if [[ -z "$scriptpath" ]]; then
print_fail "Script not found: $scriptname"
fi
# Grab arguments for the script.
scriptargs=("${@:2}")
scriptcmd="${scriptpath##*/} ${scriptargs[*]}"
# Run it.
print_status "%s\n\n" "Running: ${scriptcmd}"
"$scriptpath" "${scriptargs[@]}"