cask_switch_https: general improvements

This commit is contained in:
Vítor Galvão
2016-09-08 18:29:31 +01:00
committed by GitHub
parent e9a36e768f
commit d40d6bdb64
+28 -29
View File
@@ -2,21 +2,21 @@
set -o pipefail
readonly program="$(basename "$0")"
readonly program="$(basename "${0}")"
skip_curl_verify=0
verbose=0
syntax_error() {
echo "$program: $1" >&2
echo "Try \`$program --help\` for more information." >&2
echo "${program}: ${1}" >&2
echo "Try \`${program} --help\` for more information." >&2
exit 1
}
depends_on() {
formula="$1"
[[ "$#" -eq 2 ]] && cmd="$2" || cmd=$(basename "${formula}")
formula="${1}"
[[ "$#" -eq 2 ]] && cmd="${2}" || cmd=$(basename "${formula}")
if [[ ! $(which ${cmd}) ]]; then
if [[ ! $(which "${cmd}") ]]; then
echo -e "$(tput setaf 1)
This script depends on '${cmd}'.
If you have [Homebrew](http://brew.sh), you can install it with 'brew install ${formula}'.
@@ -50,8 +50,8 @@ usage() {
}
# available flags
while [[ "$1" ]]; do
case "$1" in
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
@@ -63,7 +63,7 @@ while [[ "$1" ]]; do
verbose=1
;;
-*)
syntax_error "unrecognized option: $1"
syntax_error "unrecognized option: ${1}"
;;
*)
break
@@ -74,16 +74,16 @@ done
# define function to check if given URL exists and is reachable using HTTPS
check_url_for_https() {
cask_url="$1"
verbose_option=""
cask_url="${1}"
verbose_option=''
[[ ${verbose} -ne 0 ]] && verbose_option="-v "
[[ "${verbose}" -ne 0 ]] && verbose_option="-v "
# check if the URL sends a 200 HTTP code, else abort
curl-check-url ${verbose_option} "${cask_url}" > /dev/null
exit_code=$?
curl-check-url "${verbose_option}" "${cask_url}" > /dev/null
exit_code="$?"
if [[ exit_code -ne 0 ]]; then
if [[ "${exit_code}" -ne 0 ]]; then
echo "curl returned ${exit_code}: FAIL for ${cask_url}"
return 1
fi
@@ -93,10 +93,10 @@ check_url_for_https() {
# define function to modify part of stanza
replace_protocol_of_stanza() {
cask_file="$1"
stanza="$2"
old_value="$3"
new_value="$4"
cask_file="${1}"
stanza="${2}"
old_value="${3}"
new_value="${4}"
sed "s|${stanza} \(['\"]\)${old_value}://|${stanza} \1${new_value}://|g" "${cask_file}" > tmpfile
mv tmpfile "${cask_file}"
@@ -105,11 +105,11 @@ replace_protocol_of_stanza() {
# define abort function, that will reset the state
finish() {
# show message
if [[ "$1" == 'abort' ]]; then
echo -e "$(tput setaf 1)$2$(tput sgr0)\n"
if [[ "${1}" == 'abort' ]]; then
echo -e "$(tput setaf 1)${2}$(tput sgr0)\n"
[[ ! -z "${cask_file}" ]] && git checkout -- "${cask_file}"
exit 1
elif [[ "$1" == 'success' ]]; then
elif [[ "${1}" == 'success' ]]; then
echo -e "$(tput setaf 2)Updated: ${cask_name} is now using HTTPS$(tput sgr0)\n"
exit 0
fi
@@ -126,22 +126,21 @@ if [[ -z "${casks_dir}" ]]; then
fi
# exit if no argument was given: Run in current directory
if [[ -z "$1" ]]; then
if [[ -z "${1}" ]]; then
options=""
[[ ${skip_curl_verify} -ne 0 ]] && options+=" --skip-verify"
[[ ${verbose} -ne 0 ]] && options+=" --verbose"
for file in *.rb;
do
"$0" ${options} ${file}
"${0}" "${options}" "${file}"
done
exit 0
fi
# clean the cask's name, and check if it is valid
cask_name="$1"
[[ "${cask_name}" == *'.rb' ]] && cask_name=$(echo "${cask_name}" | sed 's|\.rb$||')
cask_name="${1%.rb}" # remove '.rb' extension, if present
cask_file="./${cask_name}.rb"
[[ ! -f "${cask_file}" ]] && finish abort 'There is no such cask'
@@ -167,17 +166,17 @@ for stanza in url appcast homepage; do
continue
fi
replace_protocol_of_stanza ${cask_file} ${stanza} "http" "https"
replace_protocol_of_stanza "${cask_file}" "${stanza}" "http" "https"
if [[ ${skip_curl_verify} -eq 0 ]]; then
check_url_for_https $(brew cask _stanza ${stanza} "${cask_name}")
check_url_for_https "$(brew cask _stanza "${stanza}" "${cask_name}")"
else
true
fi
if [[ $? -ne 0 ]]; then
echo "Restored original value for stanza ${stanza} as curl check failed"
replace_protocol_of_stanza ${cask_file} ${stanza} "https" "http"
replace_protocol_of_stanza "${cask_file}" "${stanza}" "https" "http"
else
updated_stanzas=$((updated_stanzas+1))
fi