Improved external sync script, added push/configure and custom commit message

This commit is contained in:
nise.moe 2024-03-07 22:18:15 +01:00
parent 53bd826e91
commit 565371facf

View File

@ -1,49 +1,83 @@
#!/bin/bash #!/bin/bash
# Argument check and validation # Initialize variables
if [ -z "$1" ]; then commit_message=""
echo "Error: Please provide an action (sync or create) and a folder name." action=""
exit 1 folder_to_sync=""
fi
action="$1"
folder_to_sync="$2"
external_base_dir="../external-nise.moe" external_base_dir="../external-nise.moe"
external_dir="$external_base_dir/$folder_to_sync"
# Manual argument parsing
while [ "$#" -gt 0 ]; do
case "$1" in
sync|create|configure|push)
action="$1"
folder_to_sync="$2"
shift 2
;;
-m)
commit_message="$2"
shift 2
;;
*)
echo "Invalid argument: $1"
exit 1
;;
esac
done
# Validation # Validation
if [ -z "$folder_to_sync" ]; then if [ -z "$action" ]; then
echo "Error: Please provide a folder name." echo "Error: Please provide an action (sync or create)."
elif [ ! -d "$folder_to_sync" ]; then
echo "Error: Folder '$folder_to_sync' does not exist in the monorepo."
exit 1 exit 1
fi fi
# Action Handling if [ -z "$folder_to_sync" ]; then
echo "Error: Please provide a folder name."
exit 1
fi
target_folder="$external_base_dir/$folder_to_sync"
# Further validation and action handling
if [ "$action" == "sync" ]; then if [ "$action" == "sync" ]; then
# Create external directory if needed if [ ! -d "$folder_to_sync" ]; then
mkdir -p "$external_dir" echo "Error: Folder '$folder_to_sync' does not exist in the monorepo."
exit 1
fi
# Synchronization using rsync mkdir -p "$target_folder"
echo "Synchronizing files from '$folder_to_sync' to '$external_dir'..." echo "Synchronizing files from '$folder_to_sync' to '$target_folder'..."
rsync -av --delete --exclude-from='.gitignore' --exclude='.git' "$folder_to_sync/" "$external_dir/" rsync -av --delete --exclude-from='.gitignore' --exclude='.git' "$folder_to_sync/" "$target_folder/"
# Change directory for committing cd "$target_folder" || { echo "Error: Failed to change directory to $target_folder"; exit 1; }
cd "$external_dir" || { echo "Error: Failed to change directory to $external_dir"; exit 1; }
# Git commit
git add . git add .
git commit -S -am "$(date +%Y%m%d)" if [ -z "$commit_message" ]; then
commit_message=$(date +%Y%m%d)
fi
git commit -S -am "$commit_message"
echo "Synchronization complete and changes committed." echo "Synchronization complete and changes committed with message: '$commit_message'."
elif [ "$action" == "configure" ]; then
echo "Configuring external folder '$target_folder'..."
cd "$target_folder" || { echo "Error: Failed to change directory to $target_folder"; exit 1; }
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
elif [ "$action" == "push" ]; then
echo "Pushing changes to remote repository..."
cd "$target_folder" || { echo "Error: Failed to change directory to $target_folder"; exit 1; }
git push origin main
echo "Changes pushed to remote repository."
elif [ "$action" == "create" ]; then elif [ "$action" == "create" ]; then
# Create external directory mkdir -p "$target_folder"
mkdir -p "$external_dir"
# Change directory (with error handling) cd "$target_folder" || { echo "Error: Failed to change directory to $target_folder"; exit 1; }
cd "$external_dir" || { echo "Error: Failed to change directory to $external_dir"; exit 1; }
# Git Initialization
git init git init
git config user.email "162507023+nise-moe@users.noreply.github.com" git config user.email "162507023+nise-moe@users.noreply.github.com"
git config user.name "nise.moe" git config user.name "nise.moe"
@ -53,9 +87,8 @@ elif [ "$action" == "create" ]; then
git branch -M main git branch -M main
git remote add origin git@github.com:nise-moe/"$folder_to_sync".git 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." echo "External folder '$target_folder' created and initialized as a Git repository."
else else
echo "Error: Invalid action. Please choose 'sync' or 'create'." echo "Error: Invalid action '$action'."
exit 1 exit 1
fi fi