Skip to content

Commit

Permalink
fix(docker): try/catch parsing + do not early return
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Nov 7, 2023
1 parent 940924d commit b258437
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/analyser/__snapshots__/index.test.ts.snap
Expand Up @@ -746,7 +746,7 @@ exports[`analyser > should run correctly 1`] = `
"AMPL": 1,
"HCL": 1,
"JSON": 1,
"YAML": 2,
"YAML": 3,
},
"name": "fake",
"path": [
Expand All @@ -759,7 +759,7 @@ exports[`analyser > should run correctly 1`] = `
"matched file: .github",
"matched file: docker-compose.yml",
"matched file: package.json",
"matched file: /.github/workflows/test.yaml",
"matched file: /.github/workflows/invalid.yml",
"matched file: deno.lock",
"matched file: go.mod",
"matched file: Gemfile",
Expand Down Expand Up @@ -1158,7 +1158,7 @@ exports[`analyser > should run correctly 2`] = `
"JSON": 4,
"SCSS": 1,
"TOML": 1,
"YAML": 2,
"YAML": 3,
},
"name": "fake",
"path": [
Expand Down Expand Up @@ -1808,7 +1808,7 @@ exports[`analyser > should run correctly 2`] = `
"JSON": 11,
"SCSS": 3,
"TOML": 3,
"YAML": 4,
"YAML": 6,
},
"name": "flatten",
"path": [
Expand Down
4 changes: 2 additions & 2 deletions src/rules/spec/deno/lockfile.ts
Expand Up @@ -27,11 +27,11 @@ export const detectDenoLockfile: ComponentMatcher = async (files, provider) => {
json = JSON.parse(content);
} catch (e) {
l.warn('Failed to parse deno.lock', file.fp, e);
return false;
continue;
}

if (!json.version) {
return false;
continue;
}

const deps = {
Expand Down
19 changes: 15 additions & 4 deletions src/rules/spec/docker/component.ts
Expand Up @@ -7,6 +7,11 @@ import type { ComponentMatcher } from '../../../types/rule.js';

const FILES_REG = /^docker-compose(.*)?\.y(a)?ml$/;

interface DockerCompose {
services: {
[key: string]: DockerComposeService;
};
}
interface DockerComposeService {
image?: string;
container_name?: string;
Expand All @@ -26,10 +31,16 @@ export const detectDockerComponent: ComponentMatcher = async (
continue;
}

const parsed = parse(content, {});
if (!parsed?.services) {
l.warn('Failed to parse Docker file', file.fp);
return false;
let parsed: DockerCompose;
try {
parsed = parse(content, {});
if (!parsed?.services) {
l.warn('Failed to parse Docker file', file.fp);
continue;
}
} catch (err) {
l.warn('Failed to parse', file.fp, err);
continue;
}

const pl = new Payload({ name: 'virtual', folderPath: file.fp });
Expand Down
4 changes: 2 additions & 2 deletions src/rules/spec/githubActions/component.ts
Expand Up @@ -55,11 +55,11 @@ export const detectGithubActionsComponent: ComponentMatcher = async (
parsed = parse(content, {});
if (!parsed?.jobs) {
l.warn('No jobs in GitHub Actions', file.fp);
return false;
continue;
}
} catch (err) {
l.warn('Failed to parse', file.fp, err);
return false;
continue;
}

const pl = new Payload({ name: 'virtual', folderPath: file.fp });
Expand Down
4 changes: 2 additions & 2 deletions src/rules/spec/nodejs/component.ts
Expand Up @@ -27,11 +27,11 @@ export const detectNodeComponent: ComponentMatcher = async (
json = JSON.parse(content);
} catch (e) {
l.warn('Failed to parse package.json', file.fp, e);
return false;
continue;
}

if (!json.name) {
return false;
continue;
}

const deps = {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/spec/php/component.ts
Expand Up @@ -28,11 +28,11 @@ export const detectPhpComponent: ComponentMatcher = async (files, provider) => {
json = JSON.parse(content);
} catch (e) {
l.warn('Failed to parse composer.json', file.fp, e);
return false;
continue;
}

if (!json.name) {
return false;
continue;
}

const deps = {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/spec/rust/component.ts
Expand Up @@ -42,7 +42,7 @@ export const detectRustComponent: ComponentMatcher = async (
json = toml.parse(content);
} catch (e) {
l.warn('Failed to parse Cargo.toml', file.fp, e);
return false;
continue;
}

let pl: Payload;
Expand Down
4 changes: 2 additions & 2 deletions src/rules/spec/terraform/lockfile.ts
Expand Up @@ -30,11 +30,11 @@ export const detectTerraformLockfile: ComponentMatcher = async (
json = await parse(file.fp, content);
} catch (err) {
l.warn('Failed to parse HCL', file.fp, err);
return false;
continue;
}

if (!('provider' in json)) {
return false;
continue;
}

const pl = new Payload({
Expand Down

0 comments on commit b258437

Please sign in to comment.