imgs/imgs

75 lines
2.0 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/sh
# imgs CLI https://git.nxhs.cloud/ge/imgs
2022-01-05 08:32:46 +03:00
imgs_usage() {
cat <<- EOF
Upload images to remote imgs server.
2022-01-05 08:32:46 +03:00
Usage: imgs [-rvh] <file>...
2022-01-05 08:32:46 +03:00
Options:
-r, --remote remote imgs instance URI e.g. https://user:password@example.org
-v, --version print version and exit.
-h, --help print this help message and exit.
2022-01-05 08:32:46 +03:00
Environment variables:
IMGSREMOTE remote imgs instance URI.
IMGSDEBUG enables verbose mode and logging.
IMGSLOG path to logfile. Default: ~/imgs_debug.log
2022-01-05 08:32:46 +03:00
You can set variables in ~/.imgsremote file instead of ~/.bashrc
See <https://git.nxhs.cloud/ge/imgs> for more info.
EOF
2022-01-05 08:32:46 +03:00
}
[ "$#" -eq 0 ] && { imgs_usage; exit 1; }
2022-01-05 08:32:46 +03:00
# Transform long options to short ones
for arg in "$@"; do
shift
case "$arg" in
--remote) set -- "$@" "-r";;
--help) set -- "$@" "-h";;
--version) set -- "$@" "-v";;
*) set -- "$@" "$arg";;
esac
done
2022-01-05 08:32:46 +03:00
while getopts r:vh OPT; do
case "$OPT" in
r) IMGSREMOTE="$OPTARG";;
v) echo 'imgs CLI 1.1'; exit 0;;
h) imgs_usage; exit 0;;
*) echo "$0: Unknown option: $OPT" >&2; exit 1;;
2022-01-05 08:32:46 +03:00
esac
done
shift $((OPTIND - 1)) # shift for parse positional args
# Check variables
IMGSLOG="${IMGSLOG:-$HOME/imgs_debug.log}"
[ -n "$IMGSREMOTE" ] && return 0 # exit from func if variable is set
if [ -f "$HOME"/.imgsremote ]; then
# shellcheck source=/dev/null
. "$HOME"/.imgsremote
fi
if [ -z "$IMGSREMOTE" ]; then
echo "$0: Error: IMGSREMOTE variable is not set." >&2; exit 1
fi
2022-01-05 08:32:46 +03:00
[ -n "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Started" | tee -a "$IMGSLOG"
2022-01-05 08:32:46 +03:00
for file in "$@"; do
2022-01-05 08:32:46 +03:00
filepath="$(realpath "$file")"
if [ -n "$IMGSDEBUG" ]; then
echo "Uploading $filepath ..." | tee -a "$IMGSLOG"
2022-01-05 08:32:46 +03:00
curl -v -L -F "image=@/$filepath" "$IMGSREMOTE" 2>&1 | tee -a "$IMGSLOG"
else
curl -L -F "image=@/$filepath" "$IMGSREMOTE"
fi
done
[ -n "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Finished" | tee -a "$IMGSLOG"