Skip to content

Commit

Permalink
Make release scripts fully standalone
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Akianonymus committed Nov 7, 2020
1 parent f040e2e commit 5c7340d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 285 deletions.
81 changes: 10 additions & 71 deletions bash/release/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -964,10 +964,10 @@ Options:\n
--hide - This flag will prevent the script to print sensitive information like root folder id or drivelink.\n
-v | --verbose - Display detailed message (only for non-parallel uploads).\n
-V | --verbose-progress - Display detailed message and detailed upload progress(only for non-parallel uploads).\n
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.\n
-u | --update - Update the installed script in your system.\n
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.
$([[ ${GUPLOAD_INSTALLED_WITH} = script ]] && printf '%s\n' '\n -u | --update - Update the installed script in your system.\n
-U | --uninstall - Uninstall script, remove related files.\n')
--info - Show detailed info, only if script is installed system wide.\n
-U | --uninstall - Uninstall script, remove related files.\n
-D | --debug - Display script command trace.\n
-h | --help - Display this message.\n"
exit 0
Expand All @@ -978,71 +978,6 @@ _short_help() {
exit 0
}

###################################################
# Automatic updater, only update if script is installed system wide.
# Globals: 5 variables, 2 functions
# COMMAND_NAME, REPO, INSTALL_PATH, TYPE, TYPE_VALUE | _update, _update_value
# Arguments: None
# Result: On
# Update if AUTO_UPDATE_INTERVAL + LAST_UPDATE_TIME less than printf "%(%s)T\\n" "-1"
###################################################
_auto_update() {
export REPO
command -v "${COMMAND_NAME}" 1> /dev/null &&
if [[ -n "${REPO:+${COMMAND_NAME:+${INSTALL_PATH:+${TYPE:+${TYPE_VALUE}}}}}" ]]; then
current_time="$(printf "%(%s)T\\n" "-1")"
[[ $((LAST_UPDATE_TIME + AUTO_UPDATE_INTERVAL)) -lt ${current_time} ]] && _update
_update_value LAST_UPDATE_TIME "${current_time}"
fi
return 0
}

###################################################
# Install/Update/uninstall the script.
# Globals: 4 variables
# Varibles - HOME, REPO, TYPE_VALUE, GLOBAL_INSTALL
# Arguments: 1
# ${1} = uninstall or update
# Result: On
# ${1} = nothing - Update the script if installed, otherwise install.
# ${1} = uninstall - uninstall the script
###################################################
_update() {
declare job="${1:-update}"
[[ ${GLOBAL_INSTALL} = true ]] && ! [[ $(id -u) = 0 ]] && printf "%s\n" "Error: Need root access to update." && return 0
[[ ${job} =~ uninstall ]] && job_uninstall="--uninstall"
_print_center "justify" "Fetching ${job} script.." "-"
declare repo="${REPO:-labbots/google-drive-upload}" type_value="${TYPE_VALUE:-latest}" cmd="${COMMAND_NAME:-gupload}" path="${INSTALL_PATH:-${HOME}/.google-drive-upload/bin}"
{ [[ ${TYPE:-} != branch ]] && type_value="$(_get_latest_sha release "${type_value}" "${repo}")"; } || :
if script="$(curl --compressed -Ls "https://github.com/${repo}/raw/${type_value}/install.sh")"; then
_clear_line 1
printf "%s\n" "${script}" | bash -s -- ${job_uninstall:-} --skip-internet-check --cmd "${cmd}" --path "${path}"
current_time="$(printf "%(%s)T\\n" "-1")"
[[ -z ${job_uninstall} ]] && _update_value LAST_UPDATE_TIME "${current_time}" &
else
_clear_line 1
"${QUIET:-_print_center}" "justify" "Error: Cannot download ${job} script." "=" 1>&2
return 1
fi
return 0
}

###################################################
# Update in-script values
###################################################
_update_value() {
declare command_path="${INSTALL_PATH:?}/${COMMAND_NAME}" \
value_name="${1:-}" value="${2:-}" script_without_value_and_shebang
script_without_value_and_shebang="$(grep -v "${value_name}=\".*\".* # added values" "${command_path}" | sed 1d)"
new_script="$(
sed -n 1p "${command_path}"
printf "%s\n" "${value_name}=\"${value}\" # added values"
printf "%s\n" "${script_without_value_and_shebang}"
)"
chmod +w "${command_path}" && printf "%s\n" "${new_script}" >| "${command_path}" && chmod -w "${command_path}"
return 0
}

