summaryrefslogtreecommitdiffstats
path: root/.local/bin/compress
diff options
context:
space:
mode:
authorPavel Egorov <[email protected]>2025-06-14 02:01:20 +0300
committerPavel Egorov <[email protected]>2025-06-14 02:01:20 +0300
commit5fcb2bed9e3840d46da75afa7ea6db46e8868472 (patch)
tree631123ea7f9d036192879c4f58ab93723f98bea3 /.local/bin/compress
parent4d79423f7b1bab635c879586a92ace0476827177 (diff)
downloaddotfiles-5fcb2bed9e3840d46da75afa7ea6db46e8868472.tar.gz
dotfiles-5fcb2bed9e3840d46da75afa7ea6db46e8868472.zip
[script] Add compress script
Diffstat (limited to '.local/bin/compress')
-rwxr-xr-x.local/bin/compress96
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
3set archiver_formats tar
4set compressor_formats bz2 gz lz4 xz zstd
5set arch_and_comp_formats 7z rar zip
6set formats $archiver_formats $compressor_formats $arch_and_comp_formats
7
8set options 'h/help' 'n/name=' 't/timestamp' 'f/format=!validate_format'
9
10function validate_format
11 if not contains $_flag_value $formats
12 echo "$_flag_value archive is not supported" >&2
13 return 1
14 end
15end
16
17function 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
33end
34
35function create_tarball
36 tar --force-local -cvf $tarball $files
37end
38
39# Script main code
40
41argparse $options -- $argv
42or return 1
43
44if set -q _flag_help; or test (count $argv) -eq 0
45 print_help
46 return
47end
48
49if not set -q _flag_name
50 set _flag_name archive
51end
52
53if set -q _flag_timestamp
54 set _flag_name "$_flag_name@$(date -Iseconds)"
55end
56
57if not set -q _flag_format
58 echo "No format specified, using zip" >&2
59 set _flag_format zip
60end
61
62set tarball "$_flag_name.tar"
63
64set files $argv
65
66if 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
74end
75
76switch $_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
95end
96