Consul has been my go-to for service discovery and configuration management for a long time. I’ve primarily been using it to store JSON formatted configuration and data, but I’ve found myself writing the same code every place I go: Patch the JSON value at a specific key, but safely using CAS.

I’ve finally gotten around to formalizing a tool and open sourcing it: ngerakines/consul-patch-json.

This small tool does exactly what it says: It applies some JSON changes to a Consul key value. The README has a handful of examples and the integration test script has some practical examples that combine jq and jo.

$ consul kv put apps/foo/config '{"version": "1.0.0"}'
$ consul-patch-json apps/foo/config description='"My app"'
$ consul kv get apps/foo/config
{"description":"My app","version":"1.0.0"}

Thanks to the json-patch crate, it supports attribute additions and replace, as well as JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7396).

$ consul kv put apps/foo/config '{"version": "1.0.0","features":["coffee"]}'
$ cat > patch.json <<EOF
[
    {"op": "test", "path": "/version", "value": "1.0.0"},
    {"op": "add","path": "/features/0", "value": "metrics"}
]
EOF
$ cat patch.json | consul-patch-json apps/foo/config --json-patch --
$ consul kv get apps/foo/config
{"features":["coffee","metrics"],"version":"1.0.0"}

It’s open source under the MIT license.