【问题标题】:yq json to yaml add quotes to docker compose labelsyq json 到 yaml 为 docker compose 标签添加引号
【发布时间】:2021-12-26 20:46:44
【问题描述】:

我正在尝试从 JSON 创建一个 docker compose 覆盖 YAML 文件,其中包括 N 个 Traefik 标签。我目前有以下设置:

{
  "version": "3.7",
  "services": {
    "traefik": {
      "labels": [
        "traefik.http.routers.traefik-secure.tls.domains[0].main=domain1.local",
        "traefik.http.routers.traefik-secure.tls.domains[0].sans=*.domain1.local",
        "traefik.http.routers.traefik-secure.tls.domains[1].main=domain2.local",
        "traefik.http.routers.traefik-secure.tls.domains[1].sans=*.domain2.local",
        "traefik.http.routers.traefik-secure.tls.domains[N].main=domainN.local",
        "traefik.http.routers.traefik-secure.tls.domains[N].sans=*.domainN.local"
      ]
    }
  }
}

目前使用以下命令:

yq e -P docker-compose.override.json

结果:

version: "3.7"
services:
  traefik:
    labels:
      - traefik.http.routers.traefik-secure.tls.domains[0].main=domain1.local
      - traefik.http.routers.traefik-secure.tls.domains[0].sans=*.domain1.local
      - traefik.http.routers.traefik-secure.tls.domains[1].main=domain2.local
      - traefik.http.routers.traefik-secure.tls.domains[1].sans=*.domain2.local
      - traefik.http.routers.traefik-secure.tls.domains[N].main=domainN.local
      - traefik.http.routers.traefik-secure.tls.domains[N].sans=*.domainN.local

但我想在 traefik 标签周围加上双引号,如下所示:

version: "3.7"
services:
  traefik:
    labels:
      - "traefik.http.routers.traefik-secure.tls.domains[0].main=domain1.local"
      - "traefik.http.routers.traefik-secure.tls.domains[0].sans=*.domain1.local"
      - "traefik.http.routers.traefik-secure.tls.domains[1].main=domain2.local"
      - "traefik.http.routers.traefik-secure.tls.domains[1].sans=*.domain2.local"
      - "traefik.http.routers.traefik-secure.tls.domains[N].main=domainN.local"
      - "traefik.http.routers.traefik-secure.tls.domains[N].sans=*.domainN.local"

我将如何实现这一目标?我可以编辑 JSON,但我希望更好地使用 yq

【问题讨论】:

  • 首先,yaml 是 json 的超集,因此您可以继续使用 docker compose 运行您的 json 文件。其次,yaml 中的引号是可选的。只有当您想要一个数字或一些特殊值作为字符串时,您才需要引用它们。因此,不引用它们不会对您造成伤害。

标签: json docker-compose yq


【解决方案1】:

yq e '... style="" | with(.traefik.labels[] ; . style="double")' docker-compose.override.json

产生所需的响应。

version: "3.7"
traefik:
  labels:
    - "traefik.http.routers.traefik-secure.tls.domains[0].main=domain1.local"
    - "traefik.http.routers.traefik-secure.tls.domains[0].sans=*.domain1.local"
    - "traefik.http.routers.traefik-secure.tls.domains[1].main=domain2.local"
    - "traefik.http.routers.traefik-secure.tls.domains[1].sans=*.domain2.local"
    - "traefik.http.routers.traefik-secure.tls.domains[N].main=domainN.local"
    - "traefik.http.routers.traefik-secure.tls.domains[N].sans=*.domainN.local"

说明

  • ... style="" 为所有节点设置漂亮的打印样式。
  • with(.traefik.labels[] ; . style="double") 仅为 .traefik.labels 设置带双引号的样式

或者也可以使用yq e '... style="double"' docker-compose.override.json 将键括在双引号中。

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多