我正在寻找可以将程序中的运行时参数解析为 yaml 文件的 Node 模块(或其他东西)。
例如在 kubernetes yaml 中
metadata:
name: $PROJECT_NAME
labels:
service: $SERVICE_NAME
system: $SYSTEM_ID
app_version: $PROJECT_VERSION
tier: app
有一种很好的方法来构建新的 yaml 或更改包含我所有参数值的现有 yaml 吗?
最佳答案
我决定使用Handlebars module 只需向函数提供一个包含我想要解析的参数的模板,该函数将创建包含我所有更改的新文件
const Handlebars = require('handlebars');
const source = fs.readFileSync(`${cwd}/${file}`).toString();
const template = Handlebars.compile(source);
const contents = template({ PROJECT_NAME: `${name}`, PROJECT_VERSION: `${version}`, DOCKER_IMAGE: `${image}` });
fs.writeFileSync(`${cwd}/target/${file}`, contents);
console.log(`${file} -- Finish parsing YAML.`);
JSON 看起来像
spec:
containers:
- name: {{PROJECT_NAME}}:{{PROJECT_VERSION}}
resources:
limits:
memory: "1Gi"
cpu: "1"
image: {{DOCKER_IMAGE}}
关于node.js - Nodejs 模板 yaml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49405550/