Skip to content

Commit

Permalink
@W-14689342@: (Part 9) Final polish
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-carter-at-sf committed Jan 4, 2024
1 parent 5d920d1 commit 1bd1c02
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/lib/output/OutfileResultsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {OutputFormat} from "./OutputFormat";
import {writeToFile} from "./OutputUtils";
import {FormattedOutput} from "../../types";

/**
* Processes results to produce an output file
*/
export class OutfileResultsProcessor implements ResultsProcessor {
private readonly outputFormat: OutputFormat;
private readonly outfile: string;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/output/ResultsProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {Results} from "./Results";

/**
* Interface to process run results
*/
export interface ResultsProcessor {
processResults(results: Results): Promise<void>;
}

/**
* A composite results processor
*/
export class CompositeResultsProcessor implements ResultsProcessor {
private readonly delegates: ResultsProcessor[];

Expand Down
15 changes: 11 additions & 4 deletions src/lib/output/ResultsProcessorFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@ import {ENV_VAR_NAMES} from "../../Constants";
import {inferFormatFromInternalOutfile, OutputFormat} from "./OutputFormat";
import {OutfileResultsProcessor} from "./OutfileResultsProcessor";

/**
* Interface for creating a ResultsProcessor
*/
export interface ResultsProcessorFactory {
createResultsProcessor(display: Display, runOutputOptions: RunOutputOptions,
jsonReturnValueHolder: JsonReturnValueHolder): ResultsProcessor
}

/**
* Runtime implementation of the ResultsProcessorFactory interface
*/
export class ResultsProcessorFactoryImpl implements ResultsProcessorFactory {
public createResultsProcessor(display: Display, runOutputOptions: RunOutputOptions,
jsonReturnValueHolder: JsonReturnValueHolder): ResultsProcessor {

const resultsProcessors: ResultsProcessor[] = [new RunResultsProcessor(display, runOutputOptions, jsonReturnValueHolder)];
this.addProcessorForInternalOutfileIfNeeded(resultsProcessors, runOutputOptions.verboseViolations);
return new CompositeResultsProcessor(resultsProcessors);
}

private addProcessorForInternalOutfileIfNeeded(resultsProcessors: ResultsProcessor[], verboseViolations: boolean): void {
const internalOutfile: string = process.env[ENV_VAR_NAMES.SCANNER_INTERNAL_OUTFILE];
if (internalOutfile && internalOutfile.length > 0) {
const internalOutputFormat: OutputFormat = inferFormatFromInternalOutfile(internalOutfile);
resultsProcessors.push(new OutfileResultsProcessor(internalOutputFormat, internalOutfile, runOutputOptions.verboseViolations));
resultsProcessors.push(new OutfileResultsProcessor(internalOutputFormat, internalOutfile, verboseViolations));
}

return new CompositeResultsProcessor(resultsProcessors);
}
}
14 changes: 11 additions & 3 deletions src/lib/output/RunResultsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ export type RunOutputOptions = {
outfile?: string;
}

// TODO: We should consider separating this into multiple ResultProcessor classes:
// --> 1 for processing the json return value, 1 for creating the users outfile, and 1 for creating console output
/**
* The primary ResultsProcessor used in production
*
* Note: We should consider separating this into multiple ResultProcessor classes to separate the responsibilities:
* --> creating the json return value
* --> creating the users outfile (where we can reuse the OutfileResultsProcessor)
* --> creating console output
* --> checking and throwing an exception if over severity threshold
* Right now all of these are entangled with one another below and a design change would be needed to do this.
*/
export class RunResultsProcessor implements ResultsProcessor {
private readonly display: Display;
private readonly opts: RunOutputOptions;
private readonly jsonReturnValueHolder: JsonReturnValueHolder;
private jsonReturnValueHolder: JsonReturnValueHolder;

public constructor(display: Display, opts: RunOutputOptions, jsonReturnValueHolder: JsonReturnValueHolder) {
this.display = display;
Expand Down

0 comments on commit 1bd1c02

Please sign in to comment.