我需要生成一个简单且标准的 docker compose 内容并用它更新现有的 docker compose 文件。 这个 yaml 解析器看起来不错,但无法弄清楚它在输出中生成的问号。
这是我的 docker compose 模板文件内容:
version: "3.9"
name: "org"
services:
memory:
image: redis/redis-stack:7.2.0-v7
# ...
networks:
default:
name: org
external: true
我正在尝试向其中添加新服务:
import { readFile, writeFile } from 'node:fs/promises'
import { parseDocument } from 'yaml'
const doc = parseDocument(await readFile('./docker-compose.yml', 'utf8'))
doc.addIn(['services'], {
'my-service': {
'image': '${CONTAINER_CONN_STR}:${APP_VERSION:-latest}',
'ports': ['${GDEVOPS_APP_PORT}:${APP_PORT}']
}
})
await writeFile('./sample.yml', String(doc))
输出的sample.yaml看起来像这样:
version: "3.9"
name: "org"
services:
memory:
image: redis/redis-stack:7.2.0-v7
# ...
? my-service:
image: ${CONTAINER_CONN_STR}:${APP_VERSION:-latest}
ports:
- ${GDEVOPS_APP_PORT}:${APP_PORT}
networks:
default:
name: org
external: true
如何去掉服务前面的问号?
将选项
{simpleKeys: true}
传递给字符串生成,根据您的情况:
await writeFile('./sample.yml', String(doc, {simpleKeys: true}))
来自文档:
simpleKeys boolean false Require keys to be scalars and always use implicit rather than explicit notation.