More complex inheritance in YAML?

后端 未结 3 1998
南方客
南方客 2021-01-31 02:00

YAML has inheritance. The most clear example I have ever seen is here: http://blog.101ideas.cz/posts/dry-your-yaml-files.html

I need something more complex: I need to ov

3条回答
  •  悲哀的现实
    2021-01-31 02:20

    For this sort of problems, I have created a tool: jq-front. By using yq + jq-front, you can achieve it by slightly modifying your input.

    in.yaml:

    $local: 
      database_default:
        server:
          ip: 192.168.1.5
          port: 2000
        db_name: test
        user: 
          name: root
          password: root
    
    # database foo differs from default by only its port and user password
    foo_database:
      $extends: [ database_default ]
      server:
        port: 2001
      db_name: foo
      user:
        password: foo_root
    

    And you can process this file by a following command line.

    $ yq . -j in.yaml | jq-front  | yq . -y
    

    And you will get following output that you wanted.

    foo_database:
      server:
        ip: 192.168.1.5
        port: 2001
      db_name: foo
      user:
        name: root
        password: foo_root
    

    NOTE: jq-front is very slow. On my machine the command took 2.5s, which did not matter to me too much since system configuration can be read once and only the converted file is used by the rest of my program.

    NOTE: If you use docker + bash, it's lot easier to install jq-front by docker. You only need to add following function to your .bashrc or a file that is sourced by it.

    function jq-front() {
      docker run --rm -i \
        -v /:/var/lib/jf \
        -e JF_PATH_BASE="/var/lib/jf" \
        -e JF_PATH="${JF_PATH}" \
        -e JF_DEBUG=${JF_DEBUG:-disabled} \
        -e JF_CWD="$(pwd)" \
        dakusui/jq-front:"${JF_DOCKER_TAG:-latest}" "${@}"
    }
    

提交回复
热议问题