From 03e0fda8964e4826be2c86bbfad88a02c8534b69 Mon Sep 17 00:00:00 2001 From: fbt Date: Sun, 24 Nov 2019 13:59:08 +0300 Subject: [PATCH] A better way Signed-off-by: fbt --- ufw | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/ufw b/ufw index f886d70..00021cf 100755 --- a/ufw +++ b/ufw @@ -44,7 +44,6 @@ usage() { -t|--tags "" # Add tags to the file. -q|--quiet # Be quiet. -v|--void # Don't add the file to my files. - -c|--config # Use an alternate config file Config options (~/.config/ufw): secret # Your personal token. Get it at https://zfh.so/settings_form @@ -54,6 +53,9 @@ usage() { cfg_url_regex cfg_tmp_dir cfg_service_url + + Environment variables: + UFW_CFG_FILE # Config file to read, defaults to \$XDG_CONFIG_DIR/ufw EOF } @@ -286,13 +288,15 @@ get_token() { read -sp 'Password: ' password printf '\n' - login_response=$(curl -c $XDG_CONFIG_DIR/ufw.cookiejar -d "login=$login" -d "password=$password" -sl "$cfg_service_url/login.json") + login_response=$(curl -c "$cfg_file.cookiejar" -d "login=$login" -d "password=$password" -sl "$cfg_service_url/login.json") get_api_status <<< "$login_response" || { jq -r '.status.message' <<< "$login_response" return 1 } - api_response=$(curl -b $XDG_CONFIG_DIR/ufw.cookiejar -sl "$cfg_service_url/api_token.json") + api_response=$(curl -b "$cfg_file.cookiejar" -sl "$cfg_service_url/api_token.json") + rm -f "$cfg_file.cookiejar" + if get_api_status <<< "$api_response"; then secret=$(jq -r '.data.token' <<< "$api_response") printf 'export secret="%s"' "$secret" >> "$cfg_file" @@ -300,6 +304,7 @@ get_token() { printf 'Done\n' else printf 'Error: %s %s\n' "$api_status" "$api_status_message" + return 1 fi } @@ -355,6 +360,23 @@ main() { [[ $1 ]] || { usage; return 1; } + [[ $UFW_CFG_FILE ]] && cfg_file=$UFW_CFG_FILE + if [[ -f "$cfg_file" ]]; then + if ! source "$cfg_file"; then + printf 'Failed to source configuration file: %s\n' "$cfg_file" + return $? + fi + fi + + # Defaults + : ${cfg_file:="$XDG_CONFIG_DIR/ufw"} + : ${cfg_tmp_dir:="$XDG_RUNTIME_DIR/ufw"} + : ${cfg_service_url:='https://8fw.me'} + : ${cfg_screenshot_ext:='png'} + : ${cfg_max_filesize:='200M'} + : ${flag_shortlink:=0} + : ${flag_directlink:=1} + while [[ "$1" ]]; do case "$1" in -h|--help|--usage) usage; return;; @@ -362,7 +384,6 @@ main() { -m|--max-filesize) cfg_max_filesize=$2; shift;; -A|--album-name) album_name=$2; shift;; -t|--tags) tags=$2; shift;; - -c|--config) cfg_file=$2; shift;; -i|--my-ip) action='getmyip';; -u|--shorten) action='url';; @@ -406,22 +427,6 @@ main() { shift done - # Defaults - : ${cfg_file:="$XDG_CONFIG_DIR/ufw"} - : ${cfg_tmp_dir:="$XDG_RUNTIME_DIR/ufw"} - : ${cfg_service_url:='https://8fw.me'} - : ${cfg_screenshot_ext:='png'} - : ${cfg_max_filesize:='200M'} - : ${flag_shortlink:=0} - : ${flag_directlink:=1} - - if [[ -f "$cfg_file" ]]; then - if ! source "$cfg_file"; then - printf 'Failed to source configuration file: %s\n' "$cfg_file" - return $? - fi - fi - TEMPDIR="$cfg_tmp_dir" for i in "$cfg_tmp_dir"; do