dakrone / es-mode

An Emacs major mode for interacting with Elasticsearch
GNU General Public License v3.0
195 stars 34 forks source link

Org mode tangle not generating correct shell #66

Open jamiguet opened 6 years ago

jamiguet commented 6 years ago

The following code inside a org mode buffer does what it should. Make the request and then run the outpu json through jq and outpu in the buffer correctly formated json.

#+name: index_sizes
#+begin_src es :url localhost:9200 :var index_name="my-index-*" :jq "[.indices | to_entries[] | {\"index\": .key, \"bytes\": .value.primaries.store.size_in_bytes, \"count\": .value.primaries.docs.count, \"deleted_count\": .value.primaries.docs.deleted } ]| sort_by(.index)[]" :tangle ~/tooling/index_status.sh :shebang #!/bin/bash
GET /${index_name}/_stats
{}
#+end_src

When tangled this is the output:

#!/bin/bash
curl -XPOST localhost:9200 -d "GET /my-index-*/_stats
{}"

Which when executed does not work.

The situation can be improved if the block is modified in the following way

#+caption: Per index statistics
#+name: index_sizes
#+begin_src es :method GET :url localhost:9200/${index_name}/_stats :var index_name="my-index-*" :jq "[.indices | to_entries[] | {\"index\": .key, \"bytes\": .value.primaries.store.size_in_bytes, \"count\": .value.primaries.docs.count, \"deleted_count\": .value.primaries.docs.deleted } ]| sort_by(.index)[]" :tangle ~/tooling/index_status.sh :shebang #!/bin/bash
{}
#+end_src

But then variables are not replaced nor declared in the script.

#!/bin/bash
curl -XGET localhost:9200/${index_name}/_stats -d "{}"

Lastly the :jq command is also ignored.

Ideally the tangling of both blocks should be:

#!/bin/bash
index_name="my-index-*"
jq_cmd='[.indices | to_entries[] | {"index": .key, "bytes": .value.primaries.store.size_in_bytes, "count": .value.primaries.docs.count, "deleted_count": .value.primaries.docs.deleted } ]| sort_by(.index)[]'
echo ${jq_cmd}
curl -XGET localhost:9200/${index_name}/_stats -H Content-Type\:\ application/json -d "{}" | jq -c "${jq_cmd}"

Or follow some sort of pattern along those lines.

This would enable for the org mode document to be a prototyping environment where code and the reason why its used can live together (published as documentation) and tangled to produce running scripts.

dakrone commented 6 years ago

@jamiguet thanks for opening this, I agree it would be if tangling created a file that would generate the same response. I'll add this to the TODO list

jamiguet commented 6 years ago

It may help to reuse restclient mode as part of the fix as that mode incorporates the parsing and tangling functionality for rest calls in a very simple way.

If you don't have much time let me know I can assist and make a pull request.