Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
fix: Better error message when server not configured
Browse files Browse the repository at this point in the history
The old message misleadingly claimed that an offline scan would
commence. New message instead provides a hint on how to enable it.
  • Loading branch information
dividedmind committed Mar 15, 2022
1 parent 93d57c5 commit e9c7c35
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/cli/ci/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default {

const configData = await parseConfigFile(config);

const scanner = buildScanner(false, configData, files);
const scanner = await buildScanner(false, configData, files);

const [rawScanResults, findingStatuses]: [ScanResults, FindingStatusListItem[]] =
await Promise.all([scanner.scan(), scanner.fetchFindingStatus(appIdArg, appmapDir)]);
Expand Down
6 changes: 5 additions & 1 deletion src/cli/scan/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export default {

const configData = await parseConfigFile(config);

const scanner = buildScanner(reportAllFindings, configData, files);
const scanner = await buildScanner(reportAllFindings, configData, files).catch(
(error: Error) => {
throw new ValidationError(error.message + '\nUse --all to perform an offline scan.');
}
);

const startTime = Date.now();

Expand Down
35 changes: 9 additions & 26 deletions src/cli/scan/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,36 @@ import resolveAppId from '../resolveAppId';
import scan from '../scan';
import { ScanResults } from '../../report/scanResults';

interface Scanner {
export interface Scanner {
scan(): Promise<ScanResults>;

fetchFindingStatus(appId?: string, appMapDir?: string): Promise<FindingStatusListItem[]>;
}

export default function scanner(
export default async function scanner(
reportAllFindings: boolean,
configuration: Configuration,
files: string[]
): Scanner {
return reportAllFindings
? new StandaloneScanner(configuration, files)
: new ServerIntegratedScanner(configuration, files);
): Promise<Scanner> {
if (reportAllFindings) {
return new StandaloneScanner(configuration, files);
} else {
await loadConfiguration();
return new ServerIntegratedScanner(configuration, files);
}
}

abstract class ScannerBase {
constructor(public configuration: Configuration, public files: string[]) {}

async scan(): Promise<ScanResults> {
await this.verifyServerConfiguration();

const checks = await loadConfig(this.configuration);
const { appMapMetadata, findings } = await scan(this.files, checks);
return new ScanResults(this.configuration, appMapMetadata, findings, checks);
}

protected abstract verifyServerConfiguration(): Promise<boolean>;
}

class ServerIntegratedScanner extends ScannerBase implements Scanner {
async verifyServerConfiguration(): Promise<boolean> {
return new Promise((resolve) => {
loadConfiguration()
.then(() => resolve(true))
.catch((err) => {
console.warn(`⚠️ Notice ⚠️`);
console.warn(`⚠️ AppMap Server configuration is not available.`);
console.warn(`⚠️ Detailed message: ${err.toString()}`);
console.warn(
`⚠️ Scanning will continue without fetching existing findings from the server.`
);
resolve(false);
});
});
}

async fetchFindingStatus(
appIdArg?: string,
appMapDir?: string
Expand Down

0 comments on commit e9c7c35

Please sign in to comment.