Skip to content

Commit

Permalink
Ensure flags are unquoted (#104)
Browse files Browse the repository at this point in the history
Not sure if this is correct yet, but refs
google-github-actions/deploy-cloudrun#514
  • Loading branch information
sethvargo committed May 8, 2024
1 parent c6e1830 commit 9fb1855
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
19 changes: 19 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ export function parseFlags(input: string): string[] {
result.push(current);
}

// Post-process and remove any super-quoted strings.
for (let i = 0; i < result.length; i++) {
for (;;) {
const v = result[i];
if (v.length < 2) {
break;
}

const first = v.at(0);
const last = v.at(-1);
if ((first === `'` || first === `"`) && first === last) {
result[i] = v.slice(1, -1);
continue;
}

break;
}
}

return result;
}

Expand Down
33 changes: 19 additions & 14 deletions tests/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,47 @@ describe('flags', { concurrency: true }, async () => {
{
name: `with equals and double quotes`,
input: `--bar="2Gi"`,
exp: [`--bar`, `"2Gi"`],
exp: [`--bar`, `2Gi`],
},
{
name: `with space and double quotes`,
input: `--bar "2Gi"`,
exp: [`--bar`, `"2Gi"`],
exp: [`--bar`, `2Gi`],
},
{
name: `with equals and space and double quotes`,
input: `--bar="2Gi" --foo "2"`,
exp: [`--bar`, `"2Gi"`, `--foo`, `"2"`],
exp: [`--bar`, `2Gi`, `--foo`, `2`],
},
{
name: `with equals and space and some double quotes`,
input: `--foo 2 --bar="2Gi"`,
exp: [`--foo`, `2`, `--bar`, `"2Gi"`],
exp: [`--foo`, `2`, `--bar`, `2Gi`],
},
{
name: `with equals and single quotes`,
input: `--bar='2Gi'`,
exp: [`--bar`, `'2Gi'`],
exp: [`--bar`, `2Gi`],
},
{
name: `with space and single quotes`,
input: `--bar '2Gi'`,
exp: [`--bar`, `'2Gi'`],
exp: [`--bar`, `2Gi`],
},
{
name: `with equals and space and single quotes`,
input: `--foo '2' --bar='2Gi'`,
exp: [`--foo`, `'2'`, `--bar`, `'2Gi'`],
exp: [`--foo`, `2`, `--bar`, `2Gi`],
},
{
name: `with equals and space and some single quotes`,
input: `--foo 2 --bar='2Gi' `,
exp: [`--foo`, `2`, `--bar`, `'2Gi'`],
exp: [`--foo`, `2`, `--bar`, `2Gi`],
},
{
name: `with double and single quotes`,
input: `--foo="2" --bar='2Gi'`,
exp: [`--foo`, `"2"`, `--bar`, `'2Gi'`],
exp: [`--foo`, `2`, `--bar`, `2Gi`],
},
{
name: 'with multi-line separators',
Expand All @@ -106,12 +106,12 @@ describe('flags', { concurrency: true }, async () => {
--bar=2Gi
--zip "zap"
`,
exp: [`--foo`, `2`, `--bar`, `2Gi`, `--zip`, `"zap"`],
exp: [`--foo`, `2`, `--bar`, `2Gi`, `--zip`, `zap`],
},
{
name: 'with subflags quoted',
input: `--foo "--bar=1,--zip-zap"`,
exp: [`--foo`, `"--bar=1,--zip-zap"`],
exp: [`--foo`, `--bar=1,--zip-zap`],
},
{
name: 'with subflags equals',
Expand All @@ -121,7 +121,7 @@ describe('flags', { concurrency: true }, async () => {
{
name: 'with subflags quoted subquoted',
input: `--foo "--bar="1",--zip-zap"`,
exp: [`--foo`, `"--bar="1",--zip-zap"`],
exp: [`--foo`, `--bar="1",--zip-zap`],
},
{
name: 'with subflags equals subquoted',
Expand All @@ -141,12 +141,17 @@ describe('flags', { concurrency: true }, async () => {
{
name: 'with kv quote',
input: `--foo --env "foo=bar,zip=zap"`,
exp: [`--foo`, `--env`, `"foo=bar,zip=zap"`],
exp: [`--foo`, `--env`, `foo=bar,zip=zap`],
},
{
name: 'with kv equals quote',
input: `--foo --env="foo=bar,zip=zap"`,
exp: [`--foo`, `--env`, `"foo=bar,zip=zap"`],
exp: [`--foo`, `--env`, `foo=bar,zip=zap`],
},
{
name: 'with quotes full arg',
input: `--foo "--bar=-m -X=1"`,
exp: [`--foo`, `--bar=-m -X=1`],
},
];

Expand Down

0 comments on commit 9fb1855

Please sign in to comment.