###################################################
# Print info if installed
# Globals: 7 variable
Expand Down Expand Up @@ -1142,8 +1077,6 @@ _setup_arguments() {
case "${1}" in
-h | --help) _usage ;;
-D | --debug) DEBUG="true" && export DEBUG ;;
-u | --update) _check_debug && _update && exit "${?}" ;;
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
--info) _version_info ;;
-c | -C | --create-dir)
_check_longoptions "${1}" "${2}"
Expand Down Expand Up @@ -1229,6 +1162,12 @@ _setup_arguments() {
'') shorthelp ;;
*) # Check if user meant it to be a flag
if [[ ${1} = -* ]]; then
[[ ${GUPLOAD_INSTALLED_WITH} = script ]] && {
case "${1}" in
-u | --update) _check_debug && _update && exit "${?}" ;;
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
esac
}
printf '%s: %s: Unknown option\nTry '"%s -h/--help"' for more information.\n' "${0##*/}" "${1}" "${0##*/}" && exit 1
else
if [[ ${1} =~ (drive.google.com|docs.google.com) ]]; then
Expand Down Expand Up @@ -1700,7 +1639,7 @@ main() {
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
else
{ _cleanup_config "${CONFIG}" && _auto_update; } 1>| /dev/null &
{ _cleanup_config "${CONFIG}" && [[ ${GUPLOAD_INSTALLED_WITH} = script ]] && _auto_update; } 1>| /dev/null &
fi
} 2>| /dev/null || :
return 0
Expand Down
81 changes: 10 additions & 71 deletions bash/upload.bash
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ Options:\n
--hide - This flag will prevent the script to print sensitive information like root folder id or drivelink.\n
-v | --verbose - Display detailed message (only for non-parallel uploads).\n
-V | --verbose-progress - Display detailed message and detailed upload progress(only for non-parallel uploads).\n
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.\n
-u | --update - Update the installed script in your system.\n
--skip-internet-check - Do not check for internet connection, recommended to use in sync jobs.
$([[ ${GUPLOAD_INSTALLED_WITH} = script ]] && printf '%s\n' '\n -u | --update - Update the installed script in your system.\n
-U | --uninstall - Uninstall script, remove related files.\n')
--info - Show detailed info, only if script is installed system wide.\n
-U | --uninstall - Uninstall script, remove related files.\n
-D | --debug - Display script command trace.\n
-h | --help - Display this message.\n"
exit 0
Expand All @@ -45,71 +45,6 @@ _short_help() {
exit 0
}

###################################################
# Automatic updater, only update if script is installed system wide.
# Globals: 5 variables, 2 functions
# COMMAND_NAME, REPO, INSTALL_PATH, TYPE, TYPE_VALUE | _update, _update_value
# Arguments: None
# Result: On
# Update if AUTO_UPDATE_INTERVAL + LAST_UPDATE_TIME less than printf "%(%s)T\\n" "-1"
###################################################
_auto_update() {
export REPO
command -v "${COMMAND_NAME}" 1> /dev/null &&
if [[ -n "${REPO:+${COMMAND_NAME:+${INSTALL_PATH:+${TYPE:+${TYPE_VALUE}}}}}" ]]; then
current_time="$(printf "%(%s)T\\n" "-1")"
[[ $((LAST_UPDATE_TIME + AUTO_UPDATE_INTERVAL)) -lt ${current_time} ]] && _update
_update_value LAST_UPDATE_TIME "${current_time}"
fi
return 0
}

###################################################
# Install/Update/uninstall the script.
# Globals: 4 variables
# Varibles - HOME, REPO, TYPE_VALUE, GLOBAL_INSTALL
# Arguments: 1
# ${1} = uninstall or update
# Result: On
# ${1} = nothing - Update the script if installed, otherwise install.
# ${1} = uninstall - uninstall the script
###################################################
_update() {
declare job="${1:-update}"
[[ ${GLOBAL_INSTALL} = true ]] && ! [[ $(id -u) = 0 ]] && printf "%s\n" "Error: Need root access to update." && return 0
[[ ${job} =~ uninstall ]] && job_uninstall="--uninstall"
_print_center "justify" "Fetching ${job} script.." "-"
declare repo="${REPO:-labbots/google-drive-upload}" type_value="${TYPE_VALUE:-latest}" cmd="${COMMAND_NAME:-gupload}" path="${INSTALL_PATH:-${HOME}/.google-drive-upload/bin}"
{ [[ ${TYPE:-} != branch ]] && type_value="$(_get_latest_sha release "${type_value}" "${repo}")"; } || :
if script="$(curl --compressed -Ls "https://github.com/${repo}/raw/${type_value}/install.sh")"; then
_clear_line 1
printf "%s\n" "${script}" | bash -s -- ${job_uninstall:-} --skip-internet-check --cmd "${cmd}" --path "${path}"
current_time="$(printf "%(%s)T\\n" "-1")"
[[ -z ${job_uninstall} ]] && _update_value LAST_UPDATE_TIME "${current_time}" &
else
_clear_line 1
"${QUIET:-_print_center}" "justify" "Error: Cannot download ${job} script." "=" 1>&2
return 1
fi
return 0
}

###################################################
# Update in-script values
###################################################
_update_value() {
declare command_path="${INSTALL_PATH:?}/${COMMAND_NAME}" \
value_name="${1:-}" value="${2:-}" script_without_value_and_shebang
script_without_value_and_shebang="$(grep -v "${value_name}=\".*\".* # added values" "${command_path}" | sed 1d)"
new_script="$(
sed -n 1p "${command_path}"
printf "%s\n" "${value_name}=\"${value}\" # added values"
printf "%s\n" "${script_without_value_and_shebang}"
)"
chmod +w "${command_path}" && printf "%s\n" "${new_script}" >| "${command_path}" && chmod -w "${command_path}"
return 0
}

