funcsave: delete files for non-existant functions, output actions unless -q

This allows for:

$ functions -e somefunc
$ funcsave somefunc

to work.
This commit is contained in:
Aaron Gyes 2021-09-28 21:13:35 -07:00
parent 69074c1591
commit 3a402b19b3

View File

@ -1,5 +1,5 @@
function funcsave --description "Save the current definition of all specified functions to file"
set -l options h/help 'd/directory='
set -l options h/help q/quiet d/directory=
argparse -n funcsave $options -- $argv
or return
@ -15,21 +15,26 @@ function funcsave --description "Save the current definition of all specified fu
end
if not set -q argv[1]
printf (_ "%ls: Expected at least %d args, got only %d\n") funcsave 1 0
printf (_ "%ls: Expected at least %d args, got only %d\n") funcsave 1 0 >&2
return 1
end
if not mkdir -p $funcdir
printf (_ "%s: Could not create configuration directory '%s'\n") funcsave $funcdir
printf (_ "%s: Could not create configuration directory '%s'\n") funcsave $funcdir >&2
return 1
end
set -l retval 0
for funcname in $argv
set -l funcpath "$funcdir/$funcname.fish"
if functions -q -- $funcname
functions --no-details -- $funcname >$funcdir/$funcname.fish
functions --no-details -- $funcname >$funcpath
and set -q _flag_quiet || printf (_ "%s: wrote %s") funcsave $funcpath
else if [ -w $funcpath ]
rm $funcpath
and set -q _flag_quiet || printf (_ "%s: removed %s") funcsave $funcpath
else
printf (_ "%s: Unknown function '%s'\n") funcsave $funcname
printf (_ "%s: Unknown function '%s'\n") funcsave $funcname >&2
set retval 1
end
end