Skip to content

Commit fb62d37

Browse files
authored
feat: Automatic update check with opt out (#9)
1 parent 5ea86b0 commit fb62d37

3 files changed

Lines changed: 83 additions & 10 deletions

File tree

user/local/share/towel/bin/towel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ if [ ! -d "$TOWEL_BIN_DIR" ]; then
1414
exit 1
1515
fi
1616

17+
# shellcheck source=user/local/share/towel/bin/towel-common
18+
source "$TOWEL_BIN_DIR/towel-common"
19+
1720
function print_help() {
1821
"$TOWEL_BIN_DIR/towel-help"
1922
}
@@ -39,6 +42,8 @@ function unknown_option() {
3942
exit 1
4043
}
4144

45+
towel_auto_update_check
46+
4247
if [ $# -eq 0 ]; then
4348
exec "$TOWEL_BIN_DIR/towel-enter"
4449
fi

user/local/share/towel/bin/towel-common

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,52 @@ function towel_remove_exported_apps() {
285285
[ -d "$export_apps_dir" ] && rm -rf "$export_apps_dir"
286286
}
287287

288+
function towel_auto_update_check() {
289+
[ -n "${CONTAINER_ID:-}" ] && return
290+
[ "${TOWEL_NO_UPDATE_CHECK:-}" = "1" ] && return
291+
[ -f "$TOWEL_DATA/no-update-check" ] && return
292+
293+
local today
294+
today="$(date +%Y-%m-%d)"
295+
local check_state="$TOWEL_DATA/update_check"
296+
local notified_file="$TOWEL_DATA/update_notified"
297+
local stored_date="" stored_status="" stored_latest=""
298+
299+
local stored_line
300+
if stored_line="$(cat "$check_state" 2>/dev/null)"; then
301+
read -r stored_date stored_status stored_latest <<<"$stored_line"
302+
fi
303+
304+
if [ "$stored_date" = "$today" ] && [ "$stored_status" = "lt" ] && [ -n "$stored_latest" ]; then
305+
local notified_date
306+
notified_date="$(cat "$notified_file" 2>/dev/null || true)"
307+
if [ "$notified_date" != "$today" ]; then
308+
echo 1>&2 "towel update available: $TOWEL_VERSION -> $stored_latest"
309+
echo 1>&2 " Run 'towel update --apply' to update, or 'towel update --no-auto-check' to disable these notifications."
310+
printf '%s\n' "$today" >"$notified_file"
311+
fi
312+
fi
313+
314+
if [ "${stored_date:-}" != "$today" ]; then
315+
(
316+
out="$("$TOWEL_BIN_DIR/towel-update" --check 2>/dev/null)"
317+
rc=$?
318+
ts="$(date +%Y-%m-%d)"
319+
case $rc in
320+
0) printf '%s eq\n' "$ts" >"$check_state" ;;
321+
10)
322+
ver="$(printf '%s\n' "$out" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+$' | tail -1)"
323+
if [ -n "$ver" ]; then
324+
printf '%s lt %s\n' "$ts" "$ver" >"$check_state"
325+
else
326+
printf '%s error\n' "$ts" >"$check_state"
327+
fi
328+
;;
329+
*) printf '%s error\n' "$ts" >"$check_state" ;;
330+
esac
331+
) >/dev/null 2>&1 &
332+
disown 2>/dev/null || true
333+
fi
334+
}
335+
288336
_towel_set_opts

user/local/share/towel/bin/towel-update

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@ TOWEL_GITHUB_REPO="${TOWEL_GITHUB_REPO:-codam-coding-college/towel}"
1414

1515
function print_usage() {
1616
echo 1>&2 "Usage:
17-
towel update --check [--quiet]
18-
towel update --apply [-y|--yes]
17+
towel update --check [--quiet]
18+
towel update --apply [-y|--yes]
19+
towel update --no-auto-check
20+
towel update --auto-check
1921
towel update [-h|--help]
2022
2123
Flags:
22-
--check Query GitHub for the latest release and compare to the installed version.
23-
Exits 0 if up to date, 10 if an update is available, 1 on error.
24-
--apply Download, verify, and install the latest release.
25-
-y, --yes Skip the confirmation prompt (--apply only).
26-
--quiet Suppress stdout output (--check only; stderr and exit code preserved).
27-
-h, --help Print this help text.
24+
--check Query GitHub for the latest release and compare to the installed version.
25+
Exits 0 if up to date, 10 if an update is available, 1 on error.
26+
--apply Download, verify, and install the latest release.
27+
-y, --yes Skip the confirmation prompt (--apply only).
28+
--quiet Suppress stdout output (--check only; stderr and exit code preserved).
29+
--no-auto-check Disable the automatic daily update check and notifications.
30+
--auto-check Re-enable the automatic daily update check.
31+
-h, --help Print this help text.
2832
2933
Environment:
30-
TOWEL_GITHUB_REPO Override the GitHub owner/repo (default: codam-coding-college/towel)."
34+
TOWEL_GITHUB_REPO Override the GitHub owner/repo (default: codam-coding-college/towel)
35+
TOWEL_NO_UPDATE_CHECK Set to 1 to disable automatic update checks."
3136
}
3237

3338
function _towel_update_compare() {
@@ -67,6 +72,8 @@ for arg in "$@"; do
6772
case "$arg" in
6873
--check) mode="check" ;;
6974
--apply) mode="apply" ;;
75+
--no-auto-check) mode="no-auto-check" ;;
76+
--auto-check) mode="auto-check" ;;
7077
-y | --yes) assume_yes="true" ;;
7178
--quiet) quiet="true" ;;
7279
-h | --help)
@@ -82,11 +89,24 @@ for arg in "$@"; do
8289
done
8390

8491
if [ -z "$mode" ]; then
85-
echo 1>&2 "towel update: a mode flag is required (--check or --apply)"
92+
echo 1>&2 "towel update: a mode flag is required (--check, --apply, --no-auto-check, or --auto-check)"
8693
print_usage
8794
exit 64
8895
fi
8996

97+
if [ "$mode" = "no-auto-check" ]; then
98+
touch "$TOWEL_DATA/no-update-check"
99+
echo "Automatic update checks disabled."
100+
echo " Re-enable with: towel update --auto-check"
101+
exit 0
102+
fi
103+
104+
if [ "$mode" = "auto-check" ]; then
105+
rm -f "$TOWEL_DATA/no-update-check" "$TOWEL_DATA/update_check" "$TOWEL_DATA/update_notified"
106+
echo "Automatic update checks enabled."
107+
exit 0
108+
fi
109+
90110
latest_version="$(_towel_fetch_latest_version)" || {
91111
echo 1>&2 "towel update --${mode} failed: could not reach GitHub API (check network connectivity)"
92112
exit 1

0 commit comments

Comments
 (0)