diff options
Diffstat (limited to '.local/bin')
| -rwxr-xr-x | .local/bin/compress | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/.local/bin/compress b/.local/bin/compress new file mode 100755 index 0000000..4054008 --- /dev/null +++ b/.local/bin/compress | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | #!/usr/bin/env fish | ||
| 2 | |||
| 3 | set archiver_formats tar | ||
| 4 | set compressor_formats bz2 gz lz4 xz zstd | ||
| 5 | set arch_and_comp_formats 7z rar zip | ||
| 6 | set formats $archiver_formats $compressor_formats $arch_and_comp_formats | ||
| 7 | |||
| 8 | set options 'h/help' 'n/name=' 't/timestamp' 'f/format=!validate_format' | ||
| 9 | |||
| 10 | function validate_format | ||
| 11 | if not contains $_flag_value $formats | ||
| 12 | echo "$_flag_value archive is not supported" >&2 | ||
| 13 | return 1 | ||
| 14 | end | ||
| 15 | end | ||
| 16 | |||
| 17 | function print_help | ||
| 18 | echo -es "Archive and compressor wrapper\n" \ | ||
| 19 | "Supported formats: $formats" >&2 | ||
| 20 | echo "Usage: compress" \ | ||
| 21 | "[-h | --help]" \ | ||
| 22 | "[-n | --name archive_name]" \ | ||
| 23 | "[-t | --timestamp]" \ | ||
| 24 | "[-f | --format archive_format]" \ | ||
| 25 | "file1 [file2, file3, ...]" >&2 | ||
| 26 | echo -es \ | ||
| 27 | "-h, --help\t\tPrint this help\n" \ | ||
| 28 | "-n, --name\t\tSpecify archive name,\n" \ | ||
| 29 | "\t\t\tif archive name is not provided, default name will be used; \n" \ | ||
| 30 | "\t\t\tif $(string join '/' $compressor_formats) format used on single file, name will be ignored\n" \ | ||
| 31 | "-t, --timestamp\t\tAdd timestamp to filename\n" \ | ||
| 32 | "-f, --format\t\tSpecify archive format, zip is default" >&2 | ||
| 33 | end | ||
| 34 | |||
| 35 | function create_tarball | ||
| 36 | tar --force-local -cvf $tarball $files | ||
| 37 | end | ||
| 38 | |||
| 39 | # Script main code | ||
| 40 | |||
| 41 | argparse $options -- $argv | ||
| 42 | or return 1 | ||
| 43 | |||
| 44 | if set -q _flag_help; or test (count $argv) -eq 0 | ||
| 45 | print_help | ||
| 46 | return | ||
| 47 | end | ||
| 48 | |||
| 49 | if not set -q _flag_name | ||
| 50 | set _flag_name archive | ||
| 51 | end | ||
| 52 | |||
| 53 | if set -q _flag_timestamp | ||
| 54 | set _flag_name "$_flag_name@$(date -Iseconds)" | ||
| 55 | end | ||
| 56 | |||
| 57 | if not set -q _flag_format | ||
| 58 | echo "No format specified, using zip" >&2 | ||
| 59 | set _flag_format zip | ||
| 60 | end | ||
| 61 | |||
| 62 | set tarball "$_flag_name.tar" | ||
| 63 | |||
| 64 | set files $argv | ||
| 65 | |||
| 66 | if contains $_flag_format $compressor_formats | ||
| 67 | if test (count $files) -gt 1 | ||
| 68 | echo "Too many files, packing with tar first" >&2 | ||
| 69 | create_tarball | ||
| 70 | set _flag_name $tarball | ||
| 71 | else | ||
| 72 | set _flag_name $files | ||
| 73 | end | ||
| 74 | end | ||
| 75 | |||
| 76 | switch $_flag_format | ||
| 77 | case tar | ||
| 78 | create_tarball | ||
| 79 | case bz2 | ||
| 80 | bzip2 $_flag_name | ||
| 81 | case gz | ||
| 82 | gzip $_flag_name | ||
| 83 | case lz4 | ||
| 84 | lz4 $_flag_name | ||
| 85 | case xz | ||
| 86 | xz $_flag_name | ||
| 87 | case zstd | ||
| 88 | zstd $_flag_name | ||
| 89 | case 7z | ||
| 90 | 7z a "$_flag_name" $files | ||
| 91 | case rar | ||
| 92 | rar a "$_flag_name" $files | ||
| 93 | case zip | ||
| 94 | zip "$_flag_name" $files | ||
| 95 | end | ||
| 96 | |||
