2023-10-09 17:56:28 +02:00
|
|
|
function ssh-import --description "Import SSH keys and config from zip archive."
|
2023-10-08 20:41:30 +02:00
|
|
|
# Declare our arguments
|
|
|
|
argparse h/help f/force -- $argv
|
|
|
|
or return
|
|
|
|
|
|
|
|
# Get function name
|
|
|
|
set current_name $(status current-function)
|
|
|
|
|
2023-10-08 20:25:32 +02:00
|
|
|
# Check if no arguments were provided, or if the first argument is '--help'
|
2023-10-08 20:41:30 +02:00
|
|
|
if test -z $argv[1] || set -ql _flag_help
|
|
|
|
echo -e "Usage: $current_name <archive.zip>"
|
|
|
|
echo -e " $current_name [-h|--help] \t\t- Show this help message"
|
|
|
|
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"
|
2023-10-08 20:25:32 +02:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2023-10-08 20:59:51 +02:00
|
|
|
set archive_file "$argv[1]"
|
2023-10-08 20:25:32 +02:00
|
|
|
if test (count $argv) -ge 2
|
|
|
|
# Check if usage is respected
|
2023-10-08 20:41:30 +02:00
|
|
|
echo "$current_name: too much arguments." 1>&2
|
2023-10-08 20:25:32 +02:00
|
|
|
return 1
|
2023-10-08 20:59:51 +02:00
|
|
|
else if not test -e "$archive_file"
|
2023-10-08 20:25:32 +02:00
|
|
|
# Check if file exists
|
2023-10-08 20:41:30 +02:00
|
|
|
echo "$current_name: file doesn't exists." 1>&2
|
2023-10-08 20:25:32 +02:00
|
|
|
return 1
|
2023-10-08 20:59:51 +02:00
|
|
|
else if not test $(string sub --start=-3 "$archive_file") = zip
|
2023-10-08 20:25:32 +02:00
|
|
|
# Check if file have .zip extension
|
2023-10-08 20:41:30 +02:00
|
|
|
echo "$current_name: file isn't a zip archive." 1>&2
|
2023-10-08 20:25:32 +02:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2023-10-08 20:41:30 +02:00
|
|
|
set ssh_directory "$HOME/.ssh"
|
|
|
|
|
|
|
|
if set -ql _flag_force
|
2023-10-08 20:51:38 +02:00
|
|
|
# Delete the .ssh directory
|
2023-10-08 20:41:30 +02:00
|
|
|
find $ssh_directory -type f -not -name known_hosts -not -name environment -delete
|
|
|
|
end
|
|
|
|
|
2023-10-08 20:25:32 +02:00
|
|
|
# Update and overwrite the .ssh directory with the archive content
|
|
|
|
# Using modification date, and new files are added
|
|
|
|
# Files removed aren't tracked
|
2023-10-08 20:59:51 +02:00
|
|
|
unzip -uo "$archive_file" -d $ssh_directory
|
|
|
|
|
|
|
|
# Ask user if he want to delete the archive now that it has been imported
|
2023-10-08 21:13:56 +02:00
|
|
|
while read --nchars 1 -l response --prompt-str="Wanna delete the archive? (y/n) "
|
2023-10-08 20:59:51 +02:00
|
|
|
or return 1 # if the read was aborted with ctrl-c/ctrl-d
|
|
|
|
switch $response
|
|
|
|
case y Y
|
|
|
|
rm $archive_file
|
|
|
|
echo "$current_name: archive deleted."
|
|
|
|
break
|
|
|
|
case n N
|
|
|
|
break
|
|
|
|
case '*'
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
end
|
2023-10-08 20:25:32 +02:00
|
|
|
end
|