| 1 | #!/usr/bin/env fish |
| 2 | |
| 3 | # tt -- a tool for transfering files and folders over text |
| 4 | |
| 5 | # requirements: |
| 6 | # fish, tar, zstd, brotli, base64 |
| 7 | |
| 8 | function err |
| 9 | echo -- $argv >&2 |
| 10 | end |
| 11 | |
| 12 | switch $argv[1] |
| 13 | case s send |
| 14 | if test (count $argv[2..-1]) -eq 0 |
| 15 | err "tt: no files or directories given" |
| 16 | exit 1 |
| 17 | end |
| 18 | for f in $argv[2..-1] |
| 19 | test -e "$f"; or set -a missing "$f" |
| 20 | end |
| 21 | if set -q missing |
| 22 | for p in $missing |
| 23 | err "tt: '$p' was not found" |
| 24 | end |
| 25 | exit 1 |
| 26 | else |
| 27 | for p in $argv[2..-1] |
| 28 | set -a args -C (path resolve (path dirname $p)) (path basename $p) |
| 29 | end |
| 30 | |
| 31 | set zstd (tar -cI 'zstd -19' $args | base64 -w0) |
| 32 | set brotli (tar -cI 'brotli -9' $args | base64 -w0) |
| 33 | if test (string length -- $zstd) -le (string length -- $brotli) |
| 34 | echo z:$zstd |
| 35 | else |
| 36 | echo b:$brotli |
| 37 | end |
| 38 | end |
| 39 | case r receive |
| 40 | read -P "string: " string |
| 41 | |
| 42 | if test -z "$string" |
| 43 | err "tt: empty strings aren't allowed" |
| 44 | exit 1 |
| 45 | end |
| 46 | |
| 47 | set used_algo (string match -rg "^([zb]):" -- $string) |
| 48 | if test $used_algo = "z" |
| 49 | set algo zstd |
| 50 | else if test $used_algo = "b" |
| 51 | set algo brotli |
| 52 | else |
| 53 | err "tt: wrong or missing prefix" |
| 54 | exit 1 |
| 55 | end |
| 56 | |
| 57 | set string (string match -rg ":(.*)" -- $string) |
| 58 | echo -- $string | base64 -d 2>/dev/null | tar -xvoI "$algo" 2>/dev/null |
| 59 | set ps -l $pipestatus |
| 60 | if test $ps[2] -ne 0 |
| 61 | err "tt: invalid base64 string" |
| 62 | exit 1 |
| 63 | else if test $ps[3] -ne 0 |
| 64 | err "tt: archive is corrupted or invalid" |
| 65 | exit 1 |
| 66 | end |
| 67 | case '*' |
| 68 | echo "usage: tt <s|r> [file|dir ...]" |
| 69 | echo |
| 70 | echo " s, send <file|dir> ... " |
| 71 | echo " r, receive" |
| 72 | end |
| 73 |
a tool for transfering files and folders over text