Skip to content

Commit

Permalink
[1/2] Add support for multiple accounts in config | Fix labbots#98
Browse files Browse the repository at this point in the history
Add four new flags

* -a | --account - Use a different account than default

* -la | --list accounts - List all the accounts configured with CLIENT_ID, CLIENT_SECRET and REFRESH_TOKEN

* -ca | --create-account - Create a new account

* -da | --delete-account - Delete an existing account

The config format is changed, ACCOUNT_${account}_ prefix is added to every config var, where ${account} si the account name

The old configs will be automatically migrated with account name default( if default taken then a number suffix will be added like default1, default2, etc )

move authentication/config related functions to auth-utils.[bash|sh]
  • Loading branch information
Akianonymus committed Jan 4, 2021
1 parent 28cd628 commit 36e0fa5
Show file tree
Hide file tree
Showing 13 changed files with 1,992 additions and 727 deletions.
416 changes: 416 additions & 0 deletions bash/auth-utils.bash

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions bash/common-utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ _print_center() {
return 0
}

###################################################
# Evaluates value1=value2
# Globals: None
# Arguments: 3
# ${1} = direct ( d ) or indirect ( i ) - ( evaluation mode )
# ${2} = var name
# ${3} = var value
# Result: export value1=value2
###################################################
_set_value() {
mode="${1:?}" var="${2:?}" value="${3:?}"
case "${mode}" in
d | direct) export "${var}=${value}" ;;
i | indirect) export "${var}=${!value}" ;;
esac
}

###################################################
# Check if script terminal supports ansi escapes
# Globals: 1 variable
Expand Down
22 changes: 0 additions & 22 deletions bash/drive-utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,6 @@ _extract_id() {
printf "%b" "${ID:+${ID}\n}"
}

###################################################
# Method to regenerate access_token ( also updates in config ).
# Make a request on https://www.googleapis.com/oauth2/""${API_VERSION}""/tokeninfo?access_token=${ACCESS_TOKEN} url and check if the given token is valid, if not generate one.
# Globals: 9 variables, 2 functions
# Variables - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, TOKEN_URL, CONFIG, API_URL, API_VERSION, QUIET, NO_UPDATE_TOKEN
# Functions - _update_config and _print_center
# Result: Update access_token and expiry else print error
###################################################
_get_access_token_and_update() {
RESPONSE="${1:-$(curl --compressed -s -X POST --data "client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token" "${TOKEN_URL}")}" || :
if ACCESS_TOKEN="$(_json_value access_token 1 1 <<< "${RESPONSE}")"; then
ACCESS_TOKEN_EXPIRY="$(($(printf "%(%s)T\\n" "-1") + $(_json_value expires_in 1 1 <<< "${RESPONSE}") - 1))"
_update_config ACCESS_TOKEN "${ACCESS_TOKEN}" "${CONFIG}"
_update_config ACCESS_TOKEN_EXPIRY "${ACCESS_TOKEN_EXPIRY}" "${CONFIG}"
else
"${QUIET:-_print_center}" "justify" "Error: Something went wrong" ", printing error." "=" 1>&2
printf "%s\n" "${RESPONSE}" 1>&2
return 1
fi
return 0
}

###################################################
# Upload ( Create/Update ) files on gdrive.
# Interrupted uploads can be resumed.
Expand Down
17 changes: 17 additions & 0 deletions bash/release/gsync
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ _print_center() {
return 0
}

###################################################
# Evaluates value1=value2
# Globals: None
# Arguments: 3
# ${1} = direct ( d ) or indirect ( i ) - ( evaluation mode )
# ${2} = var name
# ${3} = var value
# Result: export value1=value2
###################################################
_set_value() {
mode="${1:?}" var="${2:?}" value="${3:?}"
case "${mode}" in
d | direct) export "${var}=${value}" ;;
i | indirect) export "${var}=${!value}" ;;
esac
}

###################################################
# Check if script terminal supports ansi escapes
# Globals: 1 variable
Expand Down
Loading

0 comments on commit 36e0fa5

Please sign in to comment.