nise/external-sync.sh

61 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# Argument check and validation
if [ -z "$1" ]; then
echo "Error: Please provide an action (sync or create) and a folder name."
exit 1
fi
action="$1"
folder_to_sync="$2"
external_base_dir="../external-nise.moe"
external_dir="$external_base_dir/$folder_to_sync"
# Validation
if [ -z "$folder_to_sync" ]; then
echo "Error: Please provide a folder name."
elif [ ! -d "$folder_to_sync" ]; then
echo "Error: Folder '$folder_to_sync' does not exist in the monorepo."
exit 1
fi
# Action Handling
if [ "$action" == "sync" ]; then
# Create external directory if needed
mkdir -p "$external_dir"
# Synchronization using rsync
echo "Synchronizing files from '$folder_to_sync' to '$external_dir'..."
rsync -av --delete --exclude-from='.gitignore' --exclude='.git' "$folder_to_sync/" "$external_dir/"
# Change directory for committing
cd "$external_dir" || { echo "Error: Failed to change directory to $external_dir"; exit 1; }
# Git commit
git add .
git commit -S -am "$(date +%Y%m%d)"
echo "Synchronization complete and changes committed."
elif [ "$action" == "create" ]; then
# Create external directory
mkdir -p "$external_dir"
# Change directory (with error handling)
cd "$external_dir" || { echo "Error: Failed to change directory to $external_dir"; exit 1; }
# Git Initialization
git init
git config user.email "162507023+nise-moe@users.noreply.github.com"
git config user.name "nise.moe"
git config gpg.format ssh
git config commit.gpgsign true
git config user.signingkey /home/anon/.ssh/nise-moe.pub
git branch -M main
git remote add origin git@github.com:nise-moe/"$folder_to_sync".git
echo "External folder '$external_dir' created and initialized as a Git repository."
else
echo "Error: Invalid action. Please choose 'sync' or 'create'."
exit 1
fi