From 67aeb014434ee308178ac2f4a01d1fdd70f0bf72 Mon Sep 17 00:00:00 2001 From: Michael Mendy Date: Tue, 17 Sep 2024 13:57:59 -0700 Subject: [PATCH] Improve `util-check.sh` Added a check for empty input at the beginning of the main function. If no files are provided, it prints a message and exits successfully. The script will only produce output in two scenarios: 1. When no files are provided for checking 2. When one or more invalid files are found In all other cases, it will run silently and exit with the appropriate status code. --- scripts/util-check.sh | 76 ++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/scripts/util-check.sh b/scripts/util-check.sh index f9e8748..b72f864 100755 --- a/scripts/util-check.sh +++ b/scripts/util-check.sh @@ -1,45 +1,47 @@ -#!/bin/sh - -# Ensures that utility files end with -utils.ts - -accepted_util_extension="-utils.ts" -banned_util_extensions=("-util.ts" ".util.ts" ".utils.ts" ".utility.ts" "-utility.ts" ".utilities.ts" "-utilities.ts") -changed_files=$@ -invalid_files="" - -yellow="\033[0;33m" -green="\033[0;32m" -no_color="\033[0m" - -function check_for_banned_name() { - changed_file=$1 - for i in "${banned_util_extensions[@]}" - do - banned_util_extension=$i - if [[ $changed_file == *$banned_util_extension ]] - then - invalid_files="$invalid_files $changed_file" +#!/bin/bash + +ACCEPTED_UTIL_EXTENSION="-utils.ts" +BANNED_UTIL_EXTENSIONS=("-util.ts" ".util.ts" ".utils.ts" ".utility.ts" "-utility.ts" ".utilities.ts" "-utilities.ts") +YELLOW="\033[0;33m" +GREEN="\033[0;32m" +NO_COLOR="\033[0m" + +check_for_banned_name() { + local file="$1" + for banned_extension in "${BANNED_UTIL_EXTENSIONS[@]}"; do + if [[ "$file" == *"$banned_extension" ]]; then + echo "$file" + return 0 fi done + return 1 } -for changed_file in ${changed_files} -do - check_for_banned_name $changed_file -done +main() { + if [ $# -eq 0 ]; then + echo "No files provided for checking." + exit 0 + fi -invalid_files_count=${#invalid_files} + local invalid_files=() -if [ $invalid_files_count -ne 0 ] -then - printf "\nUtility files should end with $green\"$accepted_util_extension\"$no_color\n\n" - printf "Files to rename:\n================\n" - for invalid_file in ${invalid_files} - do - printf "$yellow$invalid_file\n" + for file in "$@"; do + if invalid_file=$(check_for_banned_name "$file"); then + invalid_files+=("$invalid_file") + fi done - printf "$no_color\n" - exit 1 -fi -exit 0 + if [ ${#invalid_files[@]} -ne 0 ]; then + printf "\nUtility files should end with %s\"%s\"%s\n\n" "$GREEN" "$ACCEPTED_UTIL_EXTENSION" "$NO_COLOR" + printf "Files to rename:\n================\n" + for file in "${invalid_files[@]}"; do + printf "%s%s%s\n" "$YELLOW" "$file" "$NO_COLOR" + done + printf "\n" + exit 1 + fi + + exit 0 +} + +main "$@"