diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-05 22:39:31 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-05 22:39:31 +0300 |
| commit | 5ab5307714784b766b8e66821dbf498eaadf097c (patch) | |
| tree | 259746b8d7fd69559a9c58c6d5c0cdc097ca56b8 | |
| parent | 745785c43f46210d866c5892be51983743b84c43 (diff) | |
Extract ImageMagick command resolver
| -rwxr-xr-x | bin/photoalbum | 80 | ||||
| -rwxr-xr-x | src/photoalbum.sh | 80 |
2 files changed, 120 insertions, 40 deletions
diff --git a/bin/photoalbum b/bin/photoalbum index babacf9..77c24ee 100755 --- a/bin/photoalbum +++ b/bin/photoalbum @@ -246,36 +246,76 @@ init_config() { log_info "Created ./$rc_file" } +# shellcheck disable=SC2034 +resolve_imagemagick_command() { + local -r operation="$1"; shift + local -n command_ref="$1"; shift + + command_ref=() + + case "$operation" in + convert) + if command -v magick >/dev/null 2>&1; then + command_ref=(magick) + return + fi + if command -v convert >/dev/null 2>&1; then + command_ref=(convert) + return + fi + ;; + identify) + if command -v magick >/dev/null 2>&1; then + command_ref=(magick identify) + return + fi + if command -v identify >/dev/null 2>&1; then + command_ref=(identify) + return + fi + if command -v convert >/dev/null 2>&1; then + command_ref=(convert) + return + fi + ;; + *) + printf 'ERROR: Unknown ImageMagick operation %s\n' \ + "$operation" >&2 + return 1 + ;; + esac + + printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2 + return 127 +} + imagemagick() { - if command -v magick >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" magick "$@" - elif command -v convert >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" convert "$@" - else - printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2 - return 127 - fi + local -a imagemagick_command=() + + resolve_imagemagick_command convert imagemagick_command || return + + run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ + "${imagemagick_command[@]}" "$@" } imagemagick_identify() { - if command -v magick >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - magick identify "$@" - elif command -v identify >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - identify "$@" - elif command -v convert >/dev/null 2>&1; then + local -a imagemagick_command=() + + resolve_imagemagick_command identify imagemagick_command || return + + if [ "${imagemagick_command[0]}" = convert ]; then if [[ "${1:-}" = '-verbose' && $# -eq 2 ]]; then run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - convert "$2" -verbose info: + "${imagemagick_command[@]}" "$2" -verbose info: else run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - convert "$@" info: + "${imagemagick_command[@]}" "$@" info: fi - else - printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2 - return 127 + return fi + + run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ + "${imagemagick_command[@]}" "$@" } run_with_timeout() { diff --git a/src/photoalbum.sh b/src/photoalbum.sh index 2884ced..865fc45 100755 --- a/src/photoalbum.sh +++ b/src/photoalbum.sh @@ -246,36 +246,76 @@ init_config() { log_info "Created ./$rc_file" } +# shellcheck disable=SC2034 +resolve_imagemagick_command() { + local -r operation="$1"; shift + local -n command_ref="$1"; shift + + command_ref=() + + case "$operation" in + convert) + if command -v magick >/dev/null 2>&1; then + command_ref=(magick) + return + fi + if command -v convert >/dev/null 2>&1; then + command_ref=(convert) + return + fi + ;; + identify) + if command -v magick >/dev/null 2>&1; then + command_ref=(magick identify) + return + fi + if command -v identify >/dev/null 2>&1; then + command_ref=(identify) + return + fi + if command -v convert >/dev/null 2>&1; then + command_ref=(convert) + return + fi + ;; + *) + printf 'ERROR: Unknown ImageMagick operation %s\n' \ + "$operation" >&2 + return 1 + ;; + esac + + printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2 + return 127 +} + imagemagick() { - if command -v magick >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" magick "$@" - elif command -v convert >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" convert "$@" - else - printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2 - return 127 - fi + local -a imagemagick_command=() + + resolve_imagemagick_command convert imagemagick_command || return + + run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ + "${imagemagick_command[@]}" "$@" } imagemagick_identify() { - if command -v magick >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - magick identify "$@" - elif command -v identify >/dev/null 2>&1; then - run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - identify "$@" - elif command -v convert >/dev/null 2>&1; then + local -a imagemagick_command=() + + resolve_imagemagick_command identify imagemagick_command || return + + if [ "${imagemagick_command[0]}" = convert ]; then if [[ "${1:-}" = '-verbose' && $# -eq 2 ]]; then run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - convert "$2" -verbose info: + "${imagemagick_command[@]}" "$2" -verbose info: else run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ - convert "$@" info: + "${imagemagick_command[@]}" "$@" info: fi - else - printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2 - return 127 + return fi + + run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \ + "${imagemagick_command[@]}" "$@" } run_with_timeout() { |
