This repository has been archived by the owner on Apr 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathauto-update.patch
77 lines (75 loc) · 2.79 KB
/
auto-update.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
diff --git a/imgur-screenshot b/imgur-screenshot
index 4a9c8b9..a1347b6 100755
--- a/imgur-screenshot
+++ b/imgur-screenshot
@@ -82,6 +82,7 @@ load_default_config() {
declare -g EDIT="false"
declare -g AUTO_DELETE
declare -g KEEP_FILE="true"
+ declare -g AUTO_UPDATE="false"
# NOTICE: if you make changes here, also edit the docs at
# https://github.com/jomo/imgur-screenshot/wiki/Config
@@ -119,7 +120,7 @@ parse_args() {
echo " -k, --keep-file <true|false> Override 'KEEP_FILE' config"
echo " -d, --auto-delete <s> Automatically delete image after <s> seconds"
echo ""
- echo " -u, --update Check for updates, exit"
+ echo " -u, --update Check for a newer version, install if available, exit"
echo ""
echo " file Upload file instead of taking a screenshot"
exit 0;;
@@ -165,6 +166,7 @@ parse_args() {
AUTO_DELETE="${2}"
shift 2;;
-u | --update)
+ AUTO_UPDATE="true"
check_for_update
exit 0;;
*)
@@ -287,6 +289,13 @@ check_for_update() {
echo "Version ${remote_version} is available (You have ${CURRENT_VERSION})"
notify ok "Update found" "Version ${remote_version} is available (You have ${CURRENT_VERSION}). https://github.com/jomo/imgur-screenshot"
echo "Check https://github.com/jomo/imgur-screenshot/releases/${remote_version} for more info."
+
+ if [ "${AUTO_UPDATE}" == "true" ]; then
+ run_update "${remote_version}"
+ else
+ echo "Use --update to install the new version"
+ fi
+
elif [ -z "${CURRENT_VERSION}" ] || [ -z "${remote_version}" ]; then
echo "Invalid empty version string"
echo "Current (local) version: '${CURRENT_VERSION}'"
@@ -299,6 +308,33 @@ check_for_update() {
fi
}
+run_update() {
+ local remote_version remote_script self script_path tmp_script bak_script
+
+ set -o errexit
+
+ remote_version="${1}"
+ remote_script="https://github.com/jomo/imgur-screenshot/releases/download/${remote_version}/imgur-screenshot"
+ tmp_script="$(mktemp)"
+ bak_script="$(mktemp)"
+
+ # determine this script's location
+ self="$(command -v "${0}")"
+ script_path="$(readlink "${self}" || echo "${self}")"
+
+ # Download latest version
+ echo "Downloading and installing latest version..."
+ curl --compressed -fsSL --stderr - -'#' "${remote_script}" > "${tmp_script}"
+ if [ ! "${?}" -eq "0" ]; then
+ echo "Update failed:" >&2
+ cat "${tmp_script}" >&2
+ exit 1
+ fi
+ mv -v "${script_path}" "${bak_script}.bak"
+ mv -v "${tmp_script}" "${script_path}"
+ echo "Successfully updated to ${remote_version}."
+}
+
check_oauth2_client_secrets() {
if [ -z "${CLIENT_ID}" ] || [ -z "${CLIENT_SECRET}" ]; then
echo "Your CLIENT_ID and CLIENT_SECRET are not set."