From f06d537ffb9ce1554db4d73713c506ebf800211f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Fr=C3=A9rot?= Date: Sat, 28 Dec 2024 09:50:52 +0100 Subject: [PATCH] using env variables for TOKEN, USER and EMAIL --- rtenets | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/rtenets b/rtenets index 861f2b5..e8bee88 100755 --- a/rtenets +++ b/rtenets @@ -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 }