Better argument handling, also add a --force flag to delete all ssh keys

This commit is contained in:
Mylloon 2023-10-08 20:41:30 +02:00
parent 15446733e4
commit f8e6b745b3
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,9 +1,17 @@
function ssh-import function ssh-import
# Declare our arguments
argparse h/help f/force -- $argv
or return
# Get function name
set current_name $(status current-function)
# Check if no arguments were provided, or if the first argument is '--help' # Check if no arguments were provided, or if the first argument is '--help'
if test -z $argv[1] || test "$argv[1]" = --help if test -z $argv[1] || set -ql _flag_help
echo -e "Usage: ssh-import <archive.zip>" echo -e "Usage: $current_name <archive.zip>"
echo -e " ssh-import --help \t - Show this help message" echo -e " $current_name [-h|--help] \t\t- Show this help message"
echo -e " ssh-import <archive.zip> - Erase **all** the current .ssh folder and replace it with the archive content" echo -e " $current_name <archive.zip> \t\t- Update the current .ssh folder with the archive content"
echo -e " $current_name [-f|--force] <archive.zip> - Erase **all** the current .ssh folder and replace it with the archive content"
return 0 return 0
end end
@ -11,20 +19,27 @@ function ssh-import
set file "$argv[1]" set file "$argv[1]"
if test (count $argv) -ge 2 if test (count $argv) -ge 2
# Check if usage is respected # Check if usage is respected
echo "Too much arguments." 1>&2 echo "$current_name: too much arguments." 1>&2
return 1 return 1
else if not test -e "$file" else if not test -e "$file"
# Check if file exists # Check if file exists
echo "File doesn't exists." 1>&2 echo "$current_name: file doesn't exists." 1>&2
return 1 return 1
else if not test $(string sub --start=-3 "$file") = zip else if not test $(string sub --start=-3 "$file") = zip
# Check if file have .zip extension # Check if file have .zip extension
echo "File isn't a zip archive." 1>&2 echo "$current_name: file isn't a zip archive." 1>&2
return 1 return 1
end end
set ssh_directory "$HOME/.ssh"
# Delete the .ssh directory
if set -ql _flag_force
find $ssh_directory -type f -not -name known_hosts -not -name environment -delete
end
# Update and overwrite the .ssh directory with the archive content # Update and overwrite the .ssh directory with the archive content
# Using modification date, and new files are added # Using modification date, and new files are added
# Files removed aren't tracked # Files removed aren't tracked
unzip -uo "$file" -d "$HOME/.ssh" unzip -uo "$file" -d $ssh_directory
end end