###################################################
# Print info if installed
# Globals: 7 variable
Expand Down Expand Up @@ -209,8 +144,6 @@ _setup_arguments() {
case "${1}" in
-h | --help) _usage ;;
-D | --debug) DEBUG="true" && export DEBUG ;;
-u | --update) _check_debug && _update && exit "${?}" ;;
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
--info) _version_info ;;
-c | -C | --create-dir)
_check_longoptions "${1}" "${2}"
Expand Down Expand Up @@ -296,6 +229,12 @@ _setup_arguments() {
'') shorthelp ;;
*) # Check if user meant it to be a flag
if [[ ${1} = -* ]]; then
[[ ${GUPLOAD_INSTALLED_WITH} = script ]] && {
case "${1}" in
-u | --update) _check_debug && _update && exit "${?}" ;;
--uninstall) _check_debug && _update uninstall && exit "${?}" ;;
esac
}
printf '%s: %s: Unknown option\nTry '"%s -h/--help"' for more information.\n' "${0##*/}" "${1}" "${0##*/}" && exit 1
else
if [[ ${1} =~ (drive.google.com|docs.google.com) ]]; then
Expand Down Expand Up @@ -767,7 +706,7 @@ main() {
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
else
{ _cleanup_config "${CONFIG}" && _auto_update; } 1>| /dev/null &
{ _cleanup_config "${CONFIG}" && [[ ${GUPLOAD_INSTALLED_WITH} = script ]] && _auto_update; } 1>| /dev/null &
fi
} 2>| /dev/null || :
return 0
Expand Down
80 changes: 77 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,90 @@ _variables() {
date +'%s'
fi)" && export LAST_UPDATE_TIME
GLOBAL_INSTALL="false"
export GUPLOAD_INSTALLED_WITH="script"

[ -n "${SKIP_SYNC}" ] && SYNC_COMMAND_NAME=""
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"
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"

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
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
VALUES_REGEX="${VALUES_REGEX:+${VALUES_REGEX}|}^${i}=\".*\".* # added values"
done

