mirror of https://github.com/portainer/portainer
refactor(custom-templates): return parse error
parent
3810809444
commit
578f3d34a1
|
@ -83,7 +83,7 @@ class KubeCreateCustomTemplateViewController {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const variables = getTemplateVariables(templateStr);
|
const [variables] = getTemplateVariables(templateStr);
|
||||||
|
|
||||||
const isValid = !!variables;
|
const isValid = !!variables;
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ class KubeEditCustomTemplateViewController {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const variables = getTemplateVariables(templateStr);
|
const [variables] = getTemplateVariables(templateStr);
|
||||||
|
|
||||||
const isValid = !!variables;
|
const isValid = !!variables;
|
||||||
|
|
||||||
|
|
|
@ -206,7 +206,7 @@ class CreateCustomTemplateViewController {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const variables = getTemplateVariables(templateStr);
|
const [variables] = getTemplateVariables(templateStr);
|
||||||
|
|
||||||
const isValid = !!variables;
|
const isValid = !!variables;
|
||||||
|
|
||||||
|
|
|
@ -178,7 +178,7 @@ class EditCustomTemplateViewController {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const variables = getTemplateVariables(templateStr);
|
const [variables] = getTemplateVariables(templateStr);
|
||||||
|
|
||||||
const isValid = !!variables;
|
const isValid = !!variables;
|
||||||
|
|
||||||
|
|
|
@ -4,31 +4,40 @@ import Mustache from 'mustache';
|
||||||
import { VariableDefinition } from './CustomTemplatesVariablesDefinitionField/CustomTemplatesVariablesDefinitionField';
|
import { VariableDefinition } from './CustomTemplatesVariablesDefinitionField/CustomTemplatesVariablesDefinitionField';
|
||||||
|
|
||||||
export function getTemplateVariables(templateStr: string) {
|
export function getTemplateVariables(templateStr: string) {
|
||||||
const template = validateAndParse(templateStr);
|
const [template, error] = validateAndParse(templateStr);
|
||||||
|
|
||||||
if (!template) {
|
if (!template) {
|
||||||
return null;
|
return [null, error] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
return template
|
return [
|
||||||
|
template
|
||||||
.filter(([type, value]) => type === 'name' && value)
|
.filter(([type, value]) => type === 'name' && value)
|
||||||
.map(([, value]) => ({
|
.map(([, value]) => ({
|
||||||
name: value,
|
name: value,
|
||||||
label: '',
|
label: '',
|
||||||
defaultValue: '',
|
defaultValue: '',
|
||||||
description: '',
|
description: '',
|
||||||
}));
|
})),
|
||||||
|
null,
|
||||||
|
] as const;
|
||||||
}
|
}
|
||||||
|
type TemplateSpans = ReturnType<typeof Mustache.parse>;
|
||||||
function validateAndParse(templateStr: string) {
|
function validateAndParse(
|
||||||
|
templateStr: string
|
||||||
|
): readonly [TemplateSpans, null] | readonly [null, string] {
|
||||||
if (!templateStr) {
|
if (!templateStr) {
|
||||||
return [];
|
return [[] as TemplateSpans, null] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Mustache.parse(templateStr);
|
return [Mustache.parse(templateStr), null] as const;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null;
|
if (!(e instanceof Error)) {
|
||||||
|
return [null, 'Parse error'] as const;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [null, e.message] as const;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue