-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactoring, complete MSP report implementation
feat: Add `fcli util msp-report` commands
- Loading branch information
Showing
41 changed files
with
1,016 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...common/src/main/java/com/fortify/cli/common/report/collector/IReportResultsCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
fcli-common/src/main/java/com/fortify/cli/common/report/logger/IReportLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.fortify.cli.common.report.logger; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
public interface IReportLogger { | ||
void warn(String msg, Object... args); | ||
void warn(String msg, Exception e, Object... args); | ||
void error(String msg, Object... args); | ||
void error(String msg, Exception e, Object... args); | ||
void updateSummary(ObjectNode summary); | ||
} |
93 changes: 93 additions & 0 deletions
93
fcli-common/src/main/java/com/fortify/cli/common/report/logger/ReportLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.fortify.cli.common.report.logger; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.time.LocalDateTime; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fortify.cli.common.json.JsonHelper; | ||
import com.fortify.cli.common.progress.helper.IProgressHelper; | ||
import com.fortify.cli.common.report.writer.IReportWriter; | ||
import com.fortify.cli.common.util.Counter; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
public final class ReportLogger implements IReportLogger { | ||
private final IProgressHelper progressHelper; | ||
private final BufferedWriter logWriter; | ||
private Counter errorCounter = new Counter(); | ||
private Counter warnCounter = new Counter(); | ||
|
||
public ReportLogger(IReportWriter reportWriter, IProgressHelper progressHelper) { | ||
this.logWriter = reportWriter.bufferedWriter("report.log"); | ||
this.progressHelper = progressHelper; | ||
} | ||
|
||
@Override | ||
public void updateSummary(ObjectNode summary) { | ||
summary.set("logCounts", | ||
JsonHelper.getObjectMapper().createObjectNode() | ||
.put("error", errorCounter.getCount()) | ||
.put("warn", warnCounter.getCount()) | ||
); | ||
} | ||
|
||
@Override | ||
public final void warn(String msg, Object... msgArgs) { | ||
write("WARN", warnCounter, msg, null, msgArgs); | ||
} | ||
|
||
@Override | ||
public final void warn(String msg, Exception e, Object... msgArgs) { | ||
write("WARN", warnCounter, msg, e, msgArgs); | ||
} | ||
|
||
@Override | ||
public final void error(String msg, Object... msgArgs) { | ||
write("ERROR", errorCounter, msg, null, msgArgs); | ||
} | ||
|
||
@Override | ||
public final void error(String msg, Exception e, Object... msgArgs) { | ||
// We want to fail completely in case of IOExceptions as this likely | ||
// means we cannot write the report, so we rethrow as ReportWriterIOException | ||
if ( e instanceof IOException ) { throw new ReportWriterIOException((IOException)e); } | ||
// Rethrow previously thrown ReportWriterIOException | ||
if ( e instanceof ReportWriterIOException ) { throw (ReportWriterIOException)e; } | ||
write("ERROR", errorCounter, msg, e, msgArgs); | ||
} | ||
|
||
@SneakyThrows | ||
private void write(String level, Counter counter, String msg, Exception e, Object[] msgArgs) { | ||
counter.increase(); | ||
var fullMsg = msgArgs==null ? msg : String.format(msg, msgArgs); | ||
if ( e!=null ) { | ||
fullMsg = String.format("%s: %s: %s", fullMsg, e.getClass().getSimpleName(), e.getMessage()); | ||
} | ||
progressHelper.writeWarning(String.format("%s: %s", level, fullMsg)); | ||
logWriter.append(String.format("[%s] %s %s\n", LocalDateTime.now(), level, fullMsg)); | ||
if ( e!=null ) { | ||
logWriter.append(String.format("%s\n", toString(e))); | ||
} | ||
} | ||
|
||
private String toString(Exception e) { | ||
try ( var sw = new StringWriter(); var pw = new PrintWriter(sw) ) { | ||
e.printStackTrace(pw); | ||
return sw.toString(); | ||
} catch ( IOException ioe ) { | ||
return e.getClass().getName()+": "+e.getMessage(); | ||
} | ||
} | ||
|
||
private static final class ReportWriterIOException extends IllegalStateException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public ReportWriterIOException(IOException cause) { | ||
super("Error writing report; report contents may be incomplete", cause); | ||
} | ||
} | ||
|
||
} |
7 changes: 0 additions & 7 deletions
7
...mon/src/main/java/com/fortify/cli/common/report/writer/entry/IReportErrorEntryWriter.java
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
...mmon/src/main/java/com/fortify/cli/common/report/writer/entry/ReportErrorEntryWriter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
fcli-common/src/main/java/com/fortify/cli/common/util/Counter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.fortify.cli.common.util; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@ToString @EqualsAndHashCode | ||
public class Counter { | ||
@Getter private long count = 0; | ||
|
||
public Counter increase() { | ||
count++; | ||
return this; | ||
} | ||
|
||
public Counter increase(long value) { | ||
count+=value; | ||
return this; | ||
} | ||
|
||
public Counter increase(Counter counter) { | ||
return increase(counter.getCount()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.