From 6516ff47b9cae304c3356e17ebff6125faa8e4d7 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 16 Apr 2021 14:12:06 +0100 Subject: [PATCH] Update remote repositories to use ssh --- .local/bin/setup/ssh | 53 ++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/.local/bin/setup/ssh b/.local/bin/setup/ssh index c3d66a7..c6dec81 100755 --- a/.local/bin/setup/ssh +++ b/.local/bin/setup/ssh @@ -30,26 +30,41 @@ KEYS=$(curl -X GET -s --url "$KEYS_URL") KEY_EXISTS=$(echo $KEYS | jq --arg KEY "$(cat "$SSHPATH.pub")" 'contains({ key: $KEY })') -[[ $KEY_EXISTS == true ]] && return +if [[ $KEY_EXISTS != true ]]; do + # Extract the ids of the keys with the same title as this machine + IDS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(select(.title == $TITLE))[].id') -# Extract the ids of the keys with the same title as this machine -IDS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(select(.title == $TITLE))[].id') + # Loop through the keys and remove them from gitea to be replaced by the new one + if [ ! -z "$IDS" ]; then + for ID in IDS; do + curl -X DELETE \ + -s \ + -H "Accept: application/json" \ + -H "Content-Type:application/json" \ + --url "$KEYS_URL/$ID" + done + fi -# Loop through the keys and remove them from gitea to be replaced by the new one -if [ ! -z "$IDS" ]; do - for ID in IDS; do - curl -X DELETE \ - -s \ - -H "Accept: application/json" \ - -H "Content-Type:application/json" \ - --url "$KEYS_URL/$ID" - done + # Save the new key in gitea + curl -X POST \ + -s \ + -H "Accept: application/json" \ + -H "Content-Type:application/json" \ + --data "$(generate_post_data)" \ + --url "$KEYS_URL" fi -# Save the new key in gitea -curl -X POST \ - -s \ - -H "Accept: application/json" \ - -H "Content-Type:application/json" \ - --data "$(generate_post_data)" \ - --url "$KEYS_URL" +HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/" + +for dir in $(ls "$HOME/.local/src"); do + dir="$HOME/.local/src/$dir" + if [ -d $dir ]; then + cd "$dir" + SSH_URL=$(git remote get-url origin | sed "$HTTP_REPLACE") + git remote set-url origin "$SSH_URL" + fi +done + +DOTFILES_SSH_URL=$(git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" get-url origin | sed "$HTTP_REPLACE") +git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" set-url origin "$DOTFILES_SSH_URL" +