2022-04-27 14:58:50 +00:00
|
|
|
import {parse} from 'csv-parse/sync';
|
2022-04-28 07:08:21 +00:00
|
|
|
import * as core from '@actions/core';
|
2022-04-27 14:58:50 +00:00
|
|
|
|
|
|
|
export interface Image {
|
|
|
|
name: string;
|
|
|
|
enable: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Transform(inputs: string[]): Image[] {
|
2022-04-28 07:08:21 +00:00
|
|
|
let images: Image[] = [];
|
|
|
|
|
|
|
|
// backward compatibility with old format
|
|
|
|
if (inputs.length == 1) {
|
|
|
|
let newformat = false;
|
|
|
|
const fields = parse(inputs[0], {
|
|
|
|
relaxColumnCount: true,
|
|
|
|
skipEmptyLines: true
|
|
|
|
})[0];
|
|
|
|
for (const field of fields) {
|
|
|
|
const parts = field
|
|
|
|
.toString()
|
|
|
|
.split('=')
|
|
|
|
.map(item => item.trim());
|
|
|
|
if (parts.length == 1) {
|
2022-05-04 13:02:20 +00:00
|
|
|
images.push({name: parts[0], enable: true});
|
2022-04-28 07:08:21 +00:00
|
|
|
} else {
|
|
|
|
newformat = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!newformat) {
|
|
|
|
return output(images);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
images = [];
|
2022-04-27 14:58:50 +00:00
|
|
|
for (const input of inputs) {
|
|
|
|
const image: Image = {name: '', enable: true};
|
|
|
|
const fields = parse(input, {
|
|
|
|
relaxColumnCount: true,
|
|
|
|
skipEmptyLines: true
|
|
|
|
})[0];
|
|
|
|
for (const field of fields) {
|
|
|
|
const parts = field
|
|
|
|
.toString()
|
|
|
|
.split('=')
|
|
|
|
.map(item => item.trim());
|
|
|
|
if (parts.length == 1) {
|
2022-05-04 13:02:20 +00:00
|
|
|
image.name = parts[0];
|
2022-04-27 14:58:50 +00:00
|
|
|
} else {
|
|
|
|
const key = parts[0].toLowerCase();
|
|
|
|
const value = parts[1];
|
|
|
|
switch (key) {
|
|
|
|
case 'name': {
|
2022-05-04 13:02:20 +00:00
|
|
|
image.name = value;
|
2022-04-27 14:58:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'enable': {
|
|
|
|
if (!['true', 'false'].includes(value)) {
|
|
|
|
throw new Error(`Invalid enable attribute value: ${input}`);
|
|
|
|
}
|
|
|
|
image.enable = /true/i.test(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(`Unknown image attribute: ${input}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (image.name.length == 0) {
|
|
|
|
throw new Error(`Image name attribute empty: ${input}`);
|
|
|
|
}
|
|
|
|
images.push(image);
|
|
|
|
}
|
2022-04-28 07:08:21 +00:00
|
|
|
return output(images);
|
|
|
|
}
|
|
|
|
|
|
|
|
function output(images: Image[]): Image[] {
|
|
|
|
core.startGroup(`Processing images input`);
|
|
|
|
for (const image of images) {
|
|
|
|
core.info(`name=${image.name},enable=${image.enable}`);
|
|
|
|
}
|
|
|
|
core.endGroup();
|
2022-04-27 14:58:50 +00:00
|
|
|
return images;
|
|
|
|
}
|