[Agent Packages] Extend 'contains' helper to work on strings (#102786)

* Extend 'contains' helper to work on strings

* remove stray import
This commit is contained in:
Andrew Stucki 2021-06-22 11:30:32 -04:00 committed by GitHub
parent c940da4bd0
commit fd0c1fa490
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View file

@ -129,6 +129,13 @@ processors:
password: {{password}}
{{#if password}}
hidden_password: {{password}}
{{/if}}
`;
const streamTemplateWithString = `
{{#if (contains ".pcap" file)}}
pcap: true
{{else}}
pcap: false
{{/if}}
`;
@ -168,6 +175,28 @@ hidden_password: {{password}}
tags: ['foo', 'bar'],
});
});
it('should support strings', () => {
const vars = {
file: { value: 'foo.pcap' },
};
const output = compileTemplate(vars, streamTemplateWithString);
expect(output).toEqual({
pcap: true,
});
});
it('should support strings with no match', () => {
const vars = {
file: { value: 'file' },
};
const output = compileTemplate(vars, streamTemplateWithString);
expect(output).toEqual({
pcap: false,
});
});
});
it('should support optional yaml values at root level', () => {

View file

@ -111,11 +111,12 @@ function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateSt
return { vars, yamlValues };
}
function containsHelper(this: any, item: string, list: string[], options: any) {
if (Array.isArray(list) && list.includes(item)) {
function containsHelper(this: any, item: string, check: string | string[], options: any) {
if ((Array.isArray(check) || typeof check === 'string') && check.includes(item)) {
if (options && options.fn) {
return options.fn(this);
}
return true;
}
return '';
}