using env variables for TOKEN, USER and EMAIL

This commit is contained in:
Lucas Frérot 2024-12-28 09:50:52 +01:00
parent 845f0b00e1
commit f06d537ffb
No known key found for this signature in database
GPG Key ID: 03B54A50E3FBA7E8
1 changed files with 21 additions and 5 deletions

26
rtenets
View File

@ -63,6 +63,23 @@ enter() {
printf "${input_var}"
}
# Test if a variable name is set
is_set() {
local var_name="$1"
eval "! [[ -z \"\${${var_name}+x}\" ]]"
return $?
}
# Set var to first value if unset
alt_var() {
local value_if_unset="$1"
local variable="$2"
if is_set "${variable}"; then
printf "%s" "$(eval "printf \"%s\" \"\${${variable}}\"")"
else
printf "%s" "$value_if_unset"
fi
}
# Check that command exists
has_command() {
@ -88,13 +105,13 @@ get_gitea_token(){
TOKEN="$(enter "gitea token: ")"
fi
readonly TOKEN
info "gitea token: '${TOKEN}'"
}
gitea() {
check_api_prerequisites
readonly TOKEN
if [[ -z ${TOKEN+x} ]]; then
if ! is_set TOKEN; then
get_gitea_token
fi
@ -136,8 +153,8 @@ check_git_config() {
error "git not found, please install"
fi
readonly USER="$(get_git_config user.name)"
readonly EMAIL="$(get_git_config user.email)"
readonly USER="$(alt_var "$(get_git_config user.name)" USER)"
readonly EMAIL="$(alt_var "$(get_git_config user.email)" EMAIL)"
info "found git credentials:\\n - user.name: '${USER}'\\n - user.email: '${EMAIL}'"
}
@ -260,7 +277,6 @@ main() {
check_git_config
create_tenet_file_tree
get_gitea_token
gitea /user GET
}