File tree Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ This document provides comprehensive guidance for AI coding agents and contribut
14
14
- Logging and error handling.
15
15
- Configuration loading from ` users.conf ` .
16
16
- 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.
17
18
18
19
### Configuration
19
20
- ** ` 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
38
39
``` bash
39
40
./sync-ssh-keys.sh
40
41
```
42
+ 3 . To update the script to the latest version, run:
43
+ ``` bash
44
+ ./sync-ssh-keys.sh --self-update
45
+ ```
41
46
42
47
### Configuration
43
48
- 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
102
107
- The ` fetch_key_file ` function includes a retry mechanism for failed fetch operations.
103
108
- By default, it retries up to 3 times with a 2-second delay between attempts.
104
109
- 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.
Original file line number Diff line number Diff line change 47
47
with :
48
48
tag_name : v${{ steps.get_version.outputs.version }}
49
49
generate_release_notes : true
50
- files : ssh-key-sync.zip
50
+ files : |
51
+ ssh-key-sync.zip
52
+ sync-ssh-keys.sh
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ This Bash script pulls `authorized_keys` files from remote URLs and updates SSH
13
13
- Safe: Only updates keys if they’ve changed
14
14
- Logs activity per user
15
15
- 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
16
17
17
18
## ⚙️ Configuration
18
19
@@ -55,6 +56,10 @@ declare -A USER_KEYS=(
55
56
``` cron
56
57
*/15 * * * * /usr/local/bin/sync-ssh-keys.sh >> /var/log/ssh-key-sync.log 2>&1
57
58
```
59
+ 5 . To update the script to the latest version, run:
60
+ ``` bash
61
+ ./sync-ssh-keys.sh --self-update
62
+ ```
58
63
59
64
## Implementation Notes
60
65
@@ -63,6 +68,7 @@ declare -A USER_KEYS=(
63
68
- Includes a retry mechanism for failed fetch operations (3 attempts with a 2-second delay).
64
69
- Only updates a user's ` authorized_keys ` if the remote file has changed.
65
70
- 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.
66
72
67
73
## Examples
68
74
Original file line number Diff line number Diff line change 2
2
set -euo pipefail
3
3
4
4
# shellcheck disable=SC2034 # planned to be used in a future release
5
- SCRIPT_VERSION=" 0.0.8 "
5
+ SCRIPT_VERSION=" 0.0.9 "
6
6
7
7
# === Load user configuration ===
8
8
SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
@@ -56,6 +56,43 @@ fetch_key_file() {
56
56
return 1
57
57
}
58
58
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
+
59
96
TMP_FILES=()
60
97
trap ' rm -f "${TMP_FILES[@]}"' EXIT
61
98
for USER in " ${! USER_KEYS[@]} " ; do
You can’t perform that action at this time.
0 commit comments