Added variable verbosity level
This commit is contained in:
parent
62a820de5c
commit
24b3371b5a
12
README.MD
12
README.MD
@ -14,8 +14,18 @@ Syncronises music from one folder to another.
|
||||
Options:
|
||||
-s, --source <source> The source 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
|
||||
|
||||
Log levels:
|
||||
0 | Verbose
|
||||
1 | Debug
|
||||
2 | Info (Default)
|
||||
3 | Warning
|
||||
4 | Error
|
||||
5 | Fatal
|
||||
6 | No logging
|
||||
|
||||
```
|
||||
|
||||
## Licence
|
||||
|
@ -4,7 +4,7 @@
|
||||
set -o errexit -o pipefail -o noclobber -o nounset
|
||||
source="-"
|
||||
dest="-"
|
||||
verbose=true
|
||||
verbose=2
|
||||
help=false
|
||||
|
||||
GetOptions() {
|
||||
@ -12,7 +12,7 @@ GetOptions() {
|
||||
# https://stackoverflow.com/a/29754866
|
||||
! getopt --test > /dev/null
|
||||
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 "If you're using mac or another unix-like system, please install GNU getopt"
|
||||
exit 1
|
||||
@ -22,8 +22,8 @@ GetOptions() {
|
||||
source=${*: -2:1}
|
||||
dest=${*: -1:1}
|
||||
|
||||
OPTIONS=s:d:vh
|
||||
LONGOPTS=source:,dest:,verbose,help
|
||||
OPTIONS=s:d:v::h
|
||||
LONGOPTS=source:,dest:,verbose::,help
|
||||
|
||||
# -use ! and PIPESTATUS to get exit code with errexit set
|
||||
# -temporarily store output to be able to check for errors
|
||||
@ -40,12 +40,15 @@ GetOptions() {
|
||||
# read getopt’s output this way to handle the quoting right:
|
||||
eval set -- "$PARSED"
|
||||
|
||||
verbose=false
|
||||
# now enjoy the options in order and nicely split until we see --
|
||||
while true; do
|
||||
case "$1" in
|
||||
-v|--verbose)
|
||||
verbose=true
|
||||
verbose=2
|
||||
if [[ $2 != "" ]]; then
|
||||
verbose=${2}
|
||||
shift
|
||||
fi
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
@ -87,14 +90,44 @@ Usage() {
|
||||
echo "Options:"
|
||||
echo " -s, --source <source> The source 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 "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 ""
|
||||
}
|
||||
|
||||
VerboseOutput() {
|
||||
if [[ "$verbose" = true ]]; then
|
||||
echo "[$1] $2" >&2
|
||||
level=""
|
||||
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
|
||||
}
|
||||
|
||||
@ -109,11 +142,11 @@ CreateFileList() {
|
||||
if [[ -d "${1}/$relfile" ]]; then
|
||||
newdir="${3}/$relfile"
|
||||
newdir=${newdir#"/"}
|
||||
VerboseOutput "Info" "Entering $newdir"
|
||||
VerboseOutput 1 "Entering $newdir"
|
||||
CreateFileList "${1}/$relfile" "${2}/$relfile" "$newdir"
|
||||
elif [[ ! -f "${2}/$relfile" || "${1}/$relfile" -nt "${2}/$relfile" ]]; then
|
||||
echo ${3}/$relfile >> /tmp/music-sync-filelist
|
||||
VerboseOutput "Info" "Added: ${3}/${relfile}"
|
||||
VerboseOutput 2 "Added: ${3}/${relfile}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
@ -122,15 +155,15 @@ ConvertFiles() {
|
||||
mkdir -p /tmp/converted
|
||||
while read -r line
|
||||
do
|
||||
VerboseOutput "Info" "Converting $line"
|
||||
VerboseOutput 1 "Converting: $line"
|
||||
if [[ "/tmp/converted/$line" = */* ]]; then
|
||||
mkdir -p "/tmp/converted/${line%/*}";
|
||||
fi;
|
||||
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
|
||||
VerboseOutput "Info" "Converted $line"
|
||||
VerboseOutput 2 "Converted: $line"
|
||||
else
|
||||
VerboseOutput "Warning" "$line already converted"
|
||||
VerboseOutput 3 "$line already converted"
|
||||
fi;
|
||||
|
||||
done < "/tmp/music-sync-filelist"
|
||||
@ -139,20 +172,20 @@ ConvertFiles() {
|
||||
CopyFiles() {
|
||||
while read -r line
|
||||
do
|
||||
VerboseOutput "Info" "Copy $line"
|
||||
VerboseOutput 1 "Copying: $line"
|
||||
if [[ "$dest/$line" = */* ]]; then
|
||||
mkdir -p "$dest/${line%/*}";
|
||||
fi;
|
||||
cp -f $source/$line $dest/$line 1>/dev/null 2>/dev/null
|
||||
done < "/tmp/music-sync-filelist"
|
||||
VerboseOutput "Info" "Copied $line"
|
||||
VerboseOutput 2 "Copied: $line"
|
||||
}
|
||||
|
||||
CleanUp() {
|
||||
VerboseOutput "Info" "Cleaning Up"
|
||||
VerboseOutput 1 "Cleaning Up"
|
||||
rm "/tmp/music-sync-filelist"
|
||||
rm -rf "/tmp/converted"
|
||||
VerboseOutput "Info" "Done"
|
||||
VerboseOutput 1 "Done"
|
||||
}
|
||||
|
||||
GetOptions $@
|
||||
|
Loading…
Reference in New Issue
Block a user