From 91274a04dae5b06f1c73a9140206dd49fbfd07c2 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 16 Nov 2021 05:19:44 +0100 Subject: [PATCH] sort flags Signed-off-by: CrazyMax --- __tests__/buildx.test.ts | 7 ----- __tests__/context.test.ts | 54 +++++++++++++++----------------- dist/index.js | 66 +++++++++++++++++++-------------------- src/context.ts | 66 +++++++++++++++++++-------------------- 4 files changed, 92 insertions(+), 101 deletions(-) diff --git a/__tests__/buildx.test.ts b/__tests__/buildx.test.ts index e64ac90..028b001 100644 --- a/__tests__/buildx.test.ts +++ b/__tests__/buildx.test.ts @@ -28,10 +28,8 @@ jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => { describe('getImageID', () => { it('matches', async () => { const imageIDFile = await buildx.getImageIDFile(); - console.log(`imageIDFile: ${imageIDFile}`); await fs.writeFileSync(imageIDFile, digest); const imageID = await buildx.getImageID(); - console.log(`imageID: ${imageID}`); expect(imageID).toEqual(digest); }); }); @@ -39,10 +37,8 @@ describe('getImageID', () => { describe('getMetadata', () => { it('matches', async () => { const metadataFile = await buildx.getMetadataFile(); - console.log(`metadataFile: ${metadataFile}`); await fs.writeFileSync(metadataFile, metadata); const expected = await buildx.getMetadata(); - console.log(`metadata: ${expected}`); expect(expected).toEqual(metadata); }); }); @@ -132,7 +128,6 @@ describe('getVersion', () => { 'valid', async () => { const version = await buildx.getVersion(); - console.log(`version: ${version}`); expect(semver.valid(version)).not.toBeNull(); }, 100000 @@ -179,10 +174,8 @@ describe('getSecret', () => { secret = await buildx.getSecretString(kvp); } expect(true).toBe(!invalid); - console.log(`secret: ${secret}`); expect(secret).toEqual(`id=${exKey},src=${tmpNameSync}`); const secretValue = await fs.readFileSync(tmpNameSync, 'utf-8'); - console.log(`secretValue: ${secretValue}`); expect(secretValue).toEqual(exValue); } catch (err) { expect(true).toBe(invalid); diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 3435701..bb917cf 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -140,6 +140,7 @@ describe('getArgs', () => { // prettier-ignore test.each([ [ + 0, '0.4.1', new Map([ ['context', '.'], @@ -156,6 +157,7 @@ describe('getArgs', () => { ] ], [ + 1, '0.4.2', new Map([ ['build-args', 'MY_ARG=val1,val2,val3\nARG=val'], @@ -174,6 +176,7 @@ describe('getArgs', () => { ] ], [ + 2, '0.4.2', new Map([ ['tags', 'name/app:7.4, name/app:latest'], @@ -185,13 +188,14 @@ describe('getArgs', () => { [ 'buildx', 'build', + '--iidfile', '/tmp/.docker-build-push-jest/iidfile', '--tag', 'name/app:7.4', '--tag', 'name/app:latest', - '--iidfile', '/tmp/.docker-build-push-jest/iidfile', 'https://github.com/docker/build-push-action.git#refs/heads/test-jest' ] ], [ + 3, '0.4.2', new Map([ ['context', '.'], @@ -212,6 +216,7 @@ describe('getArgs', () => { ] ], [ + 4, '0.4.1', new Map([ ['context', '.'], @@ -229,6 +234,7 @@ describe('getArgs', () => { ] ], [ + 5, '0.4.1', new Map([ ['context', '.'], @@ -245,6 +251,7 @@ describe('getArgs', () => { ] ], [ + 6, '0.4.2', new Map([ ['context', '.'], @@ -263,6 +270,7 @@ describe('getArgs', () => { ] ], [ + 7, '0.4.2', new Map([ ['github-token', 'abcdefghijklmno0123456789'], @@ -281,6 +289,7 @@ describe('getArgs', () => { ] ], [ + 8, '0.4.2', new Map([ ['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'], @@ -297,16 +306,17 @@ describe('getArgs', () => { [ 'buildx', 'build', - '--platform', 'linux/amd64,linux/arm64', - '--iidfile', '/tmp/.docker-build-push-jest/iidfile', - '--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--file', './test/Dockerfile', + '--iidfile', '/tmp/.docker-build-push-jest/iidfile', + '--platform', 'linux/amd64,linux/arm64', + '--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--builder', 'builder-git-context-2', '--push', 'https://github.com/docker/build-push-action.git#refs/heads/master' ] ], [ + 9, '0.4.2', new Map([ ['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'], @@ -331,19 +341,20 @@ ccc"`], [ 'buildx', 'build', - '--platform', 'linux/amd64,linux/arm64', + '--file', './test/Dockerfile', '--iidfile', '/tmp/.docker-build-push-jest/iidfile', + '--platform', 'linux/amd64,linux/arm64', '--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=MYSECRET,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=FOO,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=EMPTYLINE,src=/tmp/.docker-build-push-jest/.tmpname-jest', - '--file', './test/Dockerfile', '--builder', 'builder-git-context-2', '--push', 'https://github.com/docker/build-push-action.git#refs/heads/master' ] ], [ + 10, '0.4.2', new Map([ ['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'], @@ -368,19 +379,20 @@ ccc`], [ 'buildx', 'build', - '--platform', 'linux/amd64,linux/arm64', + '--file', './test/Dockerfile', '--iidfile', '/tmp/.docker-build-push-jest/iidfile', + '--platform', 'linux/amd64,linux/arm64', '--secret', 'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=MYSECRET,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=FOO,src=/tmp/.docker-build-push-jest/.tmpname-jest', '--secret', 'id=EMPTYLINE,src=/tmp/.docker-build-push-jest/.tmpname-jest', - '--file', './test/Dockerfile', '--builder', 'builder-git-context-2', '--push', 'https://github.com/docker/build-push-action.git#refs/heads/master' ] ], [ + 11, '0.5.1', new Map([ ['context', 'https://github.com/docker/build-push-action.git#refs/heads/master'], @@ -397,9 +409,9 @@ ccc`], [ 'buildx', 'build', + '--file', './test/Dockerfile', '--iidfile', '/tmp/.docker-build-push-jest/iidfile', '--secret', 'id=MY_SECRET,src=/tmp/.docker-build-push-jest/.tmpname-jest', - '--file', './test/Dockerfile', '--builder', 'builder-git-context-2', '--network', 'host', '--push', @@ -407,6 +419,7 @@ ccc`], ] ], [ + 12, '0.4.2', new Map([ ['context', '.'], @@ -427,6 +440,7 @@ ccc`], ] ], [ + 13, '0.6.0', new Map([ ['context', '.'], @@ -441,25 +455,23 @@ ccc`], [ 'buildx', 'build', + '--file', './test/Dockerfile', '--iidfile', '/tmp/.docker-build-push-jest/iidfile', '--metadata-file', '/tmp/.docker-build-push-jest/metadata-file', - '--file', './test/Dockerfile', '--network', 'host', '--push', '.' ] ], ])( - 'given %p with %p as inputs, returns %p', - async (buildxVersion: string, inputs: Map, expected: Array) => { + '[%d] given %p with %p as inputs, returns %p', + async (num: number, buildxVersion: string, inputs: Map, expected: Array) => { await inputs.forEach((value: string, name: string) => { setInput(name, value); }); const defContext = context.defaultContext(); const inp = await context.getInputs(defContext); - console.log(inp); const res = await context.getArgs(inp, defContext, buildxVersion); - console.log(res); expect(res).toEqual(expected); } ); @@ -469,63 +481,54 @@ describe('getInputList', () => { it('single line correctly', async () => { await setInput('foo', 'bar'); const res = await context.getInputList('foo'); - console.log(res); expect(res).toEqual(['bar']); }); it('multiline correctly', async () => { setInput('foo', 'bar\nbaz'); const res = await context.getInputList('foo'); - console.log(res); expect(res).toEqual(['bar', 'baz']); }); it('empty lines correctly', async () => { setInput('foo', 'bar\n\nbaz'); const res = await context.getInputList('foo'); - console.log(res); expect(res).toEqual(['bar', 'baz']); }); it('comma correctly', async () => { setInput('foo', 'bar,baz'); const res = await context.getInputList('foo'); - console.log(res); expect(res).toEqual(['bar', 'baz']); }); it('empty result correctly', async () => { setInput('foo', 'bar,baz,'); const res = await context.getInputList('foo'); - console.log(res); expect(res).toEqual(['bar', 'baz']); }); it('different new lines correctly', async () => { setInput('foo', 'bar\r\nbaz'); const res = await context.getInputList('foo'); - console.log(res); expect(res).toEqual(['bar', 'baz']); }); it('different new lines and comma correctly', async () => { setInput('foo', 'bar\r\nbaz,bat'); const res = await context.getInputList('foo'); - console.log(res); expect(res).toEqual(['bar', 'baz', 'bat']); }); it('multiline and ignoring comma correctly', async () => { setInput('cache-from', 'user/app:cache\ntype=local,src=path/to/dir'); const res = await context.getInputList('cache-from', true); - console.log(res); expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']); }); it('different new lines and ignoring comma correctly', async () => { setInput('cache-from', 'user/app:cache\r\ntype=local,src=path/to/dir'); const res = await context.getInputList('cache-from', true); - console.log(res); expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']); }); @@ -539,7 +542,6 @@ ccccccccc" FOO=bar` ); const res = await context.getInputList('secrets', true); - console.log(res); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa @@ -563,7 +565,6 @@ bbbb ccc"` ); const res = await context.getInputList('secrets', true); - console.log(res); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa @@ -587,7 +588,6 @@ ccccccccc FOO=bar` ); const res = await context.getInputList('secrets', true); - console.log(res); expect(res).toEqual(['GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', 'MYSECRET=aaaaaaaa', 'bbbbbbb', 'ccccccccc', 'FOO=bar']); }); @@ -598,7 +598,6 @@ FOO=bar` FOO=bar` ); const res = await context.getInputList('secrets', true); - console.log(res); expect(res).toEqual([`GPG_KEY=${pgp}`, 'FOO=bar']); }); @@ -612,7 +611,6 @@ ccccccccc" FOO=bar` ); const res = await context.getInputList('secrets', true); - console.log(res); expect(res).toEqual([ 'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789', `MYSECRET=aaaaaaaa diff --git a/dist/index.js b/dist/index.js index 88ca69a..2064a02 100644 --- a/dist/index.js +++ b/dist/index.js @@ -298,7 +298,7 @@ function getArgs(inputs, defaultContext, buildxVersion) { return __awaiter(this, void 0, void 0, function* () { let args = ['buildx']; args.push.apply(args, yield getBuildArgs(inputs, defaultContext, buildxVersion)); - args.push.apply(args, yield getCommonArgs(inputs)); + args.push.apply(args, yield getCommonArgs(inputs, buildxVersion)); args.push(inputs.context); return args; }); @@ -307,39 +307,33 @@ exports.getArgs = getArgs; function getBuildArgs(inputs, defaultContext, buildxVersion) { return __awaiter(this, void 0, void 0, function* () { let args = ['build']; - yield exports.asyncForEach(inputs.buildArgs, (buildArg) => __awaiter(this, void 0, void 0, function* () { - args.push('--build-arg', buildArg); - })); - yield exports.asyncForEach(inputs.labels, (label) => __awaiter(this, void 0, void 0, function* () { - args.push('--label', label); - })); - yield exports.asyncForEach(inputs.tags, (tag) => __awaiter(this, void 0, void 0, function* () { - args.push('--tag', tag); - })); - if (inputs.target) { - args.push('--target', inputs.target); - } if (inputs.allow.length > 0) { args.push('--allow', inputs.allow.join(',')); } - if (inputs.platforms.length > 0) { - args.push('--platform', inputs.platforms.join(',')); - } - yield exports.asyncForEach(inputs.outputs, (output) => __awaiter(this, void 0, void 0, function* () { - args.push('--output', output); + yield exports.asyncForEach(inputs.buildArgs, (buildArg) => __awaiter(this, void 0, void 0, function* () { + args.push('--build-arg', buildArg); })); - if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) { - args.push('--iidfile', yield buildx.getImageIDFile()); - } - if (buildx.satisfies(buildxVersion, '>=0.6.0')) { - args.push('--metadata-file', yield buildx.getMetadataFile()); - } yield exports.asyncForEach(inputs.cacheFrom, (cacheFrom) => __awaiter(this, void 0, void 0, function* () { args.push('--cache-from', cacheFrom); })); yield exports.asyncForEach(inputs.cacheTo, (cacheTo) => __awaiter(this, void 0, void 0, function* () { args.push('--cache-to', cacheTo); })); + if (inputs.file) { + args.push('--file', inputs.file); + } + if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) { + args.push('--iidfile', yield buildx.getImageIDFile()); + } + yield exports.asyncForEach(inputs.labels, (label) => __awaiter(this, void 0, void 0, function* () { + args.push('--label', label); + })); + yield exports.asyncForEach(inputs.outputs, (output) => __awaiter(this, void 0, void 0, function* () { + args.push('--output', output); + })); + if (inputs.platforms.length > 0) { + args.push('--platform', inputs.platforms.join(',')); + } yield exports.asyncForEach(inputs.secrets, (secret) => __awaiter(this, void 0, void 0, function* () { try { args.push('--secret', yield buildx.getSecretString(secret)); @@ -362,30 +356,36 @@ function getBuildArgs(inputs, defaultContext, buildxVersion) { yield exports.asyncForEach(inputs.ssh, (ssh) => __awaiter(this, void 0, void 0, function* () { args.push('--ssh', ssh); })); - if (inputs.file) { - args.push('--file', inputs.file); + yield exports.asyncForEach(inputs.tags, (tag) => __awaiter(this, void 0, void 0, function* () { + args.push('--tag', tag); + })); + if (inputs.target) { + args.push('--target', inputs.target); } return args; }); } -function getCommonArgs(inputs) { +function getCommonArgs(inputs, buildxVersion) { return __awaiter(this, void 0, void 0, function* () { let args = []; - if (inputs.noCache) { - args.push('--no-cache'); - } if (inputs.builder) { args.push('--builder', inputs.builder); } - if (inputs.pull) { - args.push('--pull'); - } if (inputs.load) { args.push('--load'); } + if (buildx.satisfies(buildxVersion, '>=0.6.0')) { + args.push('--metadata-file', yield buildx.getMetadataFile()); + } if (inputs.network) { args.push('--network', inputs.network); } + if (inputs.noCache) { + args.push('--no-cache'); + } + if (inputs.pull) { + args.push('--pull'); + } if (inputs.push) { args.push('--push'); } diff --git a/src/context.ts b/src/context.ts index b951a9b..0759ea4 100644 --- a/src/context.ts +++ b/src/context.ts @@ -90,46 +90,40 @@ 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.apply(args, await getCommonArgs(inputs, buildxVersion)); args.push(inputs.context); return args; } 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); - }); - if (inputs.target) { - args.push('--target', inputs.target); - } if (inputs.allow.length > 0) { args.push('--allow', inputs.allow.join(',')); } - if (inputs.platforms.length > 0) { - args.push('--platform', inputs.platforms.join(',')); - } - await asyncForEach(inputs.outputs, async output => { - args.push('--output', output); + await asyncForEach(inputs.buildArgs, async buildArg => { + args.push('--build-arg', buildArg); }); - if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) { - args.push('--iidfile', await buildx.getImageIDFile()); - } - if (buildx.satisfies(buildxVersion, '>=0.6.0')) { - args.push('--metadata-file', await buildx.getMetadataFile()); - } await asyncForEach(inputs.cacheFrom, async cacheFrom => { args.push('--cache-from', cacheFrom); }); await asyncForEach(inputs.cacheTo, async cacheTo => { args.push('--cache-to', cacheTo); }); + if (inputs.file) { + args.push('--file', inputs.file); + } + if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) { + args.push('--iidfile', await buildx.getImageIDFile()); + } + await asyncForEach(inputs.labels, async label => { + args.push('--label', label); + }); + await asyncForEach(inputs.outputs, async output => { + args.push('--output', output); + }); + if (inputs.platforms.length > 0) { + args.push('--platform', inputs.platforms.join(',')); + } await asyncForEach(inputs.secrets, async secret => { try { args.push('--secret', await buildx.getSecretString(secret)); @@ -150,29 +144,35 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio await asyncForEach(inputs.ssh, async ssh => { args.push('--ssh', ssh); }); - if (inputs.file) { - args.push('--file', inputs.file); + await asyncForEach(inputs.tags, async tag => { + args.push('--tag', tag); + }); + if (inputs.target) { + args.push('--target', inputs.target); } return args; } -async function getCommonArgs(inputs: Inputs): Promise> { +async function getCommonArgs(inputs: Inputs, buildxVersion: string): Promise> { let args: Array = []; - if (inputs.noCache) { - args.push('--no-cache'); - } if (inputs.builder) { args.push('--builder', inputs.builder); } - if (inputs.pull) { - args.push('--pull'); - } if (inputs.load) { args.push('--load'); } + if (buildx.satisfies(buildxVersion, '>=0.6.0')) { + args.push('--metadata-file', await buildx.getMetadataFile()); + } if (inputs.network) { args.push('--network', inputs.network); } + if (inputs.noCache) { + args.push('--no-cache'); + } + if (inputs.pull) { + args.push('--pull'); + } if (inputs.push) { args.push('--push'); }