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

45
ufw
View File

@ -44,7 +44,6 @@ usage() {
-t|--tags "<tag1[, tag2,..]>" # Add tags to the file. -t|--tags "<tag1[, tag2,..]>" # Add tags to the file.
-q|--quiet # Be quiet. -q|--quiet # Be quiet.
-v|--void # Don't add the file to my files. -v|--void # Don't add the file to my files.
-c|--config <file> # Use an alternate config file
Config options (~/.config/ufw): Config options (~/.config/ufw):
secret # Your personal token. Get it at https://zfh.so/settings_form secret # Your personal token. Get it at https://zfh.so/settings_form
@ -54,6 +53,9 @@ usage() {
cfg_url_regex cfg_url_regex
cfg_tmp_dir cfg_tmp_dir
cfg_service_url cfg_service_url
Environment variables:
UFW_CFG_FILE # Config file to read, defaults to \$XDG_CONFIG_DIR/ufw
EOF EOF
} }
@ -286,13 +288,15 @@ get_token() {
read -sp 'Password: ' password read -sp 'Password: ' password
printf '\n' 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" || { get_api_status <<< "$login_response" || {
jq -r '.status.message' <<< "$login_response" jq -r '.status.message' <<< "$login_response"
return 1 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 if get_api_status <<< "$api_response"; then
secret=$(jq -r '.data.token' <<< "$api_response") secret=$(jq -r '.data.token' <<< "$api_response")
printf 'export secret="%s"' "$secret" >> "$cfg_file" printf 'export secret="%s"' "$secret" >> "$cfg_file"
@ -300,6 +304,7 @@ get_token() {
printf 'Done\n' printf 'Done\n'
else else
printf 'Error: %s %s\n' "$api_status" "$api_status_message" printf 'Error: %s %s\n' "$api_status" "$api_status_message"
return 1
fi fi
} }
@ -355,6 +360,23 @@ main() {
[[ $1 ]] || { usage; return 1; } [[ $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 while [[ "$1" ]]; do
case "$1" in case "$1" in
-h|--help|--usage) usage; return;; -h|--help|--usage) usage; return;;
@ -362,7 +384,6 @@ main() {
-m|--max-filesize) cfg_max_filesize=$2; shift;; -m|--max-filesize) cfg_max_filesize=$2; shift;;
-A|--album-name) album_name=$2; shift;; -A|--album-name) album_name=$2; shift;;
-t|--tags) tags=$2; shift;; -t|--tags) tags=$2; shift;;
-c|--config) cfg_file=$2; shift;;
-i|--my-ip) action='getmyip';; -i|--my-ip) action='getmyip';;
-u|--shorten) action='url';; -u|--shorten) action='url';;
@ -406,22 +427,6 @@ main() {
shift shift
done 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" TEMPDIR="$cfg_tmp_dir"
for i in "$cfg_tmp_dir"; do for i in "$cfg_tmp_dir"; do