replaced all logging statements with log function, resolved misconception with and argument handling

This commit is contained in:
sometimesuseful 2024-11-12 15:22:28 +00:00
parent f363327a34
commit 834ba85b30

View file

@ -15,17 +15,17 @@ cleanup() {
check_input() {
if [[ ! -e "${input_file}" ]]; then
echo "ERROR: Input file '${input_file}' does not exist"
log "ERROR" "Input file '${input_file}' does not exist"
exit 2
elif [[ -z "${archive_pwd}" ]]; then
echo "ERROR: No password given"
log "ERROR" "No password given"
exit 2
elif [[ ! "$size" =~ ^([1-9][0-9]*)?$ ]]; then
echo "Invalid size $size, must be empty or a positive number"
log "ERROR" "Invalid size $size, must be empty or a positive number"
exit 2
fi
echo "Archiving file/directory '${input_file}' with password '${archive_pwd}' and size '$size'" >&2
log "INFO" "Archiving file/directory '${input_file}' with password '${archive_pwd}' and size '$size'" >&2
}
# Calculates a "smart" volume size so that each volume (including the final part) will be approximately the same size
@ -38,25 +38,25 @@ calc_smart_volumes() {
num_volumes=$(( (total_size + size - 1) / size ))
size=$(( (total_size + num_volumes - 1) / num_volumes ))
echo "Calculated smart volume size '$size'" >&2
log "INFO" "Calculated smart volume size '$size'" >&2
}
set_volumes() {
[[ -n "$size" ]] && calc_smart_volumes && volumes="-v${size}m"
echo "Volumes set to '$volumes'" >&2
log "INFO" "Volumes set to '$volumes'" >&2
}
# Generates a random archive name of 20 alphanumeric characters
generate_name() {
archive="$archive_dir/$(tr -dc [:alnum:] < /dev/urandom | head -c 20).7z"
echo "Generated archive name '$archive'" >&2
log "INFO" "Generated archive name '$archive'" >&2
}
create_archive() {
echo "Packing '${input_file}' into archive '$archive' with password '${archive_pwd}'"
[[ -n "$volumes" ]] && echo "and volumes of size $size MB"
log "INFO" "Packing '${input_file}' into archive '$archive' with password '${archive_pwd}'"
[[ -n "$volumes" ]] && log "INFO" "Volumes size is set to $size MB"
7z a -mhe=on "-p${archive_pwd}" "$volumes" "$archive" "${input_file}" >&2
}
@ -80,12 +80,4 @@ main() {
create_archive
}
# Missing arguments must be caught here when using `set -e`, otherwise
# the script will exit prematurely without logging the error.
if (( $# == 3 )); then
main "$@"
else
log "ERROR" "Missing arguments"
exit 2
fi
main "$@"