Files
nomad/ui/app/utils/json-to-hcl.js
Juana De La Cuesta c21dfdb17a [gh-476] Sanitise HCL variables before storing on job submission (#24423)
* func: User url rules to scape non alphanumeric values in hcl variables

* docs: add changelog

* func: unscape flags before returning

* use JSON.stringify instead of bespoke value quoting to handle in-value-multi-line cases

---------

Co-authored-by: Phil Renaud <phil@riotindustries.com>
2024-11-22 11:45:02 +01:00

26 lines
552 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
// @ts-check
/**
* Convert a JSON object to an HCL string.
*
* @param {Object} obj - The JSON object to convert.
* @returns {string} The HCL string representation of the JSON object.
*/
export default function jsonToHcl(obj) {
const hclLines = [];
for (const key in obj) {
const value = obj[key];
const hclValue = typeof value === 'string' ? JSON.stringify(value) : value;
hclLines.push(`${key}=${hclValue}\n`);
}
return hclLines.join('\n');
}