Skip to content

Commit 36a56ef

Browse files
authored
Add self-update feature to sync-ssh-keys script and update documentation (#10)
* Add self-update feature to sync-ssh-keys script and update documentation * Bump script version to 0.0.9
1 parent e2db891 commit 36a56ef

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

.github/copilot-instructions.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This document provides comprehensive guidance for AI coding agents and contribut
1414
- Logging and error handling.
1515
- Configuration loading from `users.conf`.
1616
- A helper function `fetch_key_file` to handle key retrieval logic with retries for failed operations.
17+
- A `--self-update` option to fetch and replace the script with the latest version from the GitHub repository.
1718

1819
### Configuration
1920
- **`users.conf`**: Defines users and their key sources. Example structure:
@@ -38,6 +39,10 @@ This document provides comprehensive guidance for AI coding agents and contribut
3839
```bash
3940
./sync-ssh-keys.sh
4041
```
42+
3. To update the script to the latest version, run:
43+
```bash
44+
./sync-ssh-keys.sh --self-update
45+
```
4146

4247
### Configuration
4348
- Edit `users.conf` to define users and their key sources.
@@ -102,3 +107,8 @@ This document provides comprehensive guidance for AI coding agents and contribut
102107
- The `fetch_key_file` function includes a retry mechanism for failed fetch operations.
103108
- By default, it retries up to 3 times with a 2-second delay between attempts.
104109
- Logs detailed error messages for each failed attempt and skips the user if all retries fail.
110+
111+
### Self-Update Feature
112+
- The `--self-update` option fetches the latest version of the script from the GitHub repository.
113+
- Replaces the current script with the downloaded version.
114+
- Ensures the script is always up-to-date with the latest features and fixes.

.github/workflows/release.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ jobs:
4747
with:
4848
tag_name: v${{ steps.get_version.outputs.version }}
4949
generate_release_notes: true
50-
files: ssh-key-sync.zip
50+
files: |
51+
ssh-key-sync.zip
52+
sync-ssh-keys.sh

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This Bash script pulls `authorized_keys` files from remote URLs and updates SSH
1313
- Safe: Only updates keys if they’ve changed
1414
- Logs activity per user
1515
- Retries failed fetch operations up to 3 times with a delay
16+
- **Self-update**: Automatically updates the script to the latest version from the GitHub repository
1617

1718
## ⚙️ Configuration
1819

@@ -55,6 +56,10 @@ declare -A USER_KEYS=(
5556
```cron
5657
*/15 * * * * /usr/local/bin/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1
5758
```
59+
5. To update the script to the latest version, run:
60+
```bash
61+
./sync-ssh-keys.sh --self-update
62+
```
5863

5964
## Implementation Notes
6065

@@ -63,6 +68,7 @@ declare -A USER_KEYS=(
6368
- Includes a retry mechanism for failed fetch operations (3 attempts with a 2-second delay).
6469
- Only updates a user's `authorized_keys` if the remote file has changed.
6570
- Logs all actions with timestamps.
71+
- The `--self-update` option fetches the latest version of the script from the GitHub repository and replaces the current version.
6672

6773
## Examples
6874

sync-ssh-keys.sh

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -euo pipefail
33

44
# shellcheck disable=SC2034 # planned to be used in a future release
5-
SCRIPT_VERSION="0.0.8"
5+
SCRIPT_VERSION="0.0.9"
66

77
# === Load user configuration ===
88
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -56,6 +56,43 @@ fetch_key_file() {
5656
return 1
5757
}
5858

59+
self_update() {
60+
local REPO="locus313/ssh-key-sync"
61+
local LATEST_URL
62+
local TMP_DIR
63+
64+
log_message "Checking for the latest version of the script..."
65+
66+
LATEST_URL=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" | \
67+
grep "browser_download_url" | grep "sync-ssh-keys.sh" | cut -d '"' -f 4)
68+
69+
if [ -z "$LATEST_URL" ]; then
70+
log_message "Error: Could not determine the latest version URL."
71+
exit 1
72+
fi
73+
74+
TMP_DIR=$(mktemp -d)
75+
curl -fsSL "$LATEST_URL" -o "$TMP_DIR/sync-ssh-keys.sh"
76+
77+
if [ ! -s "$TMP_DIR/sync-ssh-keys.sh" ]; then
78+
log_message "Error: Downloaded script is empty. Aborting update."
79+
rm -rf "$TMP_DIR"
80+
exit 1
81+
fi
82+
83+
chmod +x "$TMP_DIR/sync-ssh-keys.sh"
84+
mv "$TMP_DIR/sync-ssh-keys.sh" "$SCRIPT_DIR/sync-ssh-keys.sh"
85+
rm -rf "$TMP_DIR"
86+
87+
log_message "Script successfully updated to the latest version."
88+
exit 0
89+
}
90+
91+
# --- Option parsing ---
92+
if [[ "${1:-}" == "--self-update" ]]; then
93+
self_update
94+
fi
95+
5996
TMP_FILES=()
6097
trap 'rm -f "${TMP_FILES[@]}"' EXIT
6198
for USER in "${!USER_KEYS[@]}"; do

0 commit comments

Comments
 (0)