From 36ea4ee5558261acd1a2c1677d90f36a50d8dd3e Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Wed, 20 Dec 2023 14:48:27 +0100 Subject: [PATCH] bake: refactor generateBakeFile Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- src/meta.ts | 96 +++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 50 deletions(-) diff --git a/src/meta.ts b/src/meta.ts index f93636f..f7d47a6 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -522,70 +522,66 @@ export class Meta { public getBakeFile(kind: string): string { if (kind == 'tags') { - return this.generateBakeFile(kind, { - tags: this.getTags(), - args: { - DOCKER_META_IMAGES: this.getImageNames().join(','), - DOCKER_META_VERSION: this.version.main - } - }); - } else if (kind == 'labels') { - return this.generateBakeFile(kind, { - labels: this.getLabels().reduce((res, label) => { - const matches = label.match(/([^=]*)=(.*)/); - if (!matches) { - return res; + return this.generateBakeFile( + { + tags: this.getTags(), + args: { + DOCKER_META_IMAGES: this.getImageNames().join(','), + DOCKER_META_VERSION: this.version.main } - res[matches[1]] = matches[2]; - return res; - }, {}) - }); + }, + kind + ); + } else if (kind == 'labels') { + return this.generateBakeFile( + { + labels: this.getLabels().reduce((res, label) => { + const matches = label.match(/([^=]*)=(.*)/); + if (!matches) { + return res; + } + res[matches[1]] = matches[2]; + return res; + }, {}) + }, + kind + ); } else if (kind.startsWith('annotations:')) { const name = kind.split(':')[0]; const annotations: Array = []; for (const level of kind.split(':')[1].split(',')) { annotations.push(...this.getAnnotations().map(label => `${level}:${label}`)); } - return this.generateBakeFile(name, { - annotations: annotations - }); + return this.generateBakeFile( + { + annotations: annotations + }, + name + ); } throw new Error(`Unknown bake file type: ${kind}`); } public getBakeFileTagsLabels(): string { - const bakeFile = path.join(ToolkitContext.tmpDir(), 'docker-metadata-action-bake.json'); - fs.writeFileSync( - bakeFile, - JSON.stringify( - { - target: { - [this.inputs.bakeTarget]: { - tags: this.getTags(), - labels: this.getLabels().reduce((res, label) => { - const matches = label.match(/([^=]*)=(.*)/); - if (!matches) { - return res; - } - res[matches[1]] = matches[2]; - return res; - }, {}), - args: { - DOCKER_META_IMAGES: this.getImageNames().join(','), - DOCKER_META_VERSION: this.version.main - } - } - } - }, - null, - 2 - ) - ); - return bakeFile; + return this.generateBakeFile({ + tags: this.getTags(), + labels: this.getLabels().reduce((res, label) => { + const matches = label.match(/([^=]*)=(.*)/); + if (!matches) { + return res; + } + res[matches[1]] = matches[2]; + return res; + }, {}), + args: { + DOCKER_META_IMAGES: this.getImageNames().join(','), + DOCKER_META_VERSION: this.version.main + } + }); } - private generateBakeFile(name: string, dt): string { - const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake-${name}.json`); + private generateBakeFile(dt, suffix?: string): string { + const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake${suffix ? `-${suffix}` : ''}.json`); fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2)); return bakeFile; }