-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsx
executable file
·246 lines (198 loc) · 5.88 KB
/
sx
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
#!/usr/bin/env bash
##? Sphynx
##?
##? Usage:
##? sx NAMESPACE COMMAND [options]
##?
##? Options:
##? sx --namespaces
##? sx --commands
##?
##? Global Options (This options can be passed to any command):
##? sx --help
##?
##?
##? Find more information at: https://github.com/hpedrorodrigues/sphynx
##?
## Root user check
if [ "${UID}" = '0' ]; then
echo '!!! You cannot run Sphynx as the root user' >&2
exit 1
fi
## Bash version check
readonly MIN_REQUIRED_BASH_VERSION='5'
if [ -z "${BASH_VERSION:-}" ]; then
echo '!!! Bash is required to run Sphynx' >&2
exit 1
fi
if [ -z "${BASH_VERSINFO:-}" ] || [ "${BASH_VERSINFO:-}" -lt "${MIN_REQUIRED_BASH_VERSION}" ]; then
cat >&2 <<EOF
!!! It's recommended to use Sphynx with Bash in at least version 5.0.0(1)-release (current version is ${BASH_VERSION}).
!!! Some commands may not work as expected!
EOF
fi
## Environment variables
export SPHYNX_EXEC="${BASH_SOURCE[0]}"
export SPHYNX_EXEC_NAME="$(basename "${SPHYNX_EXEC}")"
export SPHYNX_DIR="$(
cd -- "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 || exit 1
pwd -P
)"
export SPHYNX_CLI_DIR="${SPHYNX_DIR}/modules/cli"
## Helper functions
readonly error_message='The most similar namespaces (and global commands) are'
function all_namespaces() {
find "${SPHYNX_CLI_DIR}" \
-maxdepth 1 \
! -path "${SPHYNX_CLI_DIR}" \
! -path '*/.library*' \
! -path '*/.internal*' \
-a -type d \
-o -type f -a \( -perm -u=x -o -perm -g=x -o -perm -o=x \) \
| sort \
| xargs -I % basename %
}
function find_namespace() {
local -r filter="${1}"
if [ -d "${SPHYNX_CLI_DIR}/${filter}" ] || [ -f "${SPHYNX_CLI_DIR}/${filter}" ]; then
echo "${SPHYNX_CLI_DIR}/${filter}"
else
local -r namespaces="$(
find "${SPHYNX_CLI_DIR}" \
-maxdepth 1 \
! -path "${SPHYNX_CLI_DIR}" \
! -path '*/.library*' \
! -path '*/.internal*' \
-a -type d -a -name "${filter}*" \
-o -type f -a -name "${filter}*" -a \( -perm -u=x -o -perm -g=x -o -perm -o=x \)
)"
local -r n_namespaces=$(echo "${namespaces}" | wc -l | tr -d ' ')
if [ "${n_namespaces}" -gt 1 ]; then
echo "${error_message}:"
for namespace in ${namespaces}; do
echo " $(basename "${namespace}")"
done
exit 1
else
echo "${namespaces}"
fi
fi
}
function commands() {
local -r namespace="$(find_namespace "${1}")"
# shellcheck disable=SC2038 # Use -print0/-0 or find -exec + to allow for non-alphanumeric filenames
find "${namespace}" \
-maxdepth 1 \
! -path "${namespace}" \
-type f \
\( -perm -u=x -o -perm -g=x -o -perm -o=x \) \
| xargs -I % basename % \
| sort
}
function is_global_command() {
local -r filter="$(basename "${1}")"
find "${SPHYNX_CLI_DIR}" \
-maxdepth 1 \
! -path "${SPHYNX_CLI_DIR}" \
! -path '*/.library*' \
! -path '*/.internal*' \
-a -type f -a -name "${filter}*" -a \( -perm -u=x -o -perm -g=x -o -perm -o=x \) \
| grep -E '.*' &>/dev/null
}
function print_help() {
grep '[#]#?' "${SPHYNX_EXEC}" | cut -c 5-
}
function print_namespaces() {
echo -e 'Available namespaces (and global commands):\n'
all_namespaces | xargs -I % echo "> %"
}
function print_commands() {
local -r n_arguments="${#}"
echo 'Available commands:'
if [ ${n_arguments} -eq 0 ]; then
for namespace in $(all_namespaces); do
echo -e "\n> ${namespace}"
commands "${namespace}" | xargs -I % echo "|-- %"
done
elif [ ${n_arguments} -eq 1 ]; then
echo && commands "${1}" | xargs -I % echo "|-- %"
else
echo "!!! Wrong number of arguments supplied. Expected (0|1) but received (${n_arguments})" >&2
exit 1
fi
}
function check_arguments() {
local -r n_arguments="${#}"
if [ ${n_arguments} -lt 1 ] || [[ ${*} == '--help' ]]; then
print_help
exit 0
fi
if [[ ${*} == *'--github'* ]]; then
if [ ${n_arguments} = 1 ]; then
echo '!!! The flag "--github" cannot be used with the base command' >&2
exit 1
elif [ ${n_arguments} = 2 ] && ! is_global_command "${1}"; then
echo '!!! The flag "--github" cannot be used with namespaces' >&2
exit 1
fi
fi
if [[ ${*} == *'--raw'* ]]; then
if [ ${n_arguments} = 1 ]; then
if hash 'bat' 2>/dev/null; then
local -r printer='bat --plain --paging never --language bash'
else
local -r printer='cat'
fi
command ${printer} "${SPHYNX_EXEC}"
exit 0
elif [ ${n_arguments} = 2 ] && ! is_global_command "${1}"; then
echo '!!! The flag "--raw" cannot be used with namespaces' >&2
exit 1
fi
fi
if [[ ${*} == *'--commands'* ]] && [ ${n_arguments} = 1 ]; then
print_commands
exit 0
fi
if [[ ${*} == *'--namespaces'* ]] && [ ${n_arguments} = 1 ]; then
print_namespaces
exit 0
fi
local -r filter="${1}"
local -r namespace="$(find_namespace "${filter}")"
if [ -z "${namespace}" ]; then
echo -e "!!! No namespace matches for \"${filter}\".\n" >&2
print_namespaces >&2
exit 1
fi
if [[ ${namespace} == "${error_message}"* ]]; then
echo -e "!!! More than one match found for \"${filter}\".\n" >&2
echo "${namespace}" >&2
exit 1
fi
if [ "${n_arguments}" -lt 2 ] && [ ! -f "${namespace}" ]; then
print_commands "${filter}" >&2
exit 1
fi
if [ ! -f "${namespace}/${2}" ] && [ ! -f "${namespace}" ]; then
local -r pretty_namespace="$(basename "${namespace}")"
echo -e "!!! The command \"${2}\" does not exist at namespace **${pretty_namespace}**.\n" >&2
print_commands "${filter}" >&2
exit 1
fi
}
function main() {
check_arguments "${@}"
local -r namespace="$(find_namespace "${1}")"
if [ -f "${namespace}" ]; then
local -r cmd="${namespace}"
local -r shift='2'
else
local -r cmd="${namespace}/${2}"
local -r shift='3'
fi
export SPHYNX_NAMESPACE_DIR="${namespace}"
exec "${cmd}" "${@:shift}"
}
## Running
main "${@}"