From 565371facf7d01dce574a759c11301a2e36e8e23 Mon Sep 17 00:00:00 2001 From: "nise.moe" Date: Thu, 7 Mar 2024 22:18:15 +0100 Subject: [PATCH] Improved external sync script, added push/configure and custom commit message --- external-sync.sh | 97 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/external-sync.sh b/external-sync.sh index 69390b1..5b8c0d1 100755 --- a/external-sync.sh +++ b/external-sync.sh @@ -1,49 +1,83 @@ #!/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" +# Initialize variables +commit_message="" +action="" +folder_to_sync="" 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 -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." +if [ -z "$action" ]; then + echo "Error: Please provide an action (sync or create)." exit 1 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 - # Create external directory if needed - mkdir -p "$external_dir" + if [ ! -d "$folder_to_sync" ]; then + echo "Error: Folder '$folder_to_sync' does not exist in the monorepo." + exit 1 + fi - # 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/" + mkdir -p "$target_folder" + echo "Synchronizing files from '$folder_to_sync' to '$target_folder'..." + rsync -av --delete --exclude-from='.gitignore' --exclude='.git' "$folder_to_sync/" "$target_folder/" - # Change directory for committing - cd "$external_dir" || { echo "Error: Failed to change directory to $external_dir"; exit 1; } + cd "$target_folder" || { echo "Error: Failed to change directory to $target_folder"; exit 1; } - # Git commit 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 - # Create external directory - mkdir -p "$external_dir" + mkdir -p "$target_folder" - # Change directory (with error handling) - cd "$external_dir" || { echo "Error: Failed to change directory to $external_dir"; exit 1; } + cd "$target_folder" || { echo "Error: Failed to change directory to $target_folder"; exit 1; } - # Git Initialization git init git config user.email "162507023+nise-moe@users.noreply.github.com" git config user.name "nise.moe" @@ -53,9 +87,8 @@ elif [ "$action" == "create" ]; then 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." - + echo "External folder '$target_folder' created and initialized as a Git repository." else - echo "Error: Invalid action. Please choose 'sync' or 'create'." + echo "Error: Invalid action '$action'." exit 1 fi \ No newline at end of file