Portal example [skip ci]

This commit is contained in:
Michiel Borkent 2020-10-17 12:06:33 +02:00
parent b61002d5df
commit 5543feeb2b
3 changed files with 65 additions and 26 deletions

View file

@ -313,8 +313,14 @@ META-INF/leiningen/borkdude/sci/project.clj
See [examples/vim.clj](examples/vim.clj).
### YAML inspector
### Portal
This script uses [djblue/portal](https://github.com/djblue/portal/) for viewing a YAML file piped to stdin.
This script uses [djblue/portal](https://github.com/djblue/portal/) for inspecting EDN, JSON, XML or YAML files.
See [examples/yaml_inspector.clj](yaml_inspector.clj).
Examples usage:
``` shell
$ examples/yaml_inspector.clj ~/git/clojure/pom.xml
```
See [portal.clj](portal.clj).

56
examples/portal.clj Executable file
View file

@ -0,0 +1,56 @@
#!/usr/bin/env bb
(ns portal
(:require [babashka.classpath :as cp]
[cheshire.core :as json]
[clj-yaml.core :as yaml]
[clojure.data.xml :as xml]
[clojure.edn :as edn]
[clojure.java.shell :refer [sh]]
[clojure.string :as str]))
(def cp (str/trim (:out (sh "clojure" "-Spath" "-Sdeps" "{:deps {djblue/portal {:mvn/version \"0.6.1\"}}}"))))
(cp/add-classpath cp)
(require '[portal.api :as p])
(.addShutdownHook (Runtime/getRuntime)
(Thread. (fn [] (p/close))))
(p/open)
(p/tap)
(def file (first *command-line-args*))
(when-not file
(binding [*out* *err*]
"Usage: portal.clj <file.(edn|json|xml|yaml)>"))
(def extension (last (str/split file #"\.")))
(def contents (slurp file))
(defn xml->hiccup [xml]
(if-let [t (:tag xml)]
(let [elt [t]
elt (if-let [attrs (:attr xml)]
(conj elt attrs)
elt)]
(into elt (map xml->hiccup (:content xml))))
xml))
(def data (case extension
("edn")
(edn/read-string contents)
("json")
(json/parse-string contents)
("yml" "yaml")
(yaml/parse-string contents)
("xml")
(-> (xml/parse-str contents
:skip-whitespace true
:namespace-aware false)
(xml->hiccup))))
(tap> data)
@(promise)

View file

@ -1,23 +0,0 @@
#!/usr/bin/env bb
(ns yaml-inspector
(:require [babashka.classpath :as cp]
[clj-yaml.core :as yaml]
[clojure.java.shell :refer [sh]]
[clojure.string :as str]))
(def cp (str/trim (:out (sh "clojure" "-Spath" "-Sdeps" "{:deps {djblue/portal {:mvn/version \"0.6.1\"}}}"))))
(cp/add-classpath cp)
(require '[portal.api :as p])
(p/open)
(p/tap)
(def yaml (yaml/parse-string (slurp *in*)))
(tap> yaml)
(.addShutdownHook (Runtime/getRuntime)
(Thread. (fn [] (p/close))))
@(promise)