Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified and unified argument collection #430

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
simplified argument collection
Signed-off-by: Phred <fearphage@gmail.com>
  • Loading branch information
fearphage committed Nov 17, 2021
commit af4fd4efb5422944dd62d50a4f0bac08826c06c7
13 changes: 13 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
beforeEach(() => {
process.stdout.write = jest.fn();
41 changes: 20 additions & 21 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -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>> {
let args: Array<string> = ['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<Array<string>> {
let args: Array<string> = ['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<string> = ['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);
}
@@ -115,9 +116,6 @@ 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());
}
@@ -147,9 +145,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);
}
@@ -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
export function setOutput(name: string, value: any): void {
issueCommand('set-output', {name}, value);