Skip to content

Instantly share code, notes, and snippets.

@cmackenzie1
Created September 3, 2024 04:01
Show Gist options
  • Save cmackenzie1/04725533c2f4ff05ea609ccdb112c8c0 to your computer and use it in GitHub Desktop.
Save cmackenzie1/04725533c2f4ff05ea609ccdb112c8c0 to your computer and use it in GitHub Desktop.
Run cargo clean on multiple directories.
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Function to display error messages and exit
error_exit() {
echo "Error: $1" >&2
exit 1
}
# Find all directories that need cleaning
dirs_to_clean=()
while IFS= read -r -d '' dir; do
if [[ -f "$dir/Cargo.toml" ]]; then
dirs_to_clean+=("$dir")
fi
done < <(find . -maxdepth 1 -type d -print0)
# If no directories need cleaning, exit
if [ ${#dirs_to_clean[@]} -eq 0 ]; then
error_exit "No Rust projects found that need cleaning."
fi
# Print the directories that need cleaning
echo "The following directories contain Cargo.toml and can be cleaned:"
for dir in "${dirs_to_clean[@]}"; do
echo " $dir"
done
# Prompt for user confirmation
read -p "Do you want to proceed with cleaning these directories? (y/n): " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
# Proceed with cleaning
for dir in "${dirs_to_clean[@]}"; do
echo "Cleaning $dir"
(cd "$dir" && cargo clean) || error_exit "Failed to clean $dir"
done
echo "Finished cleaning Rust projects."
else
echo "Operation cancelled."
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment