From 1d6b20161d8098f2e8cd5dcad48170ab17bb8eac Mon Sep 17 00:00:00 2001 From: Salt Date: Fri, 19 Oct 2018 21:05:08 -0500 Subject: [PATCH] Add severity level to log function --- replicate.sh | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/replicate.sh b/replicate.sh index 27ecc77..41f1bfa 100755 --- a/replicate.sh +++ b/replicate.sh @@ -9,7 +9,12 @@ ## Helper functions log() { [ -z ${1+x} ] && return 1 - printf "\e[94m${name}\e[0m: $1\n" + out=1 + [ -z ${2+x} ] || out="$2" + col_message="\e[39m" + [ "$out" -gt "1" ] && col_message="\e[31m" + [ "$out" -lt "1" ] && out=1 && col_message="\e[37m" + printf "\e[94m${name}\e[0m: $col_message$1\e[0m\n" >&${out} } validatedep() { if ! which $1 > /dev/null 2>&1; then @@ -45,7 +50,7 @@ step_validate_deps() { log "Validating dependencies" for dep in $deps; do if ! validatedep "$dep"; then - log "Could not find critical dependency \"$dep\"" + log "Could not find critical dependency \"$dep\"" 2 return 1 fi done @@ -54,7 +59,7 @@ step_make_skeleton() { # Build Home folder skeleton log "Building skeleton folder layout" if ! mkdir Desktop Documents Downloads Games Music Pictures Projects Public ROMs System Templates Videos > /dev/null 2>&1; then - log "Failed to build skeleton layout" + log "Failed to build skeleton layout" 2 return 1 fi return 0 @@ -65,7 +70,7 @@ step_repo_clone() { git clone --recursive --depth 100 --separate-git-dir="$gitdir" "$repo" "$tmpdir" >> "$logfile" 2>&1 error="$?" if ! [ "$error" -eq "0" ]; then - log "Failed cloning repository \"$repo\": git returned $error" + log "Failed cloning repository \"$repo\": git returned $error" 2 log "See $logfile for details" log "Do you have the appropriate permissions? Does the remote host know your key?" return 1 @@ -75,7 +80,7 @@ step_repo_move() { # Move them to where they should be log "Moving cloned repo from \"$tmpdir\" into \"$bootstrapdir\"" if ! rsync -rvl --exclude ".git" "$tmpdir/" "$bootstrapdir/" >> "$logfile"; then - log "Failed to move from temp directory to bootstrap directory" + log "Failed to move from temp directory to bootstrap directory" 2 return 1 fi } @@ -100,21 +105,21 @@ main() { case $opt in d) if [ "$OPTARG" == "" ]; then - log "Option -d requires an argument" + log "Option -d requires an argument" 2 exit 1 fi bootstrapdir="$OPTARG" ;; r) if [ "$OPTARG" == "" ]; then - log "Option -r requires an argument" + log "Option -r requires an argument" 2 exit 1 fi repo="$OPTARG" ;; l) if [ "$OPTARG" == "" ]; then - log "Option -l requires an argument" + log "Option -l requires an argument" 2 exit 1 fi logfile="$OPTARG" @@ -123,7 +128,7 @@ main() { help ;; *) - log "Invalid option: \"$opt\"" + log "Invalid option: \"$opt\"" 2 exit 1 ;; esac