A better way

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2019-11-24 13:59:08 +03:00
parent 146f06e7c5
commit 03e0fda896
1 changed files with 25 additions and 20 deletions

45
ufw
View File

@ -44,7 +44,6 @@ usage() {
-t|--tags "<tag1[, tag2,..]>" # Add tags to the file.
-q|--quiet # Be quiet.
-v|--void # Don't add the file to my files.
-c|--config <file> # 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