diff --git a/local/bin/x-clean-vendordirs b/local/bin/x-clean-vendordirs index 4f1fa48..6b880ff 100755 --- a/local/bin/x-clean-vendordirs +++ b/local/bin/x-clean-vendordirs @@ -7,13 +7,15 @@ # Author: Ismo Vuorinen 2025 # License: MIT +set -euo pipefail + # Check if the user has provided a directory as an argument -if [ "$1" ]; then +if [ "${1:-}" ]; then # Check if the directory exists if [ -d "$1" ]; then CLEANDIR="$1" else - msgr err "Directory $1 does not exist." + echo "Error: Directory $1 does not exist." >&2 exit 1 fi else @@ -27,7 +29,7 @@ remove_node_modules_vendor() # If the directory is a symlink, skip it if [ -L "$dir" ]; then - msgr msg "Skipping symlink $dir" + echo "Skipping symlink $dir" return fi @@ -35,18 +37,18 @@ remove_node_modules_vendor() if [ -d "$dir" ]; then # If node_modules or vendor folder exists, remove it and all its contents if [ -d "$dir/node_modules" ]; then - msgr run "Removing $dir/node_modules" + echo "Removing $dir/node_modules" rm -rf "$dir/node_modules" fi if [ -d "$dir/vendor" ]; then - msgr run "Removing $dir/vendor" + echo "Removing $dir/vendor" rm -rf "$dir/vendor" fi # Recursively check subdirectories for item in "$dir"/*; do - remove_node_modules_vendor "$item" + [ -d "$item" ] && remove_node_modules_vendor "$item" done fi } diff --git a/local/bin/x-foreach b/local/bin/x-foreach index a2a84e2..76fe5b9 100755 --- a/local/bin/x-foreach +++ b/local/bin/x-foreach @@ -1,18 +1,26 @@ #!/usr/bin/env bash # -# foreach -# foreach "ls -d */" "git status" # run git status in each folder +# Run a command in each directory matching a pattern. +# +# Usage: x-foreach [args...] +# x-foreach "ls -d */" "git status" # # Source: https://github.com/mvdan/dotfiles/blob/master/.bin/foreach -cmd=$1 +set -euo pipefail + +if [ $# -lt 2 ]; then + echo "Usage: $0 [args...]" + exit 1 +fi + +listing=$1 shift -for dir in $($cmd); do +for dir in $(eval "$listing"); do ( echo "$dir" cd "$dir" || exit 1 - # shellcheck disable=SC2294,SC2034 - eval "$@" # allow multiple commands like "foo && bar" + "$@" ) done diff --git a/local/bin/x-ip b/local/bin/x-ip index ec35544..83e808c 100755 --- a/local/bin/x-ip +++ b/local/bin/x-ip @@ -1,4 +1,14 @@ #!/usr/bin/env bash +# +# Display external IP address. +# # Source: https://github.com/thirtythreeforty/dotfiles/blob/master/bin/extip -curl icanhazip.com "${@}" +set -euo pipefail + +if ! command -v curl &> /dev/null; then + echo "Error: curl is required" >&2 + exit 1 +fi + +curl -sf icanhazip.com