From 13cdcc1ca3c0e1b11f013ea9a62326e7e6e836c3 Mon Sep 17 00:00:00 2001 From: Florian Kaiser Date: Wed, 16 Oct 2019 19:10:26 +0200 Subject: [PATCH] Add zip compression method (fix #2) --- Dockerfile | 2 +- README.md | 4 ++-- ghr-compress | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 98d5e8d..9fca491 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ LABEL "com.github.actions.color"="black" ENV GHR_FORK tcnksm/ghr ENV GHR_VERSION 0.12.0 -RUN apk add --no-cache bash curl xz +RUN apk add --no-cache bash curl xz zip RUN sh -c "curl --silent -L https://github.com/${GHR_FORK}/releases/download/v${GHR_VERSION}/ghr_v${GHR_VERSION}_linux_amd64.tar.gz > ghr_v${GHR_VERSION}_linux_amd64.tar.gz" && \ sh -c "tar xvzf ghr_v${GHR_VERSION}_linux_amd64.tar.gz" && \ diff --git a/README.md b/README.md index 4f6ca03..a259474 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ The action will trigger on pushes to tags and exit neutrally otherwise. - `GHR_COMPRESS` — **Optional.** Compress files before uploading them. - Can be either `gz`, `bz2` or `xz`. - The correct file extension will be appended (e.g. `.tar.gz`). + Can be either `gz`, `bz2`, `xz`, or `zip`. + The correct file extension will be appended (e.g. `.tar.gz`, or `.zip`). ## Usage example diff --git a/ghr-compress b/ghr-compress index 37252e7..24a4ee4 100755 --- a/ghr-compress +++ b/ghr-compress @@ -1,24 +1,41 @@ -#!/bin/bash +#!/usr/bin/env bash set -e -u -o pipefail shopt -s nullglob -extension="$GHR_COMPRESS" - -if [ "$extension" = "gz" ]; then - option=z -elif [ "$extension" = "bz2" ]; then - option=j -elif [ "$extension" = "xz" ]; then - option=J +if [ "$GHR_COMPRESS" = "gz" ]; then + tar_option=z + extension=".tar.gz" +elif [ "$GHR_COMPRESS" = "bz2" ]; then + tar_option=j + extension=".tar.bz2" +elif [ "$GHR_COMPRESS" = "xz" ]; then + tar_option=J + extension=".tar.xz" +elif [ "$GHR_COMPRESS" = "zip" ]; then + extension=".zip" else echo "ghr-compress: error: unknown extension: ${extension}" >&2 exit 1 fi +ghr-compress-one-tar() { + local file="$1" + tar c "-${tar_option}" -f "${file}${extension}" "$file" +} + +ghr-compress-one-zip() { + local file="$1" + zip -r "${file}${extension}" "$file" +} + ghr-compress-one() { local file="$1" - /bin/tar c "-${option}" -f "${file}.tar.${extension}" "$file" - /bin/rm -r -- "$file" + if [ "$GHR_COMPRESS" = "zip" ]; then + ghr-compress-one-zip "$file" >&2 + else + ghr-compress-one-tar "$file" + fi + rm -r -- "$file" } if [ "$GHR_PATH" = "." ]; then @@ -27,5 +44,5 @@ if [ "$GHR_PATH" = "." ]; then done else ghr-compress-one "$GHR_PATH" - echo "export GHR_PATH=$(printf '%q' "${GHR_PATH}.tar.${extension}")" + echo "export GHR_PATH=$(printf '%q' "${GHR_PATH}${extension}")" fi