-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvoid-maketar
executable file
·252 lines (224 loc) · 7.22 KB
/
void-maketar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env bash
#shellcheck disable=SC2155,SC2034
#shellcheck source=/dev/null
# chili-maketar
# Description: Create tar of directories
#
# Created: 2023/07/28
# Altered: 2023/07/28
# Updated: 2024/12/25 - 03:42:08 -04 - Added gzip, xz, and normal tar support
#
# Copyright (c) 2023-2023, Vilmar Catafesta <vcatafesta@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#debug
export PS4='${red}${0##*/}${green}[$FUNCNAME]${pink}[$LINENO]${reset}'
#set -x
#set -e
shopt -s extglob
readonly APP="${0##*/}"
readonly _VERSION_="1.0.0-20230728"
readonly DEPENDENCIES=(tar tput sed pigz pzstd xz gzip)
function sh_diahora {
# DIAHORA=$(date +"%d%m%Y-%T" | sed 's/://g')
DIAHORA=$(date +"%d%m%Y-%H:%M" | sed 's/://g')
printf "%s\n" "$DIAHORA"
}
function sh_config {
declare -g TICK="${white}[${COL_LIGHT_GREEN}✓${COL_NC}${white}]"
declare -g CROSS="${white}[${COL_LIGHT_RED}✗${COL_NC}$white]"
declare -g BOOTLOG="/tmp/$APP-$(sh_diahora).log"
declare -g LOGGER='/dev/tty8'
declare -g quiet=false
declare -g use_color=true
declare -gi ncontador=1
declare -gi njobs=0
#declare -g compressor="normal" # Default compressor
declare -g compressor="pzstd" # Default compressor
sh_setvarcolors
}
function log_error {
printf "%30s:%-06d] : %s => %s\n" "$1" "$2" "$3 $4" >>"$BOOTLOG"
}
function cmdlogger {
local lastcmd="$@"
local line_number=${BASH_LINENO[0]}
local status
local error_output
local script_name="${0##*/}[${FUNCNAME[1]}]"
info_msg "Running '$*'"
# error_output=$( "$@" 2>&1 )
if $quiet; then
eval "$@" 2>&1 | tee -i -a "$BOOTLOG" >"$LOGGER"
else
eval "$@" | tee -i -a "$BOOTLOG"
fi
# status="${PIPESTATUS[0]}"
status="$?"
if [ $status -ne 0 ]; then
error_output=$(echo "$error_output" | cut -d':' -f3-)
log_error "$script_name" "$line_number" "$lastcmd" "$error_output"
fi
return $status
}
function sh_check_terminal {
if [ ! -t 1 ]; then
use_color=false
fi
}
function print_color {
if $use_color; then
echo -e "${@//\033\[*/}"
else
echo -e "$@"
fi
}
function sh_setvarcolors {
sh_check_terminal
if $use_color; then
bold=$(tput bold)
red=$(tput bold)$(tput setaf 196)
reset=$(tput sgr0)
pink=$(tput setaf 5)
yellow=$(tput bold)$(tput setaf 3)
green=$(tput bold)$(tput setaf 2)
cyan=$(tput setaf 6)
else
unset bold red reset pink yellow green cyan
fi
}
die() {
((++njobs))
printf "${red}$CROSS ${pink}%03d/%03d => ${red}FATAL: %s\n\033[m" "$ncontador" "$njobs" "$@"
((++ncontador))
exit 1
}
function info_msg() {
((++njobs))
printf "${green}$TICK ${pink}%03d/%03d => ${yellow}%s\n\033[m" "$ncontador" "$njobs" "$@"
((++ncontador))
}
function run_cmd {
info_msg "$APP: Running $*"
eval "$@"
}
function sh_checkDependencies() {
local d
local errorFound=false
declare -a missing
for d in "${DEPENDENCIES[@]}"; do
[[ -z $(command -v "$d") ]] && missing+=("$d") && errorFound=true &&
info_msg "${red}ERRO: não encontrei o comando${reset}: ${cyan}'$d'${reset}"
done
if $errorFound; then
echo "${yellow}---------------IMPOSSIBLE TO CONTINUE-------------${reset}"
echo "This script needs the commands listed above"
echo "Install them and/or make sure they are in your ${red}\$PATH${reset}"
echo "${yellow}---------------IMPOSSIBLE TO CONTINUE-------------${reset}"
die "Installation aborted!"
fi
}
function sh_main {
if [[ "$1" == '.' ]]; then
directory=$(basename "$(pwd)")
else
directory="${1%/}"
fi
local FILENAME=$directory-$(sh_diahora).tar
local error
local param='--verbose'
if $quiet; then
param=''
fi
case "$compressor" in
pigz)
FILENAME+=".gz"
cmdlogger tar -I pigz $param --posix --xattrs -chf "$FILENAME" "$1"
;;
pzstd)
FILENAME+=".zst"
cmdlogger tar -I pzstd $param --posix --xattrs -chf "$FILENAME" "$1"
;;
gzip)
FILENAME+=".gz"
cmdlogger tar -z $param --posix --xattrs -chf "$FILENAME" "$1"
;;
xz)
FILENAME+=".xz"
cmdlogger tar --xz $param --posix --xattrs -chf "$FILENAME" "$1"
;;
normal)
cmdlogger tar --posix $param --xattrs -chf "$FILENAME" "$1"
;;
esac
error=$?
if ((error)); then
die "$@"
else
info_msg "Successfully created: ${red}$FILENAME ${reset}"
fi
}
sh_config
sh_checkDependencies
#if [ "$(id -u)" -ne 0 ]; then
# die "$APP: need root perms to continue, exiting."
#fi
if test -w "$PWD"; then
info_msg "$USER com gravação permitida em $PWD, prosseguindo..."
else
info_msg "$USER sem permissão de gravação em $PWD"
if [ "$(id -u)" -ne 0 ]; then
die "$APP: need root perms to continue, exiting."
fi
fi
# Display usage information if no arguments are passed
if [[ $# -eq 0 ]]; then
cat <<-EOF
Usage: $APP [options] <directory>
Options:
-q Quiet mode (suppress progress messages)
-n Disable colors in the output
-z Use pigz for compression
-s Use pzstd for compression ${cyan}(default)${reset}
-g Use gzip for compression
-x Use xz for compression
(normal) Create tar archive without additional compression
EOF
exit 1
fi
# Process command line options using getopts
while getopts "qnsxzg" opt; do
case $opt in
q) quiet=true;info_msg 'Param -q detected, disabling verbose' ;; # Quiet mode
n) use_color=false;sh_setvarcolors;info_msg 'Param -n detected, disabling colors';; # Disable color
z) compressor="pigz";info_msg "Param -z detected, compressor is now '$compressor'";; # Use pigz for compression
s) compressor="pzstd";info_msg "Param -z detected, compressor is now '$compressor'" ;; # Use pzstd for compression
g) compressor="gzip";info_msg "Param -z detected, compressor is now '$compressor'" ;; # Use gzip for compression
x) compressor="xz";info_msg "Param -z detected, compressor is now '$compressor'" ;; # Use xz for compression
\?) die "Invalid option: -$OPTARG";;
esac
done
# Process the remaining argument (directory)
shift $((OPTIND-1))
# Check if the directory argument is missing or invalid
[[ -z "$1" || ! -d "$1" ]] && die "${cyan}argument directory ${yellow}'$1' ${reset}is either missing or does not exist!"
sh_main "$1"