function rsync_find -d "Find & download files from remote server using a regex pattern"
argparse h/help 'r/remote=' 'd/directory=' -- $argv
or begin
echo "Usage: rsync_find [OPTIONS] PATTERN"
echo "Options:"
echo " -r/--remote Remote host (name) from SSH config (default: rainbow)"
echo " -d/--directory Directory to search in on remote host (default: /oxide/workspace)"
echo " -h/--help Show this help message"
echo "Arguments:"
echo " PATTERN File pattern to search for (e.g., *.txt or *-rss.toml)"
return 1
end
if set -ql _flag_help
echo "Usage: rsync_find [OPTIONS] PATTERN"
echo "Options:"
echo " -r/--remote Remote host (name) from SSH config (default: rainbow)"
echo " -d/--directory Directory to search in on remote host (default: /oxide/workspace)"
echo " -h/--help Show this help message"
echo "Arguments:"
echo " PATTERN File pattern to search for (e.g., *.txt or *-rss.toml)"
return 0
end
set -l search_dir (test -n "$_flag_directory"; and echo $_flag_directory; or echo /oxide/workspace)
set -l remote_host (test -n "$_flag_remote"; and echo $_flag_remote; or echo rainbow)
if test (count $argv) -eq 0
echo "Error: Search pattern is required"
return 1
end
set -l pattern $argv[1]
echo "Searching for files matching '$pattern' in $search_dir on $remote_host..."
set -l find_output (ssh $remote_host "find $search_dir -type f -name \"$pattern\" 2>/dev/null")
set -l find_status $status
if test $find_status -ne 0
echo "Error: Failed to search for files on remote host"
return 1
end
if test -n "$find_output"
echo "Found the following files:"
printf "%s\n" $find_output
echo "Downloading to current directory..."
for remote_file in $find_output
echo "Downloading $remote_file..."
rsync -avz $remote_host:$remote_file ./
end
else
echo "No matching files found in $search_dir on $remote_host"
return 1
end
end