Skip to content

Instantly share code, notes, and snippets.

@adericbourg
Forked from katta/mvncolor.sh
Last active December 11, 2015 02:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adericbourg/4528735 to your computer and use it in GitHub Desktop.
Save adericbourg/4528735 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Formatting constants
export BOLD=`tput bold`
export UNDERLINE_ON=`tput smul`
export UNDERLINE_OFF=`tput rmul`
export TEXT_BLACK=`tput setaf 0`
export TEXT_RED=`tput setaf 1`
export TEXT_GREEN=`tput setaf 2`
export TEXT_YELLOW=`tput setaf 3`
export TEXT_BLUE=`tput setaf 4`
export TEXT_MAGENTA=`tput setaf 5`
export TEXT_CYAN=`tput setaf 6`
export TEXT_WHITE=`tput setaf 7`
export BACKGROUND_BLACK=`tput setab 0`
export BACKGROUND_RED=`tput setab 1`
export BACKGROUND_GREEN=`tput setab 2`
export BACKGROUND_YELLOW=`tput setab 3`
export BACKGROUND_BLUE=`tput setab 4`
export BACKGROUND_MAGENTA=`tput setab 5`
export BACKGROUND_CYAN=`tput setab 6`
export BACKGROUND_WHITE=`tput setab 7`
export RESET_FORMATTING=`tput sgr0`
# Wrapper function for Maven's mvn command.
mvncolor()
{
# Filter mvn output using sed
mvn $@ | sed -e "s/\(\[INFO\]\ \-.*\)/${TEXT_BLUE}${BOLD}\1/g" \
-e "s/\(\[INFO\]\ \[.*\)/${RESET_FORMATTING}${BOLD}\1${RESET_FORMATTING}/g" \
-e "s/\(\[INFO\]\ BUILD SUCCESSFUL\)/${BOLD}${TEXT_GREEN}\1${RESET_FORMATTING}/g" \
-e "s/\(\[WARNING\].*\)/${BOLD}${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
-e "s/\(\[ERROR\].*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}/g" \
-e "s/Tests run: \([^,]*\), Failures: \([^,]*\), Errors: \([^,]*\), Skipped: \([^,]*\)/${BOLD}${TEXT_GREEN}Tests run: \1${RESET_FORMATTING}, Failures: ${BOLD}${TEXT_RED}\2${RESET_FORMATTING}, Errors: ${BOLD}${TEXT_RED}\3${RESET_FORMATTING}, Skipped: ${BOLD}${TEXT_YELLOW}\4${RESET_FORMATTING}/g"
# Make sure formatting is reset
echo -ne ${RESET_FORMATTING}
}
# Override the mvn command with the colorized one.
alias mvn="mvncolor"
@twillouer
Copy link

ugly solution with notify-send and chaining of command (for mvn clean install && ..)

#!/bin/bash
TMPFILE=`mktemp`
ERRORLEVEL=1
(mvn $@ 2>&1 && echo 'ERRORLEVEL=0') | awk '
BEGIN { errors = 0; }
($1 == "[ALL]")     { print "\033[1;37m" $0 "\033[0m"; fflush() ; next; }
($1 == "[FATAL]")     { print "\033[1;31m" $0 "\033[0m"; errors=errors+1; fflush() ; next; }
($1 == "[ERROR]")     { print "\033[1;31m" $0 "\033[0m"; errors=errors+1; fflush() ; next; }
($1 == "[WARNING]")     { print "\033[1;33m" $0 "\033[0m"; fflush() ; next; }
($1 == "[INFO]")     { print "\033[1;38m" $0 "\033[0m"; fflush() ; next; }
($1 == "[DEBUG]")     { print "\033[1;36m" $0 "\033[0m"; fflush() ; next; }
($1 == "[TRACE]")     { print "\033[1;32m" $0 "\033[0m"; fflush() ; next; }
{ print ; fflush() }
END { print "Erreur : " errors}' | tee $TMPFILE
ERREUR=$(egrep "^Erreur :" $TMPFILE | cut -d':' -f2)
PWD=`pwd`
SUMMARY=$(echo "\nmvn $@\n\n$PWD")
TITRE="OK -- MAVEN -- OK"
if [ $ERREUR -gt 0 ]; then
        TITRE="ERREUR -- MAVEN -- ERREUR"
fi
notify-send "${TITRE}" "${SUMMARY}"
tail -2 ${TMPFILE} | grep 'ERRORLEVEL=0' > /dev/null && ERRORLEVEL=0
rm ${TMPFILE} 2>&1
exit $ERRORLEVEL

@adericbourg
Copy link
Author

Wondering if "$?" could be used...
http://tldp.org/LDP/abs/html/exit-status.html

@twillouer
Copy link

ERRORLEVEL=$? ?

@adericbourg
Copy link
Author

That's the idea.
I think I found something easier with Bash pipe management. Have to try ${PIPESTATUS[0]} and set -o pipefail

mvn  $@ | blabla
errorlevel = ${PIPESTATUS[0]}

or

set -o pipefail
mvn  $@ | blabla
errorlevel = $?

@adericbourg
Copy link
Author

Who, this code froze my system...

set -o pipefail

mvncolor()
{
  mvn $@ | blabla
  exit_status=$?

  blabla

  exit $exit_status
}

alias mvn="mvncolor"

mvn clean install # Freeze!

@twillouer
Copy link

#!/bin/bash
myecho()
{
  [ -x /usr/bin/notify-send ] && notify-send $*
  echo $*
}

mvncolor()
{
  TMPFILE=`mktemp`
  ERRORLEVEL=1
  mvn $@ 2>&1 | awk '
  BEGIN { errors = 0; }
  ($1 == "[ALL]")     { print "\033[1;37m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[FATAL]")     { print "\033[1;31m" $0 "\033[0m"; errors=errors+1; fflush() ; next; }
  ($1 == "[ERROR]")     { print "\033[1;31m" $0 "\033[0m"; errors=errors+1; fflush() ; next; }
  ($1 == "[WARNING]")     { print "\033[1;33m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[INFO]")     { print "\033[1;38m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[DEBUG]")     { print "\033[1;36m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[TRACE]")     { print "\033[1;32m" $0 "\033[0m"; fflush() ; next; }
  { print ; fflush() }
  END { print "Erreur : " errors}' | tee $TMPFILE
  ERRORLEVEL=${PIPESTATUS[0]}
  ERREUR=$(egrep "^Erreur :" $TMPFILE | cut -d':' -f2)
  PWD=`pwd`
  SUMMARY=$(echo "\nmvn $@\n\n$PWD")
  TITRE="OK -- MAVEN -- OK"
  if [ $ERREUR -gt 0 ]; then
        TITRE="ERREUR -- MAVEN -- ERREUR"
  fi
  myecho "${TITRE}" "${SUMMARY}"
  rm ${TMPFILE} 2>&1
  exit $ERRORLEVEL
}

works on ubuntu 12.10

@adericbourg
Copy link
Author

Just need to add a trap for term/exit (to reset formatting) and it should be ok. I'll try to do that tomorrow if I find some time.
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html

@twillouer
Copy link

#!/bin/bash
myecho()
{
  if [ -x /usr/bin/notify-send ];
  then
    notify-send "$1" "$2"
  else
    echo -e "$2"
  fi
}

mvncolor()
{
  trap 'echo -e "\033[0m" ; exit 255' SIGINT SIGTERM ERR
  TMPFILE=`mktemp`
  ERRORLEVEL=1
  mvn $@ 2>&1 | awk '
  BEGIN { errors = 0; }
  ($1 == "[ALL]")     { print "\033[1;37m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[FATAL]")     { print "\033[1;31m" $0 "\033[0m"; errors=errors+1; fflush() ; next; }
  ($1 == "[ERROR]")     { print "\033[1;31m" $0 "\033[0m"; errors=errors+1; fflush() ; next; }
  ($1 == "[WARNING]")     { print "\033[1;33m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[INFO]")     { print "\033[1;38m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[DEBUG]")     { print "\033[1;36m" $0 "\033[0m"; fflush() ; next; }
  ($1 == "[TRACE]")     { print "\033[1;32m" $0 "\033[0m"; fflush() ; next; }
  { print ; fflush() }
  END { print "Erreur : " errors}' | tee $TMPFILE
  ERRORLEVEL=${PIPESTATUS[0]}
  ERREUR=$(tail -1 $TMPFILE | egrep "^Erreur :" | cut -d':' -f2)
  PWD=`pwd`
  SUMMARY=$(echo "\nmvn $@\n\n$PWD")
  TITRE="OK -- MAVEN -- OK"
  if [ $ERREUR -gt 0 ]; then
        TITRE="ERREUR -- MAVEN -- ERREUR"
  fi
  myecho "${TITRE}" "${SUMMARY}"
  rm ${TMPFILE} 2>&1
  exit $ERRORLEVEL
}

mvncolor $*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment