feat: Improve and make scripts POSIX compliant

This commit is contained in:
ge 2022-09-29 10:47:48 +03:00
parent 921dd5d29d
commit 71e1d87d32
2 changed files with 72 additions and 78 deletions

View File

@ -1,38 +1,32 @@
#!/usr/bin/env bash #!/bin/sh
# This is an imgs <https://gitea.gch.icu/ge/imgs> "integration" for Nautilus. # This is an imgs https://git.nxhs.cloud/ge/imgs "integration" for Nautilus.
# Place this script into path: $HOME/.local/share/nautilus/scripts # Place this script into path: $HOME/.local/share/nautilus/scripts
# See more info at: <https://help.ubuntu.com/community/NautilusScriptsHowto> # See more info at: <https://help.ubuntu.com/community/NautilusScriptsHowto>
imgs_check_vars() { IMGSLOG="${IMGSLOG:-$HOME/imgs_debug.log}"
[ "$IMGSLOG" ] || IMGSLOG=$HOME/imgs_debug.log [ -n "$IMGSREMOTE" ] && return 0 # exit from func if variable is set
[ "$IMGSREMOTE" ] && return 0 # exit from func if variable is set
if [ -f "$HOME/.imgsremote" ]; then if [ -f "$HOME"/.imgsremote ]; then
. $HOME/.imgsremote # shellcheck source=/dev/null
. "$HOME"/.imgsremote
fi fi
if [ "$IMGSREMOTE" ]; then if [ -z "$IMGSREMOTE" ]; then
: echo "$0: Error: IMGSREMOTE variable is not set." >&2; exit 1
else
echo "$0: Error: IMGSREMOTE variable is not set." >&2
exit 1
fi fi
}
imgs_check_vars [ -n "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Started" >> "$IMGSLOG"
[ "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Started" >> "$IMGSLOG" echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read -r file; do
[ -z "$file" ] && break
while read -r file; do if [ -n "$IMGSDEBUG" ]; then
[ "$file" ] || break
if [ "$IMGSDEBUG" ]; then
image="$(curl -v -L -F "image=@$file" "$IMGSREMOTE" 2>&1 | tee -a "$IMGSLOG")" image="$(curl -v -L -F "image=@$file" "$IMGSREMOTE" 2>&1 | tee -a "$IMGSLOG")"
image="$(tail -n 1 <<< "$image")" image="$(echo "$image" | tail -n 1)"
else else
image="$(curl -L -F "image=@$file" "$IMGSREMOTE")" image="$(curl -L -F "image=@$file" "$IMGSREMOTE")"
fi fi
[ "$IMGSDEBUG" ] && echo "$(date +"[%d %b %Y %H:%M:%S]") $file --> $image" >> "$IMGSLOG" [ -n "$IMGSDEBUG" ] && echo "$(date +"[%d %b %Y %H:%M:%S]") $file --> $image" >> "$IMGSLOG"
notify-send "File uploaded to imgs!" "$image" notify-send "File uploaded to imgs!" "$image"
done <<< "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" done
[ "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Finished" >> "$IMGSLOG" [ -n "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Finished" >> "$IMGSLOG"

88
imgs
View File

@ -1,18 +1,16 @@
#!/usr/bin/env bash #!/bin/sh
# imgs CLI client. # imgs CLI https://git.nxhs.cloud/ge/imgs
# Home page: <https://gitea.gch.icu/ge/imgs>
imgs_usage() { imgs_usage() {
cat <<- EOF cat <<- EOF
Upload images to remote imgs server. Upload images to remote imgs server.
Usage: imgs [--version] [--help] [-r | --remote <URL>] <file>... Usage: imgs [-rvh] <file>...
Options: Options:
-r, --remote remote imgs instance URI. Example: -r, --remote remote imgs instance URI e.g. https://user:password@example.org
'https://user:password@example.org' -v, --version print version and exit.
--version print version and exit. -h, --help print this help message and exit.
--help print this help message and exit.
Environment variables: Environment variables:
IMGSREMOTE remote imgs instance URI. IMGSREMOTE remote imgs instance URI.
@ -20,55 +18,57 @@ imgs_usage() {
IMGSLOG path to logfile. Default: ~/imgs_debug.log IMGSLOG path to logfile. Default: ~/imgs_debug.log
You can set variables in ~/.imgsremote file instead of ~/.bashrc You can set variables in ~/.imgsremote file instead of ~/.bashrc
See <https://gitea.gch.icu/ge/imgs> for more info. See <https://git.nxhs.cloud/ge/imgs> for more info.
EOF EOF
exit 0
} }
imgs_check_vars() { [ "$#" -eq 0 ] && { imgs_usage; exit 1; }
[ "$IMGSLOG" ] || IMGSLOG=$HOME/imgs_debug.log
[ "$IMGSREMOTE" ] && return 0 # exit from func if variable is set
if [ -f "$HOME/.imgsremote" ]; then # Transform long options to short ones
. $HOME/.imgsremote for arg in "$@"; do
fi shift
case "$arg" in
if [ "$IMGSREMOTE" ]; then --remote) set -- "$@" "-r";;
: --help) set -- "$@" "-h";;
else --version) set -- "$@" "-v";;
echo "$0: Error: IMGSREMOTE variable is not set." >&2 *) set -- "$@" "$arg";;
exit 1
fi
}
[[ "$@" ]] || imgs_usage
while (( "$#" )); do
case "$1" in
-r|--remote) if [ "$2" ] && [ "${2:0:1}" != '-' ]; then
IMGSREMOTE="$2"; shift
else
echo "$0: missing argument for $1" >&2; exit 1
fi; shift;;
--version) echo 'imgs CLI 1.0'; exit 0;;
--help) imgs_usage;;
-*) echo "$0: $1: bad option" >&2; exit 1;;
*) [ -f "$1" ] || { echo "$0: $1: no such file" >&2; exit 1; }
_files+=("$1"); shift;;
esac esac
done done
imgs_check_vars 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;;
esac
done
[ "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Started" | tee -a "$IMGSLOG" shift $((OPTIND - 1)) # shift for parse positional args
for file in "${_files[@]}"; do # 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
[ -n "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Started" | tee -a "$IMGSLOG"
for file in "$@"; do
filepath="$(realpath "$file")" filepath="$(realpath "$file")"
if [ "$IMGSDEBUG" ]; then if [ -n "$IMGSDEBUG" ]; then
echo "Uploading $filepath" | tee -a "$IMGSLOG" echo "Uploading $filepath ..." | tee -a "$IMGSLOG"
curl -v -L -F "image=@/$filepath" "$IMGSREMOTE" 2>&1 | tee -a "$IMGSLOG" curl -v -L -F "image=@/$filepath" "$IMGSREMOTE" 2>&1 | tee -a "$IMGSLOG"
else else
curl -L -F "image=@/$filepath" "$IMGSREMOTE" curl -L -F "image=@/$filepath" "$IMGSREMOTE"
fi fi
done done
[ "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Finished" | tee -a "$IMGSLOG" [ -n "$IMGSDEBUG" ] && date +"[%d %b %Y %H:%M:%S] Finished" | tee -a "$IMGSLOG"