Skip to content

Instantly share code, notes, and snippets.

@nilium nilium/kv
Last active Dec 28, 2018

Embed
What would you like to do?
A few shell scripts for creating JSON from a shell
#!/usr/bin/env bash
while [ $# -gt 0 ]; do
jq -nc --arg k "$1" --arg v "$2" '{($k): (try ($v|fromjson) catch $v)}'
shift; shift
done | jq -sc add

The following command can be used to construct a simple Marathon application definition:

$ kv                                              \
    id python-server                              \
    cmd 'python3 -m http.server 8080'             \
    cpus 0.1                                      \
    mem 64                                        \
    instances 1                                   \
    container "$(kv                               \
        type DOCKER                               \
        docker "$(kv image python:3)"             \
        portMappings "$(list                      \
            "$(kv containerPort 8080 hostPort 0)" \
        )"                                        \
    )" |
    jq . # kv/list/etc. compact output -- pretty print this

This produces the following output (including the pretty-printing above):

{
  "id": "python-server",
  "cmd": "python3 -m http.server 8080",
  "cpus": 0.1,
  "mem": 64,
  "instances": 1,
  "container": {
    "type": "DOCKER",
    "docker": {
      "image": "python:3"
    },
    "portMappings": [
      {
        "containerPort": 8080,
        "hostPort": 0
      }
    ]
  }
}
#!/usr/bin/env bash
while [ $# -gt 0 ]; do
jq -nc --arg v "$1" '[(try ($v|fromjson) catch $v)]'
shift
done | jq -sc add
#!/usr/bin/env bash
exec jq -nc --arg v "$*" '$v'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.