Skip to content

Commit af4fd4e

Browse files
committed
simplified argument collection
Signed-off-by: Phred <[email protected]>
1 parent 4222161 commit af4fd4e

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

__tests__/context.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,19 @@ describe('asyncForEach', () => {
636636
});
637637
});
638638

639+
describe('flagMap', () => {
640+
it('should prepend array elements with the provided flag', async () => {
641+
const testValues = ['a', 'b', 'c'];
642+
const results: string[][] = context.flagMap(testValues, '--catpants');
643+
644+
expect(results).toEqual([
645+
['--catpants', 'a'],
646+
['--catpants', 'b'],
647+
['--catpants', 'c'],
648+
]);
649+
});
650+
});
651+
639652
describe('setOutput', () => {
640653
beforeEach(() => {
641654
process.stdout.write = jest.fn();

src/context.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,25 @@ export async function getInputs(defaultContext: string): Promise<Inputs> {
8888
}
8989

9090
export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
91-
let args: Array<string> = ['buildx'];
92-
args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion));
93-
args.push.apply(args, await getCommonArgs(inputs));
94-
args.push(inputs.context);
95-
return args;
91+
return [
92+
'buildx',
93+
...await getBuildArgs(inputs, defaultContext, buildxVersion),
94+
...await getCommonArgs(inputs),
95+
inputs.context,
96+
];
9697
}
9798

9899
async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
99-
let args: Array<string> = ['build'];
100-
await asyncForEach(inputs.buildArgs, async buildArg => {
101-
args.push('--build-arg', buildArg);
102-
});
103-
await asyncForEach(inputs.labels, async label => {
104-
args.push('--label', label);
105-
});
106-
await asyncForEach(inputs.tags, async tag => {
107-
args.push('--tag', tag);
108-
});
100+
const args: Array<string> = ['build'].concat(
101+
...flagMap(inputs.buildArgs, '--build-arg'),
102+
...flagMap(inputs.cacheFrom, '--cache-from'),
103+
...flagMap(inputs.cacheTo, '--cache-to'),
104+
...flagMap(inputs.labels, '--label'),
105+
...flagMap(inputs.outputs, '--output'),
106+
...flagMap(inputs.tags, '--tag'),
107+
...flagMap(inputs.ssh, '--ssh'),
108+
);
109+
109110
if (inputs.target) {
110111
args.push('--target', inputs.target);
111112
}
@@ -115,9 +116,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
115116
if (inputs.platforms.length > 0) {
116117
args.push('--platform', inputs.platforms.join(','));
117118
}
118-
await asyncForEach(inputs.outputs, async output => {
119-
args.push('--output', output);
120-
});
121119
if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) {
122120
args.push('--iidfile', await buildx.getImageIDFile());
123121
}
@@ -147,9 +145,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
147145
if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) {
148146
args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`));
149147
}
150-
await asyncForEach(inputs.ssh, async ssh => {
151-
args.push('--ssh', ssh);
152-
});
153148
if (inputs.file) {
154149
args.push('--file', inputs.file);
155150
}
@@ -212,6 +207,10 @@ export const asyncForEach = async (array, callback) => {
212207
}
213208
};
214209

210+
export function flagMap(array: string[], flag: string): string[][] {
211+
return array.map(value => [flag, value]);
212+
}
213+
215214
// FIXME: Temp fix https://github.com/actions/toolkit/issues/777
216215
export function setOutput(name: string, value: any): void {
217216
issueCommand('set-output', {name}, value);

0 commit comments

Comments
 (0)