return 0
}

###################################################
# For self and automatic updates
###################################################
_print_self_update_code() {
cat << 'EOF'
###################################################
# Automatic updater, only update if script is installed system wide.
# Globals: 5 variables, 2 functions
# COMMAND_NAME, REPO, INSTALL_PATH, TYPE, TYPE_VALUE | _update, _update_value
# Arguments: None
# Result: On
# Update if AUTO_UPDATE_INTERVAL + LAST_UPDATE_TIME less than printf "%(%s)T\\n" "-1"
###################################################
_auto_update() {
export REPO
command -v "${COMMAND_NAME}" 1> /dev/null &&
if [ -n "${REPO:+${COMMAND_NAME:+${INSTALL_PATH:+${TYPE:+${TYPE_VALUE}}}}}" ]; then
current_time="$(date +'%s')"
[ "$((LAST_UPDATE_TIME + AUTO_UPDATE_INTERVAL))" -lt "$(date +'%s')" ] && _update
_update_value LAST_UPDATE_TIME "${current_time}"
fi
return 0
}
###################################################
# Install/Update/uninstall the script.
# Globals: 4 variables
# Varibles - HOME, REPO, TYPE_VALUE, GLOBAL_INSTALL
# Arguments: 1
# ${1} = uninstall or update
# Result: On
# ${1} = nothing - Update the script if installed, otherwise install.
# ${1} = uninstall - uninstall the script
###################################################
_update() {
job_update="${1:-update}"
[ "${GLOBAL_INSTALL}" = true ] && ! [ "$(id -u)" = 0 ] && printf "%s\n" "Error: Need root access to update." && return 0
[ "${job_update}" = uninstall ] && job_uninstall="--uninstall"
_print_center "justify" "Fetching ${job_update} script.." "-"
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}"
{ [ "${TYPE:-}" != branch ] && type_value_update="$(_get_latest_sha release "${type_value_update}" "${repo_update}")"; } || :
if script_update="$(curl --compressed -Ls "https://github.com/${repo_update}/raw/${type_value_update}/install.sh")"; then
_clear_line 1
printf "%s\n" "${script_update}" | sh -s -- ${job_uninstall:-} --skip-internet-check --cmd "${cmd_update}" --path "${path_update}"
current_time="$(date +'%s')"
[ -z "${job_uninstall}" ] && _update_value LAST_UPDATE_TIME "${current_time}"
else
_clear_line 1
"${QUIET:-_print_center}" "justify" "Error: Cannot download" " ${job_update} script." "=" 1>&2
return 1
fi
return 0
}
###################################################
# Update in-script values
###################################################
_update_value() {
command_path="${INSTALL_PATH:?}/${COMMAND_NAME}"
value_name="${1:-}" value="${2:-}"
script_without_value_and_shebang="$(grep -v "${value_name}=\".*\".* # added values" "${command_path}" | sed 1d)"
new_script="$(
sed -n 1p "${command_path}"
printf "%s\n" "${value_name}=\"${value}\" # added values"
printf "%s\n" "${script_without_value_and_shebang}"
)"
chmod +w "${command_path}" && printf "%s\n" "${new_script}" >| "${command_path}" && chmod -w "${command_path}"
return 0
}
EOF
}

###################################################
# Download scripts
###################################################
Expand Down Expand Up @@ -417,10 +490,11 @@ _inject_values() {
chmod +w "${INSTALL_PATH}/${COMMAND_NAME}"
{
printf "%s\n" "${shebang}"
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
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
printf "%s\n" "${i}=\"$(eval printf "%s" \"\$"${i}"\")\" # added values"
done
printf "%s\n" "LATEST_INSTALLED_SHA=\"${LATEST_CURRENT_SHA}\" # added values"
_print_self_update_code # inject the self and auto update functions
printf "%s\n" "${script_without_values_and_shebang}"
} 1>| "${INSTALL_PATH}/${COMMAND_NAME}"

Expand Down

0 comments on commit 5c7340d

Please sign in to comment.