simplified argument collection

Signed-off-by: Phred <fearphage@gmail.com>
This commit is contained in:
Phred 2021-08-06 08:56:33 -05:00
parent 4222161e3e
commit af4fd4efb5
No known key found for this signature in database
GPG Key ID: 8103F27168DAA2A0
2 changed files with 33 additions and 21 deletions

View File

@ -636,6 +636,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', () => { describe('setOutput', () => {
beforeEach(() => { beforeEach(() => {
process.stdout.write = jest.fn(); process.stdout.write = jest.fn();

View File

@ -88,24 +88,25 @@ export async function getInputs(defaultContext: string): Promise<Inputs> {
} }
export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> { export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['buildx']; return [
args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion)); 'buildx',
args.push.apply(args, await getCommonArgs(inputs)); ...await getBuildArgs(inputs, defaultContext, buildxVersion),
args.push(inputs.context); ...await getCommonArgs(inputs),
return args; inputs.context,
];
} }
async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> { async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['build']; const args: Array<string> = ['build'].concat(
await asyncForEach(inputs.buildArgs, async buildArg => { ...flagMap(inputs.buildArgs, '--build-arg'),
args.push('--build-arg', buildArg); ...flagMap(inputs.cacheFrom, '--cache-from'),
}); ...flagMap(inputs.cacheTo, '--cache-to'),
await asyncForEach(inputs.labels, async label => { ...flagMap(inputs.labels, '--label'),
args.push('--label', label); ...flagMap(inputs.outputs, '--output'),
}); ...flagMap(inputs.tags, '--tag'),
await asyncForEach(inputs.tags, async tag => { ...flagMap(inputs.ssh, '--ssh'),
args.push('--tag', tag); );
});
if (inputs.target) { if (inputs.target) {
args.push('--target', inputs.target); args.push('--target', inputs.target);
} }
@ -115,9 +116,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
if (inputs.platforms.length > 0) { if (inputs.platforms.length > 0) {
args.push('--platform', inputs.platforms.join(',')); 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'))) { if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) {
args.push('--iidfile', await buildx.getImageIDFile()); args.push('--iidfile', await buildx.getImageIDFile());
} }
@ -147,9 +145,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) { if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) {
args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`)); args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`));
} }
await asyncForEach(inputs.ssh, async ssh => {
args.push('--ssh', ssh);
});
if (inputs.file) { if (inputs.file) {
args.push('--file', inputs.file); args.push('--file', inputs.file);
} }
@ -212,6 +207,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 // FIXME: Temp fix https://github.com/actions/toolkit/issues/777
export function setOutput(name: string, value: any): void { export function setOutput(name: string, value: any): void {
issueCommand('set-output', {name}, value); issueCommand('set-output', {name}, value);