3
0
Fork 0
mirror of https://gitea.com/docker/metadata-action.git synced 2024-11-25 13:39:33 +01:00

Merge pull request #143 from crazy-max/trim-val

Trim tags and flavor inputs
This commit is contained in:
CrazyMax 2021-10-23 03:53:44 +02:00 committed by GitHub
commit 548e2346a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 26 deletions

View file

@ -152,6 +152,19 @@ describe('transform', () => {
} as Flavor, } as Flavor,
false false
], ],
[
[
`prefix= `,
],
{
latest: "auto",
prefix: "",
prefixLatest: false,
suffix: "",
suffixLatest: false,
} as Flavor,
false
]
])('given %p attributes ', async (inputs: string[], expected: Flavor, invalid: boolean) => { ])('given %p attributes ', async (inputs: string[], expected: Flavor, invalid: boolean) => {
try { try {
const flavor = Transform(inputs); const flavor = Transform(inputs);

34
dist/index.js generated vendored
View file

@ -151,39 +151,44 @@ function Transform(inputs) {
})[0]; })[0];
let onlatestfor = ''; let onlatestfor = '';
for (const field of fields) { for (const field of fields) {
const parts = field.toString().split('=', 2); const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) { if (parts.length == 1) {
throw new Error(`Invalid flavor entry: ${input}`); throw new Error(`Invalid flavor entry: ${input}`);
} }
switch (parts[0]) { const key = parts[0].toLowerCase();
const value = parts[1];
switch (key) {
case 'latest': { case 'latest': {
flavor.latest = parts[1]; flavor.latest = value;
if (!['auto', 'true', 'false'].includes(flavor.latest)) { if (!['auto', 'true', 'false'].includes(flavor.latest)) {
throw new Error(`Invalid latest flavor entry: ${input}`); throw new Error(`Invalid latest flavor entry: ${input}`);
} }
break; break;
} }
case 'prefix': { case 'prefix': {
flavor.prefix = parts[1]; flavor.prefix = value;
onlatestfor = 'prefix'; onlatestfor = 'prefix';
break; break;
} }
case 'suffix': { case 'suffix': {
flavor.suffix = parts[1]; flavor.suffix = value;
onlatestfor = 'suffix'; onlatestfor = 'suffix';
break; break;
} }
case 'onlatest': { case 'onlatest': {
if (!['true', 'false'].includes(parts[1])) { if (!['true', 'false'].includes(value)) {
throw new Error(`Invalid value for onlatest attribute: ${parts[1]}`); throw new Error(`Invalid value for onlatest attribute: ${value}`);
} }
switch (onlatestfor) { switch (onlatestfor) {
case 'prefix': { case 'prefix': {
flavor.prefixLatest = /true/i.test(parts[1]); flavor.prefixLatest = /true/i.test(value);
break; break;
} }
case 'suffix': { case 'suffix': {
flavor.suffixLatest = /true/i.test(parts[1]); flavor.suffixLatest = /true/i.test(value);
break; break;
} }
} }
@ -902,13 +907,16 @@ function Parse(s) {
})[0]; })[0];
const tag = new Tag(); const tag = new Tag();
for (const field of fields) { for (const field of fields) {
const parts = field.toString().split('=', 2); const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) { if (parts.length == 1) {
tag.attrs['value'] = parts[0].trim(); tag.attrs['value'] = parts[0];
} }
else { else {
const key = parts[0].trim().toLowerCase(); const key = parts[0].toLowerCase();
const value = parts[1].trim(); const value = parts[1];
switch (key) { switch (key) {
case 'type': { case 'type': {
if (!Object.values(Type).includes(value)) { if (!Object.values(Type).includes(value)) {

View file

@ -25,39 +25,44 @@ export function Transform(inputs: string[]): Flavor {
})[0]; })[0];
let onlatestfor = ''; let onlatestfor = '';
for (const field of fields) { for (const field of fields) {
const parts = field.toString().split('=', 2); const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) { if (parts.length == 1) {
throw new Error(`Invalid flavor entry: ${input}`); throw new Error(`Invalid flavor entry: ${input}`);
} }
switch (parts[0]) { const key = parts[0].toLowerCase();
const value = parts[1];
switch (key) {
case 'latest': { case 'latest': {
flavor.latest = parts[1]; flavor.latest = value;
if (!['auto', 'true', 'false'].includes(flavor.latest)) { if (!['auto', 'true', 'false'].includes(flavor.latest)) {
throw new Error(`Invalid latest flavor entry: ${input}`); throw new Error(`Invalid latest flavor entry: ${input}`);
} }
break; break;
} }
case 'prefix': { case 'prefix': {
flavor.prefix = parts[1]; flavor.prefix = value;
onlatestfor = 'prefix'; onlatestfor = 'prefix';
break; break;
} }
case 'suffix': { case 'suffix': {
flavor.suffix = parts[1]; flavor.suffix = value;
onlatestfor = 'suffix'; onlatestfor = 'suffix';
break; break;
} }
case 'onlatest': { case 'onlatest': {
if (!['true', 'false'].includes(parts[1])) { if (!['true', 'false'].includes(value)) {
throw new Error(`Invalid value for onlatest attribute: ${parts[1]}`); throw new Error(`Invalid value for onlatest attribute: ${value}`);
} }
switch (onlatestfor) { switch (onlatestfor) {
case 'prefix': { case 'prefix': {
flavor.prefixLatest = /true/i.test(parts[1]); flavor.prefixLatest = /true/i.test(value);
break; break;
} }
case 'suffix': { case 'suffix': {
flavor.suffixLatest = /true/i.test(parts[1]); flavor.suffixLatest = /true/i.test(value);
break; break;
} }
} }

View file

@ -93,12 +93,15 @@ export function Parse(s: string): Tag {
const tag = new Tag(); const tag = new Tag();
for (const field of fields) { for (const field of fields) {
const parts = field.toString().split('=', 2); const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) { if (parts.length == 1) {
tag.attrs['value'] = parts[0].trim(); tag.attrs['value'] = parts[0];
} else { } else {
const key = parts[0].trim().toLowerCase(); const key = parts[0].toLowerCase();
const value = parts[1].trim(); const value = parts[1];
switch (key) { switch (key) {
case 'type': { case 'type': {
if (!Object.values(Type).includes(value)) { if (!Object.values(Type).includes(value)) {