babashka/examples/portal.clj

55 lines
1.3 KiB
Clojure
Raw Normal View History

2020-10-17 10:06:33 +00:00
#!/usr/bin/env bb
(ns portal
2021-01-27 12:07:09 +00:00
(:require [babashka.deps :as deps]
2020-10-17 10:06:33 +00:00
[cheshire.core :as json]
[clj-yaml.core :as yaml]
[clojure.data.xml :as xml]
[clojure.edn :as edn]
[clojure.string :as str]))
2021-01-27 12:07:09 +00:00
(deps/add-deps '{:deps {djblue/portal {:mvn/version "0.9.0"}}})
(require '[portal.api :as p])
(.addShutdownHook (Runtime/getRuntime)
(Thread. (fn [] (p/close))))
2020-10-17 10:06:33 +00:00
(def file (first *command-line-args*))
(when-not file
(binding [*out* *err*]
2020-10-17 10:17:17 +00:00
(println "Usage: portal.clj <file.(edn|json|xml|yaml)>"))
2020-10-17 10:15:50 +00:00
(System/exit 1))
2020-10-17 10:06:33 +00:00
(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))
2020-10-17 10:15:50 +00:00
(def extension (last (str/split file #"\.")))
(def contents (slurp file))
2020-10-17 10:06:33 +00:00
(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))))
2020-10-17 10:15:50 +00:00
(p/open)
(p/tap)
2020-10-17 10:06:33 +00:00
(tap> data)
@(promise)