[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:
parent
c940da4bd0
commit
fd0c1fa490
2 changed files with 32 additions and 2 deletions
|
@ -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', () => {
|
||||
|
|
|
@ -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 '';
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue