Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Improve input template API yaml comments #199168

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@
"show_user": false,
"default": "20s"
},
{
"name": "tags",
"type": "text",
"title": "Tags",
"multi": true,
"required": false,
"show_user": false,
"default": ""
},
{
"name": "maxconn",
"type": "integer",
Expand Down Expand Up @@ -203,6 +212,15 @@
{
"input": "redis/metrics",
"vars": [
{
"name": "tags_streams",
"type": "text",
"title": "Tags in streams",
"multi": true,
"required": false,
"show_user": false,
"default": ""
},
{
"name": "key.patterns",
"type": "yaml",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ period: {{period}}
processors:
{{processors}}
{{/if}}
tags:
- test
{{#each tags as |tag i|}}
- {{tag}}
{{/each}}
tags_streams:
{{#each tags_streams as |tag i|}}
- {{tag}}
{{/each}}
`),
],
]);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,7 @@ function addCommentsToYaml(
pkgInput.description ? `: ${pkgInput.description}` : ''
}`;

yamlDoc.visit(inputItem, {
Scalar(key, node) {
if (node.value) {
const val = node.value.toString();
for (const varDef of pkgInput.vars ?? []) {
const placeholder = getPlaceholder(varDef);
if (val.includes(placeholder)) {
node.comment = ` ${varDef.title}${
varDef.description ? `: ${varDef.description}` : ''
}`;
}
}
}
},
});
commentVariablesInYaml(inputItem, pkgInput.vars ?? []);

const yamlStreams = inputItem.get('streams');
if (!yamlDoc.isCollection(yamlStreams)) {
Expand All @@ -300,21 +286,7 @@ function addCommentsToYaml(
streamItem.commentBefore = ` ${pkgStream.title}${
pkgStream.description ? `: ${pkgStream.description}` : ''
}`;
yamlDoc.visit(streamItem, {
Scalar(key, node) {
if (node.value) {
const val = node.value.toString();
for (const varDef of pkgStream.vars ?? []) {
const placeholder = getPlaceholder(varDef);
if (val.includes(placeholder)) {
node.comment = ` ${varDef.title}${
varDef.description ? `: ${varDef.description}` : ''
}`;
}
}
}
},
});
commentVariablesInYaml(streamItem, pkgStream.vars ?? []);
}
}
});
Expand All @@ -324,3 +296,71 @@ function addCommentsToYaml(

return doc.toString();
}

function commentVariablesInYaml(rootNode: yamlDoc.Node, vars: RegistryVarsEntry[] = []) {
// Node need to be deleted after the end of the visit to be able to visit every node
const toDeleteFn: Array<() => void> = [];
yamlDoc.visit(rootNode, {
Scalar(key, node, path) {
if (node.value) {
const val = node.value.toString();
for (const varDef of vars) {
const placeholder = getPlaceholder(varDef);
if (val.includes(placeholder)) {
node.comment = ` ${varDef.title}${varDef.description ? `: ${varDef.description}` : ''}`;

const paths = [...path].reverse();

let prevPart: yamlDoc.Node | yamlDoc.Document | yamlDoc.Pair = node;

for (const pathPart of paths) {
if (yamlDoc.isCollection(pathPart)) {
// If only one items in the collection comment the whole collection
if (pathPart.items.length === 1) {
continue;
}
}
if (yamlDoc.isSeq(pathPart)) {
const commentDoc = new yamlDoc.Document(new yamlDoc.YAMLSeq());
commentDoc.add(prevPart);
const commentStr = commentDoc.toString().trimEnd();
pathPart.comment = pathPart.comment
? `${pathPart.comment} ${commentStr}`
: ` ${commentStr}`;
const keyToDelete = prevPart;

toDeleteFn.push(() => {
pathPart.items.forEach((item, index) => {
if (item === keyToDelete) {
pathPart.delete(new yamlDoc.Scalar(index));
}
});
});
return;
}

if (yamlDoc.isMap(pathPart)) {
if (yamlDoc.isPair(prevPart)) {
const commentDoc = new yamlDoc.Document(new yamlDoc.YAMLMap());
commentDoc.add(prevPart);
const commentStr = commentDoc.toString().trimEnd();

pathPart.comment = pathPart.comment
? `${pathPart.comment}\n ${commentStr.toString()}`
: ` ${commentStr.toString()}`;
const keyToDelete = prevPart.key;
toDeleteFn.push(() => pathPart.delete(keyToDelete));
}
return;
}

prevPart = pathPart;
}
}
}
}
},
});

toDeleteFn.forEach((deleteFn) => deleteFn());
}