Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds exit codes for detections/errors and machine-readable CSV output with an option #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed errors, improved a bit.
  • Loading branch information
Valiec committed Jun 11, 2023
commit 4087d9b6d607708fc5d79fa87a4d3b4fed14cb74
2 changes: 1 addition & 1 deletion src/main/java/me/cortex/jarscanner/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void createAndDisplayGui() {
};

Results run = Main.run(4, searchDir, true, logOutput);
Main.outputRunResults(run, logOutput);
Main.outputRunResults(run, logOutput, null);
textArea.append("Done scanning!");
} catch (Exception ex) {
if (ex instanceof InterruptedException || ex instanceof RejectedExecutionException) {
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/me/cortex/jarscanner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public static void main(String[] args) {
return outputString;
};

// Create CSV logging function
Function<String, String> logCSV = null;

//make this not null if CSV output requested
if(machineReadableOutput) {
logCSV = outputString -> {
System.err.println(outputString);
return outputString;
};
}

// Output scan start
logOutput.apply("Starting scan...");

Expand All @@ -70,22 +81,26 @@ public static void main(String[] args) {
} catch (IOException e) {
logOutput.apply("An error occurred while scanning the directory: " + dirToCheck);
e.printStackTrace();
System.exit(1); //exit code for actual errors, distinct from detections
} catch (InterruptedException e) {
logOutput.apply("An error occurred while waiting for the scan to complete.");
e.printStackTrace();
System.exit(1);
}

// Output scan completion and results
outputRunResults(results, logOutput);
outputRunResults(results, logOutput, logCSV);
}

/**
* Output the results of a scan to the specified log output function.
*
* @param results the resulting from {@link #run(int, Path, boolean, Function)}.
* @param logOutput the function to use for logging output
* @param logOutput the function to use for logging human-readable output
* @param logCSV the function to use for logging machine-readable CSV output, pass null for no CSV output
*
*/
public static void outputRunResults(Results results, Function<String, String> logOutput) {
public static void outputRunResults(Results results, Function<String, String> logOutput, Function<String, String> logCSV) {
if (results == null) {
logOutput.apply("Scan failed. Unable to display results.");
} else {
Expand All @@ -111,13 +126,18 @@ public static void outputRunResults(Results results, Function<String, String> lo
logOutput.apply(Constants.ANSI_RED + "[" + stage2InfectionNumber + "] " + Constants.ANSI_WHITE + stage2Infection + Constants.ANSI_RESET);
}
}
if(machineReadableOutput)
if(logCSV != null) //if CSV output isn't wanted, just pass in null
{
//printing to stderr instead so this output can be used by a program while the full human-readable logs go to a file
System.err.println("stage1,stage2");
System.err.println(stage1Detections.size()+","+stage2Detections.size());
for (int i = 0; i < stage1Detections.size(); i++) {
String stage1Infection = stage1Detections.get(i);
logCSV.apply("1," + stage1Infection);
}
for (int i = 0; i < stage2Detections.size(); i++) {
String stage2Infection = stage2Detections.get(i);
logCSV.apply("2," + stage2Infection);
}
}
System.exit(1); //nonzero exit code to indicate infections found
System.exit(2); //nonzero exit code to indicate infections found
}
}
}
Expand Down