-
-
Notifications
You must be signed in to change notification settings - Fork 1
fix: replace raw echo with output helpers in check #706
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
base: main
Are you sure you want to change the base?
Changes from all commits
c107189
111228a
4673a79
cd3db9a
1b089c1
29e568b
2b9127a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,22 +1,28 @@ | ||||||||||||
| #! /bin/bash | ||||||||||||
|
|
||||||||||||
| die() { | ||||||||||||
| echo "$@" | ||||||||||||
| if [ -t 2 ]; then | ||||||||||||
| printf '\n\033[31m✗\033[0m %s\n' "$*" >&2 | ||||||||||||
| else | ||||||||||||
| printf '\n✗ %s\n' "$*" >&2 | ||||||||||||
| fi | ||||||||||||
| exit 1 | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| testing() { | ||||||||||||
| echo " + $*" | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| hint() { | ||||||||||||
| echo " ? CHECK: $*" | ||||||||||||
| success() { | ||||||||||||
| if [ -t 1 ]; then | ||||||||||||
| printf '\n\033[32m✓\033[0m %s\n' "$*" | ||||||||||||
| else | ||||||||||||
| printf '\n✓ %s\n' "$*" | ||||||||||||
| fi | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fail() { | ||||||||||||
| echo " - ERROR: $*" | ||||||||||||
| exit 1 | ||||||||||||
|
|
||||||||||||
| info() { | ||||||||||||
| if [ -t 1 ]; then | ||||||||||||
| printf '\n\033[32m→\033[0m %s\n' "$*" | ||||||||||||
| else | ||||||||||||
| printf '\n→ %s\n' "$*" | ||||||||||||
| fi | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
@@ -39,7 +45,7 @@ excluded() { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| # Find all the script files both executable and ones with shebang | ||||||||||||
| FILES=$({ \ | ||||||||||||
| mapfile -t FILES < <({ \ | ||||||||||||
| find "$BASEDIR" -not -path '*/.*' -type f -executable -print || die "Search: find" | ||||||||||||
| while IFS= read -r SHEBANG_FILE; do | ||||||||||||
| IFS= read -r FIRST_LINE < "$SHEBANG_FILE" || continue | ||||||||||||
|
|
@@ -52,47 +58,51 @@ FILES=$({ \ | |||||||||||
| done < <(find "$BASEDIR" -not -path '*/.*' -type f -print) || die "Search: Shebang" | ||||||||||||
| } | sort | uniq) | ||||||||||||
|
|
||||||||||||
| echo "Testing:" | ||||||||||||
| for FILE in $FILES; do | ||||||||||||
| echo "* $FILE" | ||||||||||||
| testing "Executable" | ||||||||||||
| [ "${#FILES[@]}" -gt 0 ] || die "No script files found in ${BASEDIR}" | ||||||||||||
|
|
||||||||||||
| info "Testing:" | ||||||||||||
| for FILE in "${FILES[@]}"; do | ||||||||||||
| info "* $FILE" | ||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing: The fix is to read into an array and iterate with mapfile -t FILES < <({ \
find "$BASEDIR" -not -path '*/.*' -type f -executable -print || die "Search: find"
...
} | sort -u)
for FILE in "${FILES[@]}"; doThis is adjacent to the changes in this PR — worth fixing here while the loop is being touched.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in cd3db9a — replaced |
||||||||||||
| info "Executable" | ||||||||||||
| if [ ! -x "$FILE" ]; then | ||||||||||||
| fail "Not Executable" | ||||||||||||
| die "Not Executable: ${FILE}" | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| testing "Shebang" | ||||||||||||
| info "Shebang" | ||||||||||||
| FIRST_LINE=$(head -1 "$FILE") | ||||||||||||
| if [[ $FIRST_LINE =~ $REGEX_SHEBANG ]]; then | ||||||||||||
| SHEBANG_EXEC="${BASH_REMATCH[1]}" | ||||||||||||
| SHELL= | ||||||||||||
| if [ "$SHEBANG_EXEC" = "/bin/bash" ]; then | ||||||||||||
| SHELL="bash" | ||||||||||||
| bash -n -u "$FILE" || fail "$SHELL: Invalid Script" | ||||||||||||
| sh -n "$FILE" > /dev/null 2>&1 && hint "Could be run in bash?" | ||||||||||||
| bash -n -u "$FILE" || die "$SHELL: Invalid Script: ${FILE}" | ||||||||||||
| sh -n "$FILE" > /dev/null 2>&1 && info "Hint: Could be run in sh?" | ||||||||||||
| elif [ "$SHEBANG_EXEC" = "/usr/bin/env bash" ]; then | ||||||||||||
| SHELL="bash" | ||||||||||||
| bash -n -u "$FILE" || fail "$SHELL: Invalid Script" | ||||||||||||
| sh -n "$FILE" > /dev/null 2>&1 && hint "Could be run in bash?" | ||||||||||||
| bash -n -u "$FILE" || die "$SHELL: Invalid Script: ${FILE}" | ||||||||||||
| sh -n "$FILE" > /dev/null 2>&1 && info "Hint: Could be run in sh?" | ||||||||||||
| elif [ "$SHEBANG_EXEC" = "/bin/sh" ]; then | ||||||||||||
| SHELL="sh" | ||||||||||||
| sh -n "$FILE" || fail "$SHELL: Invalid Script" | ||||||||||||
| sh -n "$FILE" || die "$SHELL: Invalid Script: ${FILE}" | ||||||||||||
| elif [ "$SHEBANG_EXEC" = "/usr/bin/env sh" ]; then | ||||||||||||
| SHELL="sh" | ||||||||||||
| sh -n "$FILE" || fail "$SHELL: Invalid Script" | ||||||||||||
| sh -n "$FILE" || die "$SHELL: Invalid Script: ${FILE}" | ||||||||||||
| else | ||||||||||||
| fail "Unknown Shell: $SHEBANG_EXEC" | ||||||||||||
| die "Unknown Shell: $SHEBANG_EXEC in ${FILE}" | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| else | ||||||||||||
| fail "No Shebang: $FIRST_LINE" | ||||||||||||
| die "No Shebang: $FIRST_LINE in ${FILE}" | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| ## Run Shellcheck on the file | ||||||||||||
| if [ "$(excluded "$FILE")" = "1" ]; then | ||||||||||||
| hint "Excluded from shellcheck" | ||||||||||||
| info "Excluded from shellcheck" | ||||||||||||
| else | ||||||||||||
| shellcheck "$FILE" || fail "Shellcheck" | ||||||||||||
| shellcheck "$FILE" || die "Shellcheck: ${FILE}" | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| done | ||||||||||||
|
|
||||||||||||
| success "All checks passed" | ||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Add a guard before the loop:
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 1b089c1 — added |
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
success()is defined here but is never called anywhere in the script.Every file that passes all checks exits the loop silently — the user sees a stream of
→progress lines and then nothing, making it impossible to distinguish "completed successfully" from "script terminated mid-run". The convention inshell-scripts.instructions.mdstatessuccessis for completion (green ✓). At minimum it should be called once after the loop to confirm all checks passed:And at the end of the script (after
done), add:success "All checks passed"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 111228a — added
success "All checks passed"after the loop.