metadata-action/src/context.ts

69 lines
1.6 KiB
TypeScript
Raw Normal View History

import csvparse from 'csv-parse/lib/sync';
2020-10-25 02:25:23 +01:00
import * as core from '@actions/core';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
let _tmpDir: string;
2020-10-25 02:25:23 +01:00
export interface Inputs {
images: string[];
tags: string[];
flavor: string[];
labels: string[];
2020-10-25 02:25:23 +01:00
sepTags: string;
sepLabels: string;
githubToken: string;
}
export function tmpDir(): string {
if (!_tmpDir) {
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ghaction-docker-meta-')).split(path.sep).join(path.posix.sep);
}
return _tmpDir;
}
2020-10-25 02:25:23 +01:00
export function getInputs(): Inputs {
return {
images: getInputList('images'),
tags: getInputList('tags', true),
flavor: getInputList('flavor', true),
labels: getInputList('labels', true),
2020-10-25 02:25:23 +01:00
sepTags: core.getInput('sep-tags') || `\n`,
sepLabels: core.getInput('sep-labels') || `\n`,
githubToken: core.getInput('github-token')
};
}
export function getInputList(name: string, ignoreComma?: boolean): string[] {
let res: Array<string> = [];
2020-10-25 02:25:23 +01:00
const items = core.getInput(name);
if (items == '') {
return res;
}
for (let output of csvparse(items, {
columns: false,
relaxColumnCount: true,
skipLinesWithEmptyValues: true
}) as Array<string[]>) {
if (output.length == 1) {
res.push(output[0]);
continue;
} else if (!ignoreComma) {
res.push(...output);
continue;
}
res.push(output.join(','));
2020-10-25 02:25:23 +01:00
}
return res.filter(item => item).map(pat => pat.trim());
2020-10-25 02:25:23 +01:00
}
export const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};