alternate config file

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2019-11-24 13:50:24 +03:00
parent d2529e32bd
commit 146f06e7c5

52
ufw
View File

@ -44,6 +44,7 @@ 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
@ -276,6 +277,7 @@ login() {
get_token() { get_token() {
[[ $secret ]] && return 0 [[ $secret ]] && return 0
[[ -f $cfg_file ]] && return 1
declare api_response login password declare api_response login password
@ -293,8 +295,8 @@ get_token() {
api_response=$(curl -b $XDG_CONFIG_DIR/ufw.cookiejar -sl "$cfg_service_url/api_token.json") api_response=$(curl -b $XDG_CONFIG_DIR/ufw.cookiejar -sl "$cfg_service_url/api_token.json")
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" > "$XDG_CONFIG_DIR/ufw" printf 'export secret="%s"' "$secret" >> "$cfg_file"
chmod 600 "$XDG_CONFIG_DIR/ufw" chmod 600 "$cfg_file"
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"
@ -338,7 +340,7 @@ set_argv() {
main() { main() {
declare args file target flag_private flag_rm flag_scn flag_scn_fullscreen cfg_scn_delay flag_notify declare args file target flag_private flag_rm flag_scn flag_scn_fullscreen cfg_scn_delay flag_notify
declare flag_shortlink=0 declare flag_shortlink
# Defaults for XDG # Defaults for XDG
if ! [[ "$XDG_RUNTIME_DIR" ]]; then if ! [[ "$XDG_RUNTIME_DIR" ]]; then
@ -350,25 +352,8 @@ main() {
fi fi
cfg_url_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+' cfg_url_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+'
cfg_tmp_dir="$XDG_RUNTIME_DIR/ufw"; TEMPDIR="$cfg_tmp_dir"
cfg_service_url='https://8fw.me'
cfg_screenshot_ext='png'
cfg_max_filesize='200M'
flag_shortlink=0 [[ $1 ]] || { usage; return 1; }
flag_directlink=1
if [[ -f "$HOME/.suprc" ]]; then
printf 'Found legacy configfile, moving...\n' >&2
mv "$HOME/.suprc" "$XDG_CONFIG_DIR/ufw"
fi
if [[ -f "$XDG_CONFIG_DIR/ufw" ]]; then
if ! source "$XDG_CONFIG_DIR/ufw"; then
printf 'Failed to source configuration file: %s\n' "$XDG_CONFIG_DIR/ufw"
return $?
fi
fi
while [[ "$1" ]]; do while [[ "$1" ]]; do
case "$1" in case "$1" in
@ -377,6 +362,7 @@ 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';;
@ -420,14 +406,32 @@ 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"
for i in "$cfg_tmp_dir"; do for i in "$cfg_tmp_dir"; do
[[ -d "$i" ]] || { mkdir -p "$i"; } [[ -d "$i" ]] || { mkdir -p "$i"; }
done done
get_token || return $?
case "${action:-upload}" in case "${action:-upload}" in
upload) upload)
get_token || return $?
if (( $# )); then if (( $# )); then
for t in "$@"; do for t in "$@"; do
upload "$t" upload "$t"
@ -438,6 +442,8 @@ main() {
;; ;;
url) url)
get_token || return $?
for t in "$@"; do for t in "$@"; do
get_shortlink "$1" get_shortlink "$1"
done done