-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IDEA: Prevent incorrect CLI calls by automatic ShellCheck validation (REPL) #1535
Comments
I've been mulling this over for a few days. I've been meaning to learn how to write bash readline widgets, and well, here I am! This overwrites the default enter (
I recommend adding a sc_verify_or_unbind(){
shellcheck -S "${SC_VERIFY_LEVEL:=info}" -s bash -x \
--exclude=2154 \
<(printf '%s\n' "$READLINE_LINE") ||
bind -x '"\C-x\C-b2": sc_verify_bind_accept'
}
sc_verify_bind_accept(){
bind '"\C-x\C-b2": accept-line'
}
sc_verify_bind_accept
bind -x '"\C-x\C-b1": sc_verify_or_unbind'
bind '"\C-m":"\C-x\C-b1\C-x\C-b2"' TODO: do some EDIT: Added |
Thank you - this is brilliant. I'll take it for a ride locally and see if there's anything that needs to be tweaked. I'm forced to use older versions of ShellCheck on different systems (e.g. Ubuntu 18.04 is still ShellCheck 0.4.6) and noticed that #!/usr/bin/env bash
## Source: https://github.com/koalaman/shellcheck/issues/1535
function sc_version() {
if [ -z "${SHELLCHECK_VERSION+x}" ]; then
# Example: '0.4.6'
SHELLCHECK_VERSION=$(shellcheck --version | grep version: | sed -E 's/version:[ ]+//')
# Example: '0.4'
SHELLCHECK_VERSION_X_Y="${SHELLCHECK_VERSION%.*}"
fi
}
function version_gt() {
test "$(printf '%s\n' "$@" | sort --version-sort | head -n 1)" != "$1"
}
function sc_repl_verify_or_unbind() {
local opts=("--shell=bash" "--external-sources")
if [ ! -z "${SHELLCHECK_REPL_EXCLUDE+x}" ]; then
opts+=("--exclude=${SHELLCHECK_REPL_EXCLUDE}")
fi
# Option -S/--severity requires ShellCheck (>= 0.6.0)
if version_gt "${SHELLCHECK_VERSION_X_Y}" 0.5; then
opts+=("--severity=\"${SC_VERIFY_LEVEL:=info}\"")
fi
shellcheck "${opts[@]}" <(printf '%s\n' "$READLINE_LINE") ||
bind -x '"\C-x\C-b2": sc_repl_verify_bind_accept'
}
function sc_repl_verify_bind_accept() {
bind '"\C-x\C-b2": accept-line'
}
function sc_repl_setup() {
SHELLCHECK_REPL_EXCLUDE=2154
sc_version
sc_repl_verify_bind_accept
bind -x '"\C-x\C-b1": sc_repl_verify_or_unbind'
bind '"\C-m":"\C-x\C-b1\C-x\C-b2"'
}
sc_repl_setup |
Even though I'm not going to be using this, I figure I'd throw some feedback your way.
No need for SHELLCHECK_VERSION="$(shellcheck --version | sed -nE 's/version: +(.+)/\1/p')" You also probably don't want to pass null exclude value, so I'd drop the if [[ -n "${SHELLCHECK_REPL_EXCLUDE}" ]]; then I'd change |
* No need for grep | sed * don't want to pass null exclude value, so I'd drop the ${ +x} * Harmonize env variables * Drop 'function' to support older versions of Bash, cf. https://github.com/dylanaraps/pure-bash-bible#function-declaration
Thank you for this feedback - learning new things every day ... just hope I'm still learning faster than I forget ;) I've incorporated it. So, I ended up setting up https://github.com/HenrikBengtsson/shellcheck-repl to host the code etc. PS. I'm closing this one to prevent more "noise" hitting this repos. Anyone interested, feel free to continue any discussions in the 'shellcheck-repl' repos. |
For new checks and feature suggestions
I'm a big user of ShellCheck (thanks - it's brilliant) and it just struck me: would it be possible to run ShellCheck on the fly at the Bash prompt preventing incorrect calls from being evaluated?
For example,
Is that possible to in the Bash read-eval-print loop (REPL)? ... in other shells?
The text was updated successfully, but these errors were encountered: