Skip to content

Commit

Permalink
Merge pull request #36 from crazy-max/update-input-list
Browse files Browse the repository at this point in the history
util: opt to escape quotes for input list
  • Loading branch information
crazy-max authored Feb 18, 2023
2 parents 4d9d62d + 44b1545 commit 5695c00
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
20 changes: 13 additions & 7 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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']);
});

Expand All @@ -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']);
});

Expand All @@ -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
Expand Down
23 changes: 16 additions & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = [];

const items = core.getInput(name);
Expand All @@ -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<string[]>) {
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());
Expand Down

0 comments on commit 5695c00

Please sign in to comment.