From 7a80bb2c06e359c35d3003b7e53a01b00d4780b6 Mon Sep 17 00:00:00 2001 From: Phred Date: Fri, 6 Aug 2021 08:56:33 -0500 Subject: [PATCH] simplified argument collection --- __tests__/context.test.ts | 13 +++++++++++ src/context.ts | 47 +++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 3fc2cfe38..687fb7456 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -613,6 +613,19 @@ describe('asyncForEach', () => { }); }); +describe('flagMap', () => { + it('should prepend array elements with the provided flag', async () => { + const testValues = ['a', 'b', 'c']; + const results: string[][] = context.flagMap(testValues, '--catpants'); + + expect(results).toEqual([ + ['--catpants', 'a'], + ['--catpants', 'b'], + ['--catpants', 'c'], + ]); + }); +}); + describe('setOutput', () => { beforeEach(() => { process.stdout.write = jest.fn(); diff --git a/src/context.ts b/src/context.ts index 552b0fe6e..ddc1d9dae 100644 --- a/src/context.ts +++ b/src/context.ts @@ -89,24 +89,25 @@ export async function getInputs(defaultContext: string): Promise { } export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise> { - let args: Array = ['buildx']; - args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion)); - args.push.apply(args, await getCommonArgs(inputs)); - args.push(inputs.context); - return args; + return [ + 'buildx', + ...await getBuildArgs(inputs, defaultContext, buildxVersion), + ...await getCommonArgs(inputs), + inputs.context, + ]; } async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise> { - let args: Array = ['build']; - await asyncForEach(inputs.buildArgs, async buildArg => { - args.push('--build-arg', buildArg); - }); - await asyncForEach(inputs.labels, async label => { - args.push('--label', label); - }); - await asyncForEach(inputs.tags, async tag => { - args.push('--tag', tag); - }); + const args: Array = ['build'].concat( + ...flagMap(inputs.buildArgs, '--build-arg'), + ...flagMap(inputs.cacheFrom, '--cache-from'), + ...flagMap(inputs.cacheTo, '--cache-to'), + ...flagMap(inputs.labels, '--label'), + ...flagMap(inputs.outputs, '--output'), + ...flagMap(inputs.tags, '--tag'), + ...flagMap(inputs.ssh, '--ssh'), + ); + if (inputs.target) { args.push('--target', inputs.target); } @@ -116,18 +117,9 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio if (inputs.platforms.length > 0) { args.push('--platform', inputs.platforms.join(',')); } - await asyncForEach(inputs.outputs, async output => { - args.push('--output', output); - }); if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) { args.push('--iidfile', await buildx.getImageIDFile()); } - await asyncForEach(inputs.cacheFrom, async cacheFrom => { - args.push('--cache-from', cacheFrom); - }); - await asyncForEach(inputs.cacheTo, async cacheTo => { - args.push('--cache-to', cacheTo); - }); await asyncForEach(inputs.secrets, async secret => { try { args.push('--secret', await buildx.getSecretString(secret)); @@ -145,9 +137,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) { args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`)); } - await asyncForEach(inputs.ssh, async ssh => { - args.push('--ssh', ssh); - }); if (inputs.file) { args.push('--file', inputs.file); } @@ -210,6 +199,10 @@ export const asyncForEach = async (array, callback) => { } }; +export function flagMap(array: string[], flag: string): string[][] { + return array.map(value => [flag, value]); +} + // FIXME: Temp fix https://github.com/actions/toolkit/issues/777 export function setOutput(name: string, value: any): void { issueCommand('set-output', {name}, value);