Home > OS >  yq json to yaml add quotes to docker compose labels
yq json to yaml add quotes to docker compose labels

Time:12-28

I'm trying to create a docker compose override YAML file from JSON, which includes N amount of Traefik labels. I currently have the following setup:

{
  "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"
    ]
  }
}

with currently the following command:

yq e -P docker-compose.override.json

Which results in:

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

But I'd like to have double quotes around the traefik labels like so:

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"

How would I achieve this? I'm fine with possibly editing the JSON, but I would prefer a better use of yq instead.

CodePudding user response:

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

produces the desired response.

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"

Explanation

  • ... style="" sets pretty print style for all nodes.
  • with(.traefik.labels[] ; . style="double") sets styling with double quotes for .traefik.labels only

Or justs use yq e '... style="double"' docker-compose.override.json to enclose the keys into double quotes also.

  • Related