runtime/add_repos.sh

72 lines
2.1 KiB
Bash
Raw Normal View History

#!/bin/bash
# Check if a YAML file is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <repositories.yaml>"
exit 1
fi
YAML_FILE=$1
# Ensure the YAML file exists
if [ ! -f "$YAML_FILE" ]; then
echo "File not found: $YAML_FILE"
exit 1
fi
# Function to extract values from YAML using yq (https://github.com/mikefarah/yq)
extract_yaml_value() {
local key=$1
local repo_name=$2
# Use brackets to handle keys with special characters
yq ".repositories[\"$repo_name\"].$key" "$YAML_FILE" | tr -d '"'
}
# Check if yq is installed
if ! command -v yq &> /dev/null; then
echo "yq could not be found. Please install yq from https://github.com/mikefarah/yq"
exit 1
fi
# Debugging: Print the entire YAML content for verification
echo "Content of $YAML_FILE:"
cat "$YAML_FILE"
# Parse the YAML file and process each repository
REPOSITORIES=$(yq '.repositories | keys_unsorted' "$YAML_FILE")
# Debugging: Print the list of repositories in raw form
echo "Raw repositories list:"
echo "$REPOSITORIES"
# Extract and clean up the list of repository names
REPOSITORIES=$(echo "$REPOSITORIES" | grep -v '^-\s*$' | tr -d '[]"' | tr ',' '\n' | sed '/^$/d')
# Debugging: Print the cleaned list of repositories
echo "Cleaned repositories list:"
echo "$REPOSITORIES"
for REPO_NAME in $REPOSITORIES; do
TYPE=$(extract_yaml_value "type" "$REPO_NAME")
URL=$(extract_yaml_value "url" "$REPO_NAME")
VERSION=$(extract_yaml_value "version" "$REPO_NAME")
# Only process repositories of type 'git'
if [ "$TYPE" == "git" ]; then
PREFIX="src/$REPO_NAME"
# Run the git subtree add command
echo "Adding repository '$REPO_NAME' with URL '$URL' and version '$VERSION' to prefix '$PREFIX'"
git subtree add --prefix="$PREFIX" "$URL" "$VERSION" --squash
# Check if the command was successful
if [ $? -ne 0 ]; then
echo "Failed to add repository '$REPO_NAME'."
exit 1
fi
else
echo "Skipping non-git repository '$REPO_NAME'."
fi
done
echo "All repositories have been added successfully."