db19589fcf03690a443f1a206361963b47809b93

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(updater): simplify check for available updates

Diff

 1diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh
 2index 8264762b6c5e3301f53a7e6245db710a2b85f976..70cd21f8413ebb08571660500a6a19d607147378 100644
 3--- a/tools/check_for_upgrade.sh
 4+++ b/tools/check_for_upgrade.sh
 5@@ -58,23 +58,20 @@ function is_update_available() {
 6   local local_head
 7   local_head=$(git -C "$ZSH" rev-parse $branch 2>/dev/null) || return 0
 8 
 9-  # Get remote HEAD. If we can't get it assume there are updates unless there is no connection:
10-  # - curl: 6 (could not resolve) or 7 (could not connect)
11-  # - wget: 4 (network unreachable)
12-  # - fetch: 1 (no route to host)
13-  local remote_head ret
14+  # Get remote HEAD. If no suitable command is found assume there are updates
15+  # On any other error, skip the update (connection may be down)
16+  local remote_head
17   remote_head=$(
18-    curl -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null || {
19-      [[ $? -eq 6 || $? -eq 7 ]] && exit 1
20-    } || wget -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null || {
21-      [[ $? -eq 4 ]] && exit 1
22-    } || HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -o - $api_url 2>/dev/null || {
23-      [[ $? -eq 1 ]] && exit 1
24-    } || exit 0
25-  )
26-
27-  # If can't fetch remote HEAD, return exit code
28-  ret=$?; [[ -n "$remote_head" ]] || return $ret
29+    if (( ${+commands[curl]} )); then
30+      curl -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
31+    elif (( ${+commands[wget]} )); then
32+      wget -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null
33+    elif (( ${+commands[fetch]} )); then
34+      HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -o - $api_url 2>/dev/null
35+    else
36+      exit 0
37+    fi
38+  ) || return 1
39 
40   # Compare local and remote HEADs
41   [[ "$local_head" != "$remote_head" ]]