From e5d665865e7cc800bb4deecd6aedcf3015e65b10 Mon Sep 17 00:00:00 2001 From: Deposite Pirate Date: Mon, 16 Feb 2026 02:22:00 +0100 Subject: Best effort dependency resolution Add a new check_install function that checks if a tool is installed and tries to install it on a best effort basic. Modify all scripts that benefit from it. Small indentation and correctness fixes. modified: functions/7za.fish modified: functions/alert.fish new file: functions/check_install.fish modified: functions/lscmd.fish modified: functions/rchmod.fish modified: functions/renlow.fish modified: functions/rlsxattr.fish modified: functions/rsdos.fish modified: functions/rsmac.fish modified: functions/rsxattr.fish modified: functions/uz.fish --- functions/7za.fish | 2 +- functions/alert.fish | 34 ++++++++++++++++++---------------- functions/check_install.fish | 27 +++++++++++++++++++++++++++ functions/lscmd.fish | 2 +- functions/rchmod.fish | 5 ----- functions/renlow.fish | 5 ----- functions/rlsxattr.fish | 14 ++------------ functions/rsdos.fish | 11 ++--------- functions/rsmac.fish | 7 +------ functions/rsxattr.fish | 9 ++------- functions/uz.fish | 3 +-- 11 files changed, 55 insertions(+), 64 deletions(-) create mode 100644 functions/check_install.fish diff --git a/functions/7za.fish b/functions/7za.fish index f7c5d76..8a5dbac 100644 --- a/functions/7za.fish +++ b/functions/7za.fish @@ -5,7 +5,7 @@ if test (count $argv) -lt 1 return 1 end -if not command -sq bsdtar +if not check_install 'bsdtar' 'libarchive' echo "Can't find bsdtar." return 1 end diff --git a/functions/alert.fish b/functions/alert.fish index 6ab53cb..a21deac 100644 --- a/functions/alert.fish +++ b/functions/alert.fish @@ -1,22 +1,24 @@ function alert --description 'Report status of command to desktop' - set -l cmd $argv - set -l cmd_str (string join ' ' $cmd) - set -l output (eval $cmd 2>&1) - set -l retval $status - if functions -q notify - if test $retval -eq 0 - notify "✅ Done: $cmd_str" "$output" - else - notify "❌ Failed $cmd_str" "$output" - end +set -l cmd $argv +set -l cmd_str (string join ' ' $cmd) +set -l output (eval $cmd 2>&1) +set -l retval $status + +if functions -q notify + if test $retval -eq 0 + notify "✅ Done: $cmd_str" "$output" + else + notify "❌ Failed $cmd_str" "$output" + end +else + if test $retval -eq 0 + printf "✅ Done: %s\n%s\n" "$cmd_str" "$output" else - if test $retval -eq 0 - printf "✅ Done: %s\n%s\n" "$cmd_str" "$output" - else - printf "❌ Failed: %s\n%s\n" "$cmd_str" "$output" - end + printf "❌ Failed: %s\n%s\n" "$cmd_str" "$output" end +end + +return $retval - return $retval end diff --git a/functions/check_install.fish b/functions/check_install.fish new file mode 100644 index 0000000..4c5da6c --- /dev/null +++ b/functions/check_install.fish @@ -0,0 +1,27 @@ +function check_install -a tool -a package + +if test (count $argv) -lt 1 + return 1 +end + +if command -sq $tool + return 0 +end + +if not ask "$tool doesn't seem to be installed. Attempt to install it?" + return 1 +end + +if test -z $package + set package $tool +end + +pkgadd $package || return 1 + +if not command -sq $tool + return 1 +end + +return 0 + +end diff --git a/functions/lscmd.fish b/functions/lscmd.fish index d59974d..0c9c34a 100644 --- a/functions/lscmd.fish +++ b/functions/lscmd.fish @@ -2,7 +2,7 @@ function lscmd --description "Show installed useful command line tools" set progsfile "$__fish_config_dir/progs.csv" -if not command -sq 'textql' +if not check_install 'textql' echo "Can't find textql." return 1 end diff --git a/functions/rchmod.fish b/functions/rchmod.fish index 63f64f0..70ddbc4 100644 --- a/functions/rchmod.fish +++ b/functions/rchmod.fish @@ -1,10 +1,5 @@ function rchmod --description 'Recursively reset perms of files and folders' -a dmode -a fmode -if not command -sq 'find' - echo "Can't find find." - return 1 -end - if test -z $dmode set dmode '0755' end diff --git a/functions/renlow.fish b/functions/renlow.fish index 98b0e4c..1581550 100644 --- a/functions/renlow.fish +++ b/functions/renlow.fish @@ -1,10 +1,5 @@ function renlow --description 'Rename all files and directories with lowercase characters' -if not command -sq 'find' - echo "Can't find find." - return 1 -end - for file in (find . -type f) set -l element (string split -r -m1 / $file) mv $file $element[1]/(string lower $element[2]) diff --git a/functions/rlsxattr.fish b/functions/rlsxattr.fish index 0e71e49..e52d820 100644 --- a/functions/rlsxattr.fish +++ b/functions/rlsxattr.fish @@ -1,20 +1,10 @@ function rlsxattr --description 'Recursively list all files with xattrs' -if not command -sq 'getfattr' +if not check_install 'getfattr' 'attr' echo "Can't find getfattr." return 1 end -if not command -sq 'sed' - echo "Can't find sed." - return 1 -end - -if not command -sq 'awk' - echo "Can't find awk." - return 1 -end - -getfattr -R -h -d . 2>/dev/null | grep '^# file:' | sed 's/^# file: //' +command getfattr -R -h -d . 2>/dev/null | grep '^# file:' | sed 's/^# file: //' end diff --git a/functions/rsdos.fish b/functions/rsdos.fish index 6efec55..eabe440 100644 --- a/functions/rsdos.fish +++ b/functions/rsdos.fish @@ -1,17 +1,10 @@ function rsdos --description 'Recursively delete all user.DOSATTRIB xattrs' -if not command -sq 'find' - echo "Can't find find." - return 1 -end - -if not command -sq 'setfattr' +if not check_install 'setfattr' 'attr' echo "Can't find setfattr." return 1 end -find . -exec setfattr -x user.DOSATTRIB {} + 2>/dev/null - -return 0 +command find . -exec setfattr -x user.DOSATTRIB {} + 2>/dev/null end diff --git a/functions/rsmac.fish b/functions/rsmac.fish index f0d3e49..826f7dc 100644 --- a/functions/rsmac.fish +++ b/functions/rsmac.fish @@ -1,10 +1,5 @@ function rsmac --description 'Recursively delete all .DS_Store files' -if not command -sq 'find' - echo "Can't find find." - return 1 -end - -find . -type f -name '.DS_Store' -delete +command find . -type f -name '.DS_Store' -delete end diff --git a/functions/rsxattr.fish b/functions/rsxattr.fish index 7003879..bd1456e 100644 --- a/functions/rsxattr.fish +++ b/functions/rsxattr.fish @@ -1,16 +1,11 @@ function rsxattr --description 'Recursively delete all user xattrs' -if not command -sq 'getfattr'; and command -sq 'setfattr' +if not check_install 'getfattr' 'attr' echo "Can't find xattr tools." return 1 end -if not command -sq 'sed' - echo "Can't find sed." - return 1 -end - -if not command -sq 'awk' +if not check_install 'awk' 'gawk' echo "Can't find awk." return 1 end diff --git a/functions/uz.fish b/functions/uz.fish index b23780f..1611157 100644 --- a/functions/uz.fish +++ b/functions/uz.fish @@ -5,7 +5,7 @@ if test (count $argv) -lt 1 return 1 end -if not command -sq bsdtar +if not check_install 'bsdtar' 'libarchive' echo "Can't fint bsdtar." return 1 end @@ -23,4 +23,3 @@ for archive in $argv end end - -- cgit v1.3.1