Created
December 6, 2024 11:11
-
-
Save Codehunter-py/284e9c6edee2a4155a045cb5d2b2b722 to your computer and use it in GitHub Desktop.
A shell script that parses this YAML-formatted requirements file and clones the specified Git repositories.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Function to parse and clone repositories | |
| clone_repositories() { | |
| local requirements_file="$1" | |
| local projects_dir="${HOME}/Projects" | |
| # Validate requirements file | |
| if [ ! -f "$requirements_file" ]; then | |
| echo "Error: Requirements file not found." >&2 | |
| return 1 | |
| fi | |
| # Ensure Projects directory exists | |
| mkdir -p "$projects_dir" | |
| cd "$projects_dir" || exit 1 | |
| # Parse repositories | |
| while read -r line; do | |
| if [[ "$line" =~ ^\ *-\ *src:\ *([^ ]+)$ ]]; then | |
| repo_url="${BASH_REMATCH[1]}" | |
| # Find corresponding version | |
| # version=$(grep -A1 "src: $repo_url" "$requirements_file" | grep "version:" | sed 's/.*version:[ ]*//') | |
| # version=${version:-master} | |
| # Repository name | |
| repo_name=$(basename "$repo_url" .git) | |
| # Check if repository folder already exists | |
| if [ -d "$repo_name" ]; then | |
| echo "Skipping $repo_url: Folder '$repo_name' already exists." | |
| continue | |
| fi | |
| echo "Cloning $repo_url (version: $version)" | |
| if git clone "$repo_url"; then | |
| cd "$repo_name" | |
| cd .. | |
| else | |
| echo "Failed to clone $repo_url" >&2 | |
| fi | |
| fi | |
| done < "$requirements_file" | |
| } | |
| # Main script execution | |
| main() { | |
| if [ $# -eq 0 ]; then | |
| echo "Usage: $0 <requirements_file>" >&2 | |
| exit 1 | |
| fi | |
| clone_repositories "$1" | |
| } | |
| # Call main with all script arguments | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment