Skip to content

Commit 5c7340d

Browse files
committed
Make release scripts fully standalone
remove self and auto update code from main script only inject when install.sh is used show update help only if possible GUPLOAD_INSTALLED_WITH variable is checked, it should be equal to script this will also help in integrating to package managers
1 parent f040e2e commit 5c7340d

File tree

5 files changed

+115
-285
lines changed

5 files changed

+115
-285
lines changed

bash/release/gupload

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -964,10 +964,10 @@ Options:\n
964964
--hide - This flag will prevent the script to print sensitive information like root folder id or drivelink.\n
965965
-v | --verbose - Display detailed message (only for non-parallel uploads).\n
966966
-V | --verbose-progress - Display detailed message and detailed upload progress(only for non-parallel uploads).\n
967-
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.\n
968-
-u | --update - Update the installed script in your system.\n
967+
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.
968+
$([[ ${GUPLOAD_INSTALLED_WITH} = script ]] && printf '%s\n' '\n -u | --update - Update the installed script in your system.\n
969+
-U | --uninstall - Uninstall script, remove related files.\n')
969970
--info - Show detailed info, only if script is installed system wide.\n
970-
-U | --uninstall - Uninstall script, remove related files.\n
971971
-D | --debug - Display script command trace.\n
972972
-h | --help - Display this message.\n"
973973
exit 0
@@ -978,71 +978,6 @@ _short_help() {
978978
exit 0
979979
}
980980

981-
###################################################
982-
# Automatic updater, only update if script is installed system wide.
983-
# Globals: 5 variables, 2 functions
984-
# COMMAND_NAME, REPO, INSTALL_PATH, TYPE, TYPE_VALUE | _update, _update_value
985-
# Arguments: None
986-
# Result: On
987-
# Update if AUTO_UPDATE_INTERVAL + LAST_UPDATE_TIME less than printf "%(%s)T\\n" "-1"
988-
###################################################
989-
_auto_update() {
990-
export REPO
991-
command -v "${COMMAND_NAME}" 1> /dev/null &&
992-
if [[ -n "${REPO:+${COMMAND_NAME:+${INSTALL_PATH:+${TYPE:+${TYPE_VALUE}}}}}" ]]; then
993-
current_time="$(printf "%(%s)T\\n" "-1")"
994-
[[ $((LAST_UPDATE_TIME + AUTO_UPDATE_INTERVAL)) -lt ${current_time} ]] && _update
995-
_update_value LAST_UPDATE_TIME "${current_time}"
996-
fi
997-
return 0
998-
}
999-
1000-
###################################################
1001-
# Install/Update/uninstall the script.
1002-
# Globals: 4 variables
1003-
# Varibles - HOME, REPO, TYPE_VALUE, GLOBAL_INSTALL
1004-
# Arguments: 1
1005-
# ${1} = uninstall or update
1006-
# Result: On
1007-
# ${1} = nothing - Update the script if installed, otherwise install.
1008-
# ${1} = uninstall - uninstall the script
1009-
###################################################
1010-
_update() {
1011-
declare job="${1:-update}"
1012-
[[ ${GLOBAL_INSTALL} = true ]] && ! [[ $(id -u) = 0 ]] && printf "%s\n" "Error: Need root access to update." && return 0
1013-
[[ ${job} =~ uninstall ]] && job_uninstall="--uninstall"
1014-
_print_center "justify" "Fetching ${job} script.." "-"
1015-
declare repo="${REPO:-labbots/google-drive-upload}" type_value="${TYPE_VALUE:-latest}" cmd="${COMMAND_NAME:-gupload}" path="${INSTALL_PATH:-${HOME}/.google-drive-upload/bin}"
1016-
{ [[ ${TYPE:-} != branch ]] && type_value="$(_get_latest_sha release "${type_value}" "${repo}")"; } || :
1017-
if script="$(curl --compressed -Ls "https://github.com/${repo}/raw/${type_value}/install.sh")"; then
1018-
_clear_line 1
1019-
printf "%s\n" "${script}" | bash -s -- ${job_uninstall:-} --skip-internet-check --cmd "${cmd}" --path "${path}"
1020-
current_time="$(printf "%(%s)T\\n" "-1")"
1021-
[[ -z ${job_uninstall} ]] && _update_value LAST_UPDATE_TIME "${current_time}" &
1022-
else
1023-
_clear_line 1
1024-
"${QUIET:-_print_center}" "justify" "Error: Cannot download ${job} script." "=" 1>&2
1025-
return 1
1026-
fi
1027-
return 0
1028-
}
1029-
1030-
###################################################
1031-
# Update in-script values
1032-
###################################################
1033-
_update_value() {
1034-
declare command_path="${INSTALL_PATH:?}/${COMMAND_NAME}" \
1035-
value_name="${1:-}" value="${2:-}" script_without_value_and_shebang
1036-
script_without_value_and_shebang="$(grep -v "${value_name}=\".*\".* # added values" "${command_path}" | sed 1d)"
1037-
new_script="$(
1038-
sed -n 1p "${command_path}"
1039-
printf "%s\n" "${value_name}=\"${value}\" # added values"
1040-
printf "%s\n" "${script_without_value_and_shebang}"
1041-
)"
1042-
chmod +w "${command_path}" && printf "%s\n" "${new_script}" >| "${command_path}" && chmod -w "${command_path}"
1043-
return 0
1044-
}
1045-
1046981
###################################################
1047982
# Print info if installed
1048983
# Globals: 7 variable
@@ -1142,8 +1077,6 @@ _setup_arguments() {
11421077
case "${1}" in
11431078
-h | --help) _usage ;;
11441079
-D | --debug) DEBUG="true" && export DEBUG ;;
1145-
-u | --update) _check_debug && _update && exit "${?}" ;;
1146-
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
11471080
--info) _version_info ;;
11481081
-c | -C | --create-dir)
11491082
_check_longoptions "${1}" "${2}"
@@ -1229,6 +1162,12 @@ _setup_arguments() {
12291162
'') shorthelp ;;
12301163
*) # Check if user meant it to be a flag
12311164
if [[ ${1} = -* ]]; then
1165+
[[ ${GUPLOAD_INSTALLED_WITH} = script ]] && {
1166+
case "${1}" in
1167+
-u | --update) _check_debug && _update && exit "${?}" ;;
1168+
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
1169+
esac
1170+
}
12321171
printf '%s: %s: Unknown option\nTry '"%s -h/--help"' for more information.\n' "${0##*/}" "${1}" "${0##*/}" && exit 1
12331172
else
12341173
if [[ ${1} =~ (drive.google.com|docs.google.com) ]]; then
@@ -1700,7 +1639,7 @@ main() {
17001639
printf "\n\n%s\n" "Script exited manually."
17011640
kill -- -$$ &
17021641
else
1703-
{ _cleanup_config "${CONFIG}" && _auto_update; } 1>| /dev/null &
1642+
{ _cleanup_config "${CONFIG}" && [[ ${GUPLOAD_INSTALLED_WITH} = script ]] && _auto_update; } 1>| /dev/null &
17041643
fi
17051644
} 2>| /dev/null || :
17061645
return 0

bash/upload.bash

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ Options:\n
3131
--hide - This flag will prevent the script to print sensitive information like root folder id or drivelink.\n
3232
-v | --verbose - Display detailed message (only for non-parallel uploads).\n
3333
-V | --verbose-progress - Display detailed message and detailed upload progress(only for non-parallel uploads).\n
34-
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.\n
35-
-u | --update - Update the installed script in your system.\n
34+
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.
35+
$([[ ${GUPLOAD_INSTALLED_WITH} = script ]] && printf '%s\n' '\n -u | --update - Update the installed script in your system.\n
36+
-U | --uninstall - Uninstall script, remove related files.\n')
3637
--info - Show detailed info, only if script is installed system wide.\n
37-
-U | --uninstall - Uninstall script, remove related files.\n
3838
-D | --debug - Display script command trace.\n
3939
-h | --help - Display this message.\n"
4040
exit 0
@@ -45,71 +45,6 @@ _short_help() {
4545
exit 0
4646
}
4747

48-
###################################################
49-
# Automatic updater, only update if script is installed system wide.
50-
# Globals: 5 variables, 2 functions
51-
# COMMAND_NAME, REPO, INSTALL_PATH, TYPE, TYPE_VALUE | _update, _update_value
52-
# Arguments: None
53-
# Result: On
54-
# Update if AUTO_UPDATE_INTERVAL + LAST_UPDATE_TIME less than printf "%(%s)T\\n" "-1"
55-
###################################################
56-
_auto_update() {
57-
export REPO
58-
command -v "${COMMAND_NAME}" 1> /dev/null &&
59-
if [[ -n "${REPO:+${COMMAND_NAME:+${INSTALL_PATH:+${TYPE:+${TYPE_VALUE}}}}}" ]]; then
60-
current_time="$(printf "%(%s)T\\n" "-1")"
61-
[[ $((LAST_UPDATE_TIME + AUTO_UPDATE_INTERVAL)) -lt ${current_time} ]] && _update
62-
_update_value LAST_UPDATE_TIME "${current_time}"
63-
fi
64-
return 0
65-
}
66-
67-
###################################################
68-
# Install/Update/uninstall the script.
69-
# Globals: 4 variables
70-
# Varibles - HOME, REPO, TYPE_VALUE, GLOBAL_INSTALL
71-
# Arguments: 1
72-
# ${1} = uninstall or update
73-
# Result: On
74-
# ${1} = nothing - Update the script if installed, otherwise install.
75-
# ${1} = uninstall - uninstall the script
76-
###################################################
77-
_update() {
78-
declare job="${1:-update}"
79-
[[ ${GLOBAL_INSTALL} = true ]] && ! [[ $(id -u) = 0 ]] && printf "%s\n" "Error: Need root access to update." && return 0
80-
[[ ${job} =~ uninstall ]] && job_uninstall="--uninstall"
81-
_print_center "justify" "Fetching ${job} script.." "-"
82-
declare repo="${REPO:-labbots/google-drive-upload}" type_value="${TYPE_VALUE:-latest}" cmd="${COMMAND_NAME:-gupload}" path="${INSTALL_PATH:-${HOME}/.google-drive-upload/bin}"
83-
{ [[ ${TYPE:-} != branch ]] && type_value="$(_get_latest_sha release "${type_value}" "${repo}")"; } || :
84-
if script="$(curl --compressed -Ls "https://github.com/${repo}/raw/${type_value}/install.sh")"; then
85-
_clear_line 1
86-
printf "%s\n" "${script}" | bash -s -- ${job_uninstall:-} --skip-internet-check --cmd "${cmd}" --path "${path}"
87-
current_time="$(printf "%(%s)T\\n" "-1")"
88-
[[ -z ${job_uninstall} ]] && _update_value LAST_UPDATE_TIME "${current_time}" &
89-
else
90-
_clear_line 1
91-
"${QUIET:-_print_center}" "justify" "Error: Cannot download ${job} script." "=" 1>&2
92-
return 1
93-
fi
94-
return 0
95-
}
96-
97-
###################################################
98-
# Update in-script values
99-
###################################################
100-
_update_value() {
101-
declare command_path="${INSTALL_PATH:?}/${COMMAND_NAME}" \
102-
value_name="${1:-}" value="${2:-}" script_without_value_and_shebang
103-
script_without_value_and_shebang="$(grep -v "${value_name}=\".*\".* # added values" "${command_path}" | sed 1d)"
104-
new_script="$(
105-
sed -n 1p "${command_path}"
106-
printf "%s\n" "${value_name}=\"${value}\" # added values"
107-
printf "%s\n" "${script_without_value_and_shebang}"
108-
)"
109-
chmod +w "${command_path}" && printf "%s\n" "${new_script}" >| "${command_path}" && chmod -w "${command_path}"
110-
return 0
111-
}
112-
11348
###################################################
11449
# Print info if installed
11550
# Globals: 7 variable
@@ -209,8 +144,6 @@ _setup_arguments() {
209144
case "${1}" in
210145
-h | --help) _usage ;;
211146
-D | --debug) DEBUG="true" && export DEBUG ;;
212-
-u | --update) _check_debug && _update && exit "${?}" ;;
213-
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
214147
--info) _version_info ;;
215148
-c | -C | --create-dir)
216149
_check_longoptions "${1}" "${2}"
@@ -296,6 +229,12 @@ _setup_arguments() {
296229
'') shorthelp ;;
297230
*) # Check if user meant it to be a flag
298231
if [[ ${1} = -* ]]; then
232+
[[ ${GUPLOAD_INSTALLED_WITH} = script ]] && {
233+
case "${1}" in
234+
-u | --update) _check_debug && _update && exit "${?}" ;;
235+
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
236+
esac
237+
}
299238
printf '%s: %s: Unknown option\nTry '"%s -h/--help"' for more information.\n' "${0##*/}" "${1}" "${0##*/}" && exit 1
300239
else
301240
if [[ ${1} =~ (drive.google.com|docs.google.com) ]]; then
@@ -767,7 +706,7 @@ main() {
767706
printf "\n\n%s\n" "Script exited manually."
768707
kill -- -$$ &
769708
else
770-
{ _cleanup_config "${CONFIG}" && _auto_update; } 1>| /dev/null &
709+
{ _cleanup_config "${CONFIG}" && [[ ${GUPLOAD_INSTALLED_WITH} = script ]] && _auto_update; } 1>| /dev/null &
771710
fi
772711
} 2>| /dev/null || :
773712
return 0

install.sh

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,90 @@ _variables() {
361361
date +'%s'
362362
fi)" && export LAST_UPDATE_TIME
363363
GLOBAL_INSTALL="false"
364+
export GUPLOAD_INSTALLED_WITH="script"
364365

365366
[ -n "${SKIP_SYNC}" ] && SYNC_COMMAND_NAME=""
366-
export VALUES_LIST="REPO COMMAND_NAME ${SYNC_COMMAND_NAME:+SYNC_COMMAND_NAME} INSTALL_PATH TYPE TYPE_VALUE SHELL_RC LAST_UPDATE_TIME AUTO_UPDATE_INTERVAL INSTALLATION GUPLOAD_SCRIPT_SHA GSYNC_SCRIPT_SHA GLOBAL_INSTALL"
367+
export VALUES_LIST="REPO COMMAND_NAME ${SYNC_COMMAND_NAME:+SYNC_COMMAND_NAME} INSTALL_PATH TYPE TYPE_VALUE SHELL_RC LAST_UPDATE_TIME AUTO_UPDATE_INTERVAL INSTALLATION GUPLOAD_SCRIPT_SHA GSYNC_SCRIPT_SHA GLOBAL_INSTALL GUPLOAD_INSTALLED_WITH"
367368

368-
VALUES_REGEX="" && for i in VALUES_LIST REPO COMMAND_NAME ${SYNC_COMMAND_NAME:+SYNC_COMMAND_NAME} INSTALL_PATH TYPE TYPE_VALUE SHELL_RC LAST_UPDATE_TIME AUTO_UPDATE_INTERVAL INSTALLATION GUPLOAD_SCRIPT_SHA GSYNC_SCRIPT_SHA GLOBAL_INSTALL; do
369+
VALUES_REGEX="" && for i in VALUES_LIST REPO COMMAND_NAME ${SYNC_COMMAND_NAME:+SYNC_COMMAND_NAME} INSTALL_PATH TYPE TYPE_VALUE SHELL_RC LAST_UPDATE_TIME AUTO_UPDATE_INTERVAL INSTALLATION GUPLOAD_SCRIPT_SHA GSYNC_SCRIPT_SHA GLOBAL_INSTALL GUPLOAD_INSTALLED_WITH; do
369370
VALUES_REGEX="${VALUES_REGEX:+${VALUES_REGEX}|}^${i}=\".*\".* # added values"
370371
done
371372

372373
return 0
373374
}
374375

376+
###################################################
377+
# For self and automatic updates
378+
###################################################
379+
_print_self_update_code() {
380+
cat << 'EOF'
381+
###################################################
382+
# Automatic updater, only update if script is installed system wide.
383+
# Globals: 5 variables, 2 functions
384+
# COMMAND_NAME, REPO, INSTALL_PATH, TYPE, TYPE_VALUE | _update, _update_value
385+
# Arguments: None
386+
# Result: On
387+
# Update if AUTO_UPDATE_INTERVAL + LAST_UPDATE_TIME less than printf "%(%s)T\\n" "-1"
388+
###################################################
389+
_auto_update() {
390+
export REPO
391+
command -v "${COMMAND_NAME}" 1> /dev/null &&
392+
if [ -n "${REPO:+${COMMAND_NAME:+${INSTALL_PATH:+${TYPE:+${TYPE_VALUE}}}}}" ]; then
393+
current_time="$(date +'%s')"
394+
[ "$((LAST_UPDATE_TIME + AUTO_UPDATE_INTERVAL))" -lt "$(date +'%s')" ] && _update
395+
_update_value LAST_UPDATE_TIME "${current_time}"
396+
fi
397+
return 0
398+
}
399+
400+
###################################################
401+
# Install/Update/uninstall the script.
402+
# Globals: 4 variables
403+
# Varibles - HOME, REPO, TYPE_VALUE, GLOBAL_INSTALL
404+
# Arguments: 1
405+
# ${1} = uninstall or update
406+
# Result: On
407+
# ${1} = nothing - Update the script if installed, otherwise install.
408+
# ${1} = uninstall - uninstall the script
409+
###################################################
410+
_update() {
411+
job_update="${1:-update}"
412+
[ "${GLOBAL_INSTALL}" = true ] && ! [ "$(id -u)" = 0 ] && printf "%s\n" "Error: Need root access to update." && return 0
413+
[ "${job_update}" = uninstall ] && job_uninstall="--uninstall"
414+
_print_center "justify" "Fetching ${job_update} script.." "-"
415+
repo_update="${REPO:-labbots/google-drive-upload}" type_value_update="${TYPE_VALUE:-latest}" cmd_update="${COMMAND_NAME:-gupload}" path_update="${INSTALL_PATH:-${HOME}/.google-drive-upload/bin}"
416+
{ [ "${TYPE:-}" != branch ] && type_value_update="$(_get_latest_sha release "${type_value_update}" "${repo_update}")"; } || :
417+
if script_update="$(curl --compressed -Ls "https://github.com/${repo_update}/raw/${type_value_update}/install.sh")"; then
418+
_clear_line 1
419+
printf "%s\n" "${script_update}" | sh -s -- ${job_uninstall:-} --skip-internet-check --cmd "${cmd_update}" --path "${path_update}"
420+
current_time="$(date +'%s')"
421+
[ -z "${job_uninstall}" ] && _update_value LAST_UPDATE_TIME "${current_time}"
422+
else
423+
_clear_line 1
424+
"${QUIET:-_print_center}" "justify" "Error: Cannot download" " ${job_update} script." "=" 1>&2
425+
return 1
426+
fi
427+
return 0
428+
}
429+
430+
###################################################
431+
# Update in-script values
432+
###################################################
433+
_update_value() {
434+
command_path="${INSTALL_PATH:?}/${COMMAND_NAME}"
435+
value_name="${1:-}" value="${2:-}"
436+
script_without_value_and_shebang="$(grep -v "${value_name}=\".*\".* # added values" "${command_path}" | sed 1d)"
437+
new_script="$(
438+
sed -n 1p "${command_path}"
439+
printf "%s\n" "${value_name}=\"${value}\" # added values"
440+
printf "%s\n" "${script_without_value_and_shebang}"
441+
)"
442+
chmod +w "${command_path}" && printf "%s\n" "${new_script}" >| "${command_path}" && chmod -w "${command_path}"
443+
return 0
444+
}
445+
EOF
446+
}
447+
375448
###################################################
376449
# Download scripts
377450
###################################################
@@ -417,10 +490,11 @@ _inject_values() {
417490
chmod +w "${INSTALL_PATH}/${COMMAND_NAME}"
418491
{
419492
printf "%s\n" "${shebang}"
420-
for i in VALUES_LIST REPO COMMAND_NAME ${SYNC_COMMAND_NAME:+SYNC_COMMAND_NAME} INSTALL_PATH TYPE TYPE_VALUE SHELL_RC LAST_UPDATE_TIME AUTO_UPDATE_INTERVAL INSTALLATION GUPLOAD_SCRIPT_SHA GSYNC_SCRIPT_SHA GLOBAL_INSTALL; do
493+
for i in VALUES_LIST REPO COMMAND_NAME ${SYNC_COMMAND_NAME:+SYNC_COMMAND_NAME} INSTALL_PATH TYPE TYPE_VALUE SHELL_RC LAST_UPDATE_TIME AUTO_UPDATE_INTERVAL INSTALLATION GUPLOAD_SCRIPT_SHA GSYNC_SCRIPT_SHA GLOBAL_INSTALL GUPLOAD_INSTALLED_WITH; do
421494
printf "%s\n" "${i}=\"$(eval printf "%s" \"\$"${i}"\")\" # added values"
422495
done
423496
printf "%s\n" "LATEST_INSTALLED_SHA=\"${LATEST_CURRENT_SHA}\" # added values"
497+
_print_self_update_code # inject the self and auto update functions
424498
printf "%s\n" "${script_without_values_and_shebang}"
425499
} 1>| "${INSTALL_PATH}/${COMMAND_NAME}"
426500

0 commit comments

Comments
 (0)