skip to Main Content

This is my docker-compose file:

{
  "name": "movies-docker-test",
  "services": {
    "movies-rp": {
      "volumes": [
        {
          "type": "volume",
          "source": "certs",
          "target": "/etc/nginx/certs",
        },
        {
          "type": "bind",
          "source": "/home/nblanchet/jeudi_infra/kubernetes/okd/movies-docker-test/reverse-proxy/wikibase.conf.template",
          "target": "/etc/nginx/templates/wikibase.conf.template"
        }
      ]
    }
  }
}

I’d like to update this file so that each volumes of each service to be sorted by the ‘.type’:

{
  "name": "movies-docker-test",
  "services": {
    "movies-rp": {
      "volumes": [
        {
          "type": "bind",
          "source": "/home/nblanchet/jeudi_infra/kubernetes/okd/movies-docker-test/reverse-proxy/wikibase.conf.template",
          "target": "/etc/nginx/templates/wikibase.conf.template"
        },
        {
          "type": "volume",
          "source": "certs",
          "target": "/etc/nginx/certs"
        }
      ]
    }
  

I managed to do this, but the file is not updated:

jq  '[.services[].volumes[]?]|sort_by(.type)'

2

Answers


  1. Chosen as BEST ANSWER

    here is what I successfully tested with ''yq'' and the wrapped ''jq'' syntax

    yq '.services|=with_entries(.value|=(select(has("volumes")).volumes |= sort_by((.type)) ))' docker-compose.yml
    

    or what I initially did with regular ''jq''

    cat movies.yml | yq eval -ojson |  jq '.services|=with_entries(.value|=(select(has("volumes")).volumes |= sort_by((.type)) ))' | yq -P
    

  2. Given file:

    name: movies-docker-test
    services:
      movies-rp:
        volumes:
          - type: volume
            source: conf
            target: /etc/nginx/conf.d
          - type: volume
            source: certs
            target: /etc/nginx/certs
            read_only: true
          - type: bind
            source: /home/nblanchet/jeudi_infra/kubernetes/okd/movies-docker-test/reverse-proxy/uploadsize.conf
            target: /etc/nginx/conf.d/uploadsize.conf
          - type: bind
            source: /home/nblanchet/jeudi_infra/kubernetes/okd/movies-docker-test/reverse-proxy/wikibase.conf.template
            target: /etc/nginx/templates/wikibase.conf.template
    

    then, using yq 4.40.3:

    $ yq e 'select(.services.*.volumes | select(tag == "!!seq")|=sort_by(.type))' file
    
    name: movies-docker-test
    services:
      movies-rp:
        volumes:
          - type: bind
            source: /home/nblanchet/jeudi_infra/kubernetes/okd/movies-docker-test/reverse-proxy/uploadsize.conf
            target: /etc/nginx/conf.d/uploadsize.conf
          - type: bind
            source: /home/nblanchet/jeudi_infra/kubernetes/okd/movies-docker-test/reverse-proxy/wikibase.conf.template
            target: /etc/nginx/templates/wikibase.conf.template
          - type: volume
            source: conf
            target: /etc/nginx/conf.d
          - type: volume
            source: certs
            target: /etc/nginx/certs
            read_only: true
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search