Improving scripts

This commit is contained in:
2026-03-14 19:15:38 +00:00
parent 23bbbbdbe8
commit 2de91ed77b
4 changed files with 76 additions and 104 deletions

View File

@@ -5,9 +5,9 @@ echo "SSH Setup Script"
echo "-------------------------------------"
echo "Extracting login details from enpass"
LABS=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show "Scarif: Labs" 2>&1)
LABSUN=$(echo "$LABS" | grep -Po "(?<=login: )\w+")
LABSPW=$(enpass-cli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort pass "Scarif: Labs")
LABS=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -json -sort show "Scarif: Labs")
LABSUN=$(echo "$LABS" | jq -r '.[].login')
LABSPW=$(echo "$LABS" | jq -r '.[].password')
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
TITLE="$USER@$(cat /etc/hostname)" # The title for the SSH key
@@ -42,19 +42,23 @@ echo "Checking if the key exists on the server"
if [[ $KEY_EXISTS == true ]]; then
echo "Found keys with the same title as this machine."
# Extract the ids of the keys with the same title as this machine
IDS=$(echo "$KEYS" | jq --arg TITLE "$TITLE" 'map(select(.title|ascii_downcase == ($TITLE|ascii_downcase)))[].id')
MATCH=$(echo "$KEYS" | jq --arg TITLE "$TITLE" 'map(select(.title|ascii_downcase == ($TITLE|ascii_downcase)))[]')
# Loop through the keys and remove them from gitea to be replaced by the new one
echo "Removing found keys to replace with this machine"
if [ ! -z "$IDS" ]; then
for ID in $IDS; do
echo "Deleting key with ID $ID"
if [[ -n "$MATCH" ]]; then
ID=$(echo "$MATCH" | jq -r '.id')
EXISTING_KEY=$(echo "$MATCH" | jq -r '.key')
if [[ "$KEY" == "$EXISTING_KEY" ]]; then
echo "Key already exists and is correct. Nothing to do."
exit 0;
else
echo "Key exists but differs. Deleting existing key ID $ID"
curl -X DELETE \
-s -S \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
--url "$KEYS_URL/$ID"
done
--url "$KEYS_URL/$ID"
fi
fi
fi
@@ -83,3 +87,4 @@ echo "Replacing remote URL to use SSH key"
DOTFILES_SSH_URL=$(git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote get-url origin | sed "$HTTP_REPLACE")
git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote set-url origin "$DOTFILES_SSH_URL"