Added variable verbosity level

This commit is contained in:
Jeroen De Meerleer 2018-11-30 16:12:50 +01:00
parent 62a820de5c
commit 24b3371b5a
2 changed files with 62 additions and 19 deletions

View File

@ -14,8 +14,18 @@ Syncronises music from one folder to another.
Options: Options:
-s, --source <source> The source folder of the music -s, --source <source> The source folder of the music
-d, --dest <destination> The destionation folder of the music -d, --dest <destination> The destionation folder of the music
-v, --verbose Enable verbose output -v, --verbose <0-6> Set log level
-h, --help Display this help text -h, --help Display this help text
Log levels:
0 | Verbose
1 | Debug
2 | Info (Default)
3 | Warning
4 | Error
5 | Fatal
6 | No logging
``` ```
## Licence ## Licence

View File

@ -4,7 +4,7 @@
set -o errexit -o pipefail -o noclobber -o nounset set -o errexit -o pipefail -o noclobber -o nounset
source="-" source="-"
dest="-" dest="-"
verbose=true verbose=2
help=false help=false
GetOptions() { GetOptions() {
@ -12,7 +12,7 @@ GetOptions() {
# https://stackoverflow.com/a/29754866 # https://stackoverflow.com/a/29754866
! getopt --test > /dev/null ! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
VerboseOutput "Fatal" "\`getopt --test\` failed" VerboseOutput 5 "\`getopt --test\` failed"
echo "Sorry, It seems that your shell is not supported" echo "Sorry, It seems that your shell is not supported"
echo "If you're using mac or another unix-like system, please install GNU getopt" echo "If you're using mac or another unix-like system, please install GNU getopt"
exit 1 exit 1
@ -22,8 +22,8 @@ GetOptions() {
source=${*: -2:1} source=${*: -2:1}
dest=${*: -1:1} dest=${*: -1:1}
OPTIONS=s:d:vh OPTIONS=s:d:v::h
LONGOPTS=source:,dest:,verbose,help LONGOPTS=source:,dest:,verbose::,help
# -use ! and PIPESTATUS to get exit code with errexit set # -use ! and PIPESTATUS to get exit code with errexit set
# -temporarily store output to be able to check for errors # -temporarily store output to be able to check for errors
@ -40,12 +40,15 @@ GetOptions() {
# read getopts output this way to handle the quoting right: # read getopts output this way to handle the quoting right:
eval set -- "$PARSED" eval set -- "$PARSED"
verbose=false
# now enjoy the options in order and nicely split until we see -- # now enjoy the options in order and nicely split until we see --
while true; do while true; do
case "$1" in case "$1" in
-v|--verbose) -v|--verbose)
verbose=true verbose=2
if [[ $2 != "" ]]; then
verbose=${2}
shift
fi
shift shift
;; ;;
-h|--help) -h|--help)
@ -87,14 +90,44 @@ Usage() {
echo "Options:" echo "Options:"
echo " -s, --source <source> The source folder of the music" echo " -s, --source <source> The source folder of the music"
echo " -d, --dest <destination> The destionation folder of the music" echo " -d, --dest <destination> The destionation folder of the music"
echo " -v, --verbose Enable verbose output" echo " -v, --verbose <0-6> Set log level"
echo " -h, --help Display this help text" echo " -h, --help Display this help text"
echo "Log levels:"
echo " 0 | Verbose"
echo " 1 | Debug"
echo " 2 | Info (Default)"
echo " 3 | Warning"
echo " 4 | Error"
echo " 5 | Fatal"
echo " 6 | No logging"
echo "" echo ""
} }
VerboseOutput() { VerboseOutput() {
if [[ "$verbose" = true ]]; then level=""
echo "[$1] $2" >&2 if [[ $verbose -le $1 ]]; then
case "$1" in
0)
level="Verbose"
;;
1)
level=" Debug "
;;
2)
level=" Info "
;;
3)
level="Warning"
;;
4)
level=" Error "
;;
5)
level=" Fatal "
;;
esac
echo "[$level] $2" >&2
fi fi
} }
@ -109,11 +142,11 @@ CreateFileList() {
if [[ -d "${1}/$relfile" ]]; then if [[ -d "${1}/$relfile" ]]; then
newdir="${3}/$relfile" newdir="${3}/$relfile"
newdir=${newdir#"/"} newdir=${newdir#"/"}
VerboseOutput "Info" "Entering $newdir" VerboseOutput 1 "Entering $newdir"
CreateFileList "${1}/$relfile" "${2}/$relfile" "$newdir" CreateFileList "${1}/$relfile" "${2}/$relfile" "$newdir"
elif [[ ! -f "${2}/$relfile" || "${1}/$relfile" -nt "${2}/$relfile" ]]; then elif [[ ! -f "${2}/$relfile" || "${1}/$relfile" -nt "${2}/$relfile" ]]; then
echo ${3}/$relfile >> /tmp/music-sync-filelist echo ${3}/$relfile >> /tmp/music-sync-filelist
VerboseOutput "Info" "Added: ${3}/${relfile}" VerboseOutput 2 "Added: ${3}/${relfile}"
fi fi
done done
} }
@ -122,15 +155,15 @@ ConvertFiles() {
mkdir -p /tmp/converted mkdir -p /tmp/converted
while read -r line while read -r line
do do
VerboseOutput "Info" "Converting $line" VerboseOutput 1 "Converting: $line"
if [[ "/tmp/converted/$line" = */* ]]; then if [[ "/tmp/converted/$line" = */* ]]; then
mkdir -p "/tmp/converted/${line%/*}"; mkdir -p "/tmp/converted/${line%/*}";
fi; fi;
if [[ ! -f "/tmp/converted/$line" || "${source}/$file" -nt "/tmp/converted/$line" ]]; then if [[ ! -f "/tmp/converted/$line" || "${source}/$file" -nt "/tmp/converted/$line" ]]; then
lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null lame -b 192 $source/$line /tmp/converted/$line 1>/dev/null 2>/dev/null
VerboseOutput "Info" "Converted $line" VerboseOutput 2 "Converted: $line"
else else
VerboseOutput "Warning" "$line already converted" VerboseOutput 3 "$line already converted"
fi; fi;
done < "/tmp/music-sync-filelist" done < "/tmp/music-sync-filelist"
@ -139,20 +172,20 @@ ConvertFiles() {
CopyFiles() { CopyFiles() {
while read -r line while read -r line
do do
VerboseOutput "Info" "Copy $line" VerboseOutput 1 "Copying: $line"
if [[ "$dest/$line" = */* ]]; then if [[ "$dest/$line" = */* ]]; then
mkdir -p "$dest/${line%/*}"; mkdir -p "$dest/${line%/*}";
fi; fi;
cp -f $source/$line $dest/$line 1>/dev/null 2>/dev/null cp -f $source/$line $dest/$line 1>/dev/null 2>/dev/null
done < "/tmp/music-sync-filelist" done < "/tmp/music-sync-filelist"
VerboseOutput "Info" "Copied $line" VerboseOutput 2 "Copied: $line"
} }
CleanUp() { CleanUp() {
VerboseOutput "Info" "Cleaning Up" VerboseOutput 1 "Cleaning Up"
rm "/tmp/music-sync-filelist" rm "/tmp/music-sync-filelist"
rm -rf "/tmp/converted" rm -rf "/tmp/converted"
VerboseOutput "Info" "Done" VerboseOutput 1 "Done"
} }
GetOptions $@ GetOptions $@