From 492f712df281e96d6d1aa9ebb53b1304284b19a0 Mon Sep 17 00:00:00 2001 From: Konfekt Date: Tue, 5 Jan 2021 18:53:29 +0100 Subject: feat(plugins): add `universalarchive` plugin to conveniently compress files (#6846) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cornellà --- .../universalarchive/universalarchive.plugin.zsh | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 plugins/universalarchive/universalarchive.plugin.zsh (limited to 'plugins/universalarchive/universalarchive.plugin.zsh') diff --git a/plugins/universalarchive/universalarchive.plugin.zsh b/plugins/universalarchive/universalarchive.plugin.zsh new file mode 100644 index 000000000..5a661a5b0 --- /dev/null +++ b/plugins/universalarchive/universalarchive.plugin.zsh @@ -0,0 +1,70 @@ +function ua() { + local usage=\ +"Archive files and directories using a given compression algorithm. + +Usage: $0 +Example: $0 tbz PKGBUILD + +Supported archive formats are: +7z, bz2, gz, lzma, lzo, rar, tar, tbz (tar.bz2), tgz (tar.gz), +tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst." + + if [[ $# -lt 2 ]]; then + echo >&2 "$usage" + return 1 + fi + + local ext="$1" + local input="$(realpath "$2")" + + shift + + if [[ ! -e "$input" ]]; then + echo >&2 "$input not found" + return 1 + fi + + # generate output file name + local output + if [[ $# -gt 1 ]]; then + output="$(basename "${input%/*}")" + elif [[ -f "$input" ]]; then + output="$(basename "${input%.[^.]*}")" + elif [[ -d "$input" ]]; then + output="$(basename "${input}")" + fi + + # if output file exists, generate a random name + if [[ -f "${output}.${ext}" ]]; then + output=$(mktemp "${output}_XXX") && rm "$output" || return 1 + fi + + # add extension + output="${output}.${ext}" + + # safety check + if [[ -f "$output" ]]; then + echo >&2 "output file '$output' already exists. Aborting" + return 1 + fi + + case "$ext" in + 7z) 7z u "${output}" "${@}" ;; + bz2) bzip2 -vcf "${@}" > "${output}" ;; + gz) gzip -vcf "${@}" > "${output}" ;; + lzma) lzma -vc -T0 "${@}" > "${output}" ;; + lzo) lzop -vc "${@}" > "${output}" ;; + rar) rar a "${output}" "${@}" ;; + tar) tar -cvf "${output}" "${@}" ;; + tbz|tar.bz2) tar -cvjf "${output}" "${@}" ;; + tgz|tar.gz) tar -cvzf "${output}" "${@}" ;; + tlz|tar.lzma) XZ_OPT=-T0 tar --lzma -cvf "${output}" "${@}" ;; + txz|tar.xz) XZ_OPT=-T0 tar -cvJf "${output}" "${@}" ;; + tZ|tar.Z) tar -cvZf "${output}" "${@}" ;; + xz) xz -vc -T0 "${@}" > "${output}" ;; + Z) compress -vcf "${@}" > "${output}" ;; + zip) zip -rull "${output}" "${@}" ;; + zst) zstd -c -T0 "${@}" > "${output}" ;; + *) echo >&2 "$usage"; return 1 ;; + esac +} -- cgit v1.2.3-70-g09d2 From 3635d7cf7e300b5ba746c384fc3e87a35561e62d Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sun, 10 Jan 2021 17:22:52 +0100 Subject: fix(universalarchive): make plugin zsh-only to fix `realpath` not found error Fixes #9586 --- plugins/universalarchive/universalarchive.plugin.zsh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'plugins/universalarchive/universalarchive.plugin.zsh') diff --git a/plugins/universalarchive/universalarchive.plugin.zsh b/plugins/universalarchive/universalarchive.plugin.zsh index 5a661a5b0..b287c22b2 100644 --- a/plugins/universalarchive/universalarchive.plugin.zsh +++ b/plugins/universalarchive/universalarchive.plugin.zsh @@ -10,28 +10,28 @@ Supported archive formats are: tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst." if [[ $# -lt 2 ]]; then - echo >&2 "$usage" + print -u2 -- "$usage" return 1 fi local ext="$1" - local input="$(realpath "$2")" + local input="${2:a}" shift if [[ ! -e "$input" ]]; then - echo >&2 "$input not found" + print -u2 -- "$input not found" return 1 fi # generate output file name local output if [[ $# -gt 1 ]]; then - output="$(basename "${input%/*}")" + output="${input:h:t}" elif [[ -f "$input" ]]; then - output="$(basename "${input%.[^.]*}")" + output="${input:r:t}" elif [[ -d "$input" ]]; then - output="$(basename "${input}")" + output="${input:t}" fi # if output file exists, generate a random name @@ -44,7 +44,7 @@ tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst." # safety check if [[ -f "$output" ]]; then - echo >&2 "output file '$output' already exists. Aborting" + print -u2 -- "output file '$output' already exists. Aborting" return 1 fi @@ -65,6 +65,6 @@ tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst." Z) compress -vcf "${@}" > "${output}" ;; zip) zip -rull "${output}" "${@}" ;; zst) zstd -c -T0 "${@}" > "${output}" ;; - *) echo >&2 "$usage"; return 1 ;; + *) print -u2 -- "$usage"; return 1 ;; esac } -- cgit v1.2.3-70-g09d2