Hi,
I'm starting with Clojure, better yet, re-starting after trying it several years ago.
There are some new tools like Babashka that I'm trying to use for this.
The challenge is to do the following:
Start ansible virtual environment
Run an external process with 'ansible-inventory' against one host
at a time, or all hosts, let's see...
The interesting part of the output of 'ansible-inventory' goes to
stderr, so read it.
It consists of lines of text and then some JSON.
Parse the JSON contents.
It will be more elaborate than this, but it's enough for the case.
Let's forget about 1.
Instead of 2., just do a simple 'cat a.txt' file built with some text and some JSON like
line1
line 2
{
'field': 'value'
}
What I've been able to build was this:
#!/usr/bin/env bb
(require '[babashka.process :refer [shell]])
(require '[cheshire.core :as json])
(require '[clojure.string :as str])
(-> (shell {:out :string} "cat" "a.txt")
:out json/parse-string)
What I'do in an imperative language, that I use to use would be:
Read line by line. In Clojure (str/split-lines)
Discard any line until a line consisiting only of a '{' is found.
From there, accumulate all lines in a string until a '}' is found.
Parse the JSON from that string.
How can I approach this in Clojure?