Skip to content

Commit

Permalink
gupload: Add _cleanup_config function
Browse files Browse the repository at this point in the history
this function removes any expired tokens on the basis of expiry

will be useful once service accounts or multi account config implemented
  • Loading branch information
Akianonymus committed Nov 3, 2020
1 parent fce68fb commit e3625c6
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
31 changes: 30 additions & 1 deletion bash/release/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,35 @@ _version_info() {
exit 0
}

###################################################
# Function to cleanup config file
# Remove invalid access tokens on the basis of corresponding expiry
# Globals: None
# Arguments: 1
# ${1} = config file
# Result: read description
###################################################
_cleanup_config() {
declare config="${1:?Error: Missing config}" values_regex

! [ -f "${config}" ] && return 0

while read -r line; do
expiry_value_name="${line%%=*}"
token_value_name="${expiry_value_name%%_EXPIRY}"

: "${line##*=}" && : "${_%\"}" && expiry="${_#\"}"
[[ ${expiry} -le "$(printf "%(%s)T\\n" "-1")" ]] &&
values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\""

done <<< "$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :)"

chmod +w "${config}" &&
printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" &&
chmod -w "${config}"
return 0
}

###################################################
# Process all arguments given to the script
# Globals: 2 variable, 1 function
Expand Down Expand Up @@ -1672,7 +1701,7 @@ main() {
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
else
_auto_update &
{ _cleanup_config "${CONFIG}" && _auto_update; } &
fi
} 2>| /dev/null 1>&2 || :
return 0
Expand Down
31 changes: 30 additions & 1 deletion bash/upload.bash
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ _version_info() {
exit 0
}

###################################################
# Function to cleanup config file
# Remove invalid access tokens on the basis of corresponding expiry
# Globals: None
# Arguments: 1
# ${1} = config file
# Result: read description
###################################################
_cleanup_config() {
declare config="${1:?Error: Missing config}" values_regex

! [ -f "${config}" ] && return 0

while read -r line; do
expiry_value_name="${line%%=*}"
token_value_name="${expiry_value_name%%_EXPIRY}"

: "${line##*=}" && : "${_%\"}" && expiry="${_#\"}"
[[ ${expiry} -le "$(printf "%(%s)T\\n" "-1")" ]] &&
values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\""

done <<< "$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :)"

chmod +w "${config}" &&
printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" &&
chmod -w "${config}"
return 0
}

###################################################
# Process all arguments given to the script
# Globals: 2 variable, 1 function
Expand Down Expand Up @@ -739,7 +768,7 @@ main() {
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
else
_auto_update &
{ _cleanup_config "${CONFIG}" && _auto_update; } &
fi
} 2>| /dev/null 1>&2 || :
return 0
Expand Down
32 changes: 31 additions & 1 deletion sh/release/gupload
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,36 @@ _version_info() {
exit 0
}
###################################################
# Function to cleanup config file
# Remove invalid access tokens on the basis of corresponding expiry
# Globals: None
# Arguments: 1
# ${1} = config file
# Result: read description
###################################################
_cleanup_config() {
config="${1:?Error: Missing config}" && unset values_regex _tmp
! [ -f "${config}" ] && return 0
while read -r line <&4; do
expiry_value_name="${line%%=*}"
token_value_name="${expiry_value_name%%_EXPIRY}"
_tmp="${line##*=}" && _tmp="${_tmp%\"}" && expiry="${_tmp#\"}"
[ "${expiry}" -le "$(date +"%s")" ] &&
values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\""
done 4<< EOF
$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :)
EOF
chmod +w "${config}" &&
printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" &&
chmod -w "${config}"
return 0
}
###################################################
# Process all arguments given to the script
# Globals: 1 variable, 1 function
Expand Down Expand Up @@ -1682,7 +1712,7 @@ main() {
printf "\n\n%s\n" "Script exited manually."
kill -9 -$$ &
else
_auto_update &
{ _cleanup_config "${CONFIG}" && _auto_update; } &
fi
} 2>| /dev/null 1>&2 || :
return 0
Expand Down
32 changes: 31 additions & 1 deletion sh/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,36 @@ _version_info() {
exit 0
}

###################################################
# Function to cleanup config file
# Remove invalid access tokens on the basis of corresponding expiry
# Globals: None
# Arguments: 1
# ${1} = config file
# Result: read description
###################################################
_cleanup_config() {
config="${1:?Error: Missing config}" && unset values_regex _tmp

! [ -f "${config}" ] && return 0

while read -r line <&4; do
expiry_value_name="${line%%=*}"
token_value_name="${expiry_value_name%%_EXPIRY}"

_tmp="${line##*=}" && _tmp="${_tmp%\"}" && expiry="${_tmp#\"}"
[ "${expiry}" -le "$(date +"%s")" ] &&
values_regex="${values_regex:+${values_regex}|}${expiry_value_name}=\".*\"|${token_value_name}=\".*\""
done 4<< EOF
$(grep -F ACCESS_TOKEN_EXPIRY "${config}" || :)
EOF

chmod +w "${config}" &&
printf "%s\n" "$(grep -Ev "^\$${values_regex:+|${values_regex}}" "${config}")" >| "${config}" &&
chmod -w "${config}"
return 0
}

###################################################
# Process all arguments given to the script
# Globals: 1 variable, 1 function
Expand Down Expand Up @@ -764,7 +794,7 @@ main() {
printf "\n\n%s\n" "Script exited manually."
kill -9 -$$ &
else
_auto_update &
{ _cleanup_config "${CONFIG}" && _auto_update; } &
fi
} 2>| /dev/null 1>&2 || :
return 0
Expand Down

0 comments on commit e3625c6

Please sign in to comment.