diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index 87adffd8..59b2debc 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -69,16 +69,22 @@ describe('getInputList', () => { it('multiline and ignoring comma correctly', async () => { setInput('cache-from', 'user/app:cache\ntype=local,src=path/to/dir'); - const res = Util.getInputList('cache-from', true); + const res = Util.getInputList('cache-from', {ignoreComma: true}); expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']); }); it('different new lines and ignoring comma correctly', async () => { setInput('cache-from', 'user/app:cache\r\ntype=local,src=path/to/dir'); - const res = Util.getInputList('cache-from', true); + const res = Util.getInputList('cache-from', {ignoreComma: true}); expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']); }); + it('escape surrounding quotes', async () => { + setInput('platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'); + const res = Util.getInputList('platforms', {escapeQuotes: true}); + expect(res).toEqual(['linux/amd64', 'linux/arm64', 'linux/arm/v7']); + }); + it('multiline values', async () => { setInput( 'secrets', @@ -88,7 +94,7 @@ bbbbbbb ccccccccc" FOO=bar` ); - const res = Util.getInputList('secrets', true); + const res = Util.getInputList('secrets', {ignoreComma: true}); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa @@ -111,7 +117,7 @@ FOO=bar bbbb ccc"` ); - const res = Util.getInputList('secrets', true); + const res = Util.getInputList('secrets', {ignoreComma: true}); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa @@ -134,7 +140,7 @@ bbbbbbb ccccccccc FOO=bar` ); - const res = Util.getInputList('secrets', true); + const res = Util.getInputList('secrets', {ignoreComma: true}); expect(res).toEqual(['GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', 'MYSECRET=aaaaaaaa', 'bbbbbbb', 'ccccccccc', 'FOO=bar']); }); @@ -145,7 +151,7 @@ FOO=bar` `"GPG_KEY=${pgp}" FOO=bar` ); - const res = Util.getInputList('secrets', true); + const res = Util.getInputList('secrets', {ignoreComma: true}); expect(res).toEqual([`GPG_KEY=${pgp}`, 'FOO=bar']); }); @@ -158,7 +164,7 @@ bbbb""bbb ccccccccc" FOO=bar` ); - const res = Util.getInputList('secrets', true); + const res = Util.getInputList('secrets', {ignoreComma: true}); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa diff --git a/src/util.ts b/src/util.ts index 09fa0c90..e8b9b13b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -17,8 +17,13 @@ import * as core from '@actions/core'; import {parse} from 'csv-parse/sync'; +export interface InputListOpts { + ignoreComma?: boolean; + escapeQuotes?: boolean; +} + export class Util { - public static getInputList(name: string, ignoreComma?: boolean): string[] { + public static getInputList(name: string, opts?: InputListOpts): string[] { const res: Array = []; const items = core.getInput(name); @@ -31,18 +36,22 @@ export class Util { relaxQuotes: true, comment: '#', relaxColumnCount: true, - skipEmptyLines: true + skipEmptyLines: true, + quote: opts?.escapeQuotes ?? `"` }); for (const record of records as Array) { if (record.length == 1) { - res.push(record[0]); - continue; - } else if (!ignoreComma) { + if (opts?.ignoreComma) { + res.push(record[0]); + } else { + res.push(...record[0].split(',')); + } + } else if (!opts?.ignoreComma) { res.push(...record); - continue; + } else { + res.push(record.join(',')); } - res.push(record.join(',')); } return res.filter(item => item).map(pat => pat.trim());