CLIEventListener.java
package org.docascode.cli;
import org.docascode.api.DocAsCode;
import org.docascode.api.core.errors.DocAsCodeException;
import org.docascode.api.event.Event;
import org.docascode.api.event.ProgressEvent;
import org.docascode.api.listener.EventListener;
import org.fusesource.jansi.Ansi;
import java.io.PrintStream;
import static org.fusesource.jansi.Ansi.Color.*;
public class CLIEventListener implements EventListener {
protected static PrintStream logger = System.out;
@Override
public void fireEvent(Event event) {
String msg;
if (event instanceof ProgressEvent){
ProgressEvent p = (ProgressEvent) event;
msg = String.format("%s: %3d%% (%s/%s)...\r",
p.getMessage(),
100*p.getProgress()/p.getTotal(),
p.getProgress(),
p.getTotal());
} else {
msg = String.format("%s%n",
event.getMessage());
}
switch (event.getLevel()){
case SUCESS:
success(msg);
break;
case INFO:
info(msg);
break;
case WARN:
warn(msg);
break;
case DEBUG:
debug(msg);
break;
default:
break;
}
}
protected void success(String msg){
logger .print(Ansi.ansi().fg(GREEN).a(msg).reset());
}
protected void info(String msg){
logger .print(Ansi.ansi().fg(BLUE).a(msg).reset());
}
protected void warn(String msg){
logger .print(Ansi.ansi().fg(YELLOW).a(msg).reset());
}
protected void debug(String msg) {
logger .print(Ansi.ansi().fg(MAGENTA).a(msg).reset());
}
protected void error(Exception e){
logger .println(
Ansi.ansi().fg(RED)
.a(String.format("Error. %s",
e.getMessage())).reset());
logger .println(
Ansi.ansi().fg(RED)
.a("You can submit an issue at 'https://gitlab.com/mtusseau/docascode/issues'.").reset());
logger .println(
Ansi.ansi().fg(RED)
.a("Please give the following logs:").reset());
try {
logger .println(
Ansi.ansi().fg(RED)
.a(String.format(
"%nBuild Number: '%s'",
DocAsCode.getBuildNumber())).reset());
} catch (DocAsCodeException er) {
error(er);
}
logger .println(
Ansi.ansi().fg(RED)
.a(e.getMessage()).reset());
for (StackTraceElement s : e.getStackTrace()){
logger .println(
Ansi.ansi().fg(RED)
.a(s.toString()).reset());
}
System.exit(1);
}
}