Skip to content

Commit

Permalink
Babashka pod support for freeze/thaw from files
Browse files Browse the repository at this point in the history
  • Loading branch information
justone committed Jun 22, 2020
1 parent 1d2d713 commit 0f6cc4a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,25 @@ If input or output is not specified, stdin or stdout will be used:
cat data.edn | brisk -f | brisk -t > data2.edn
```

# To Do
# [Babashka pod](https://github.com/babashka/babashka.pods) support

* [Babashka pod](https://github.com/babashka/babashka.pods) support, exposing nippy functions.
There are two functions exposed via the pod interface:

* `(freeze-to-file filename data)` - returns the number of bytes written
* `(thaw-from-file filename)` - returns data thawed from the file

Example:

```
#!/usr/bin/env bb
(require '[babashka.pods :as pods])
(pods/load-pod "brisk")
(require '[pod.brisk :as brisk])
(brisk/freeze-to-file "pod.nippy" {:han :solo})
(prn (brisk/thaw-from-file "pod.nippy"))
```

# Things that don't work

Expand Down
4 changes: 3 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
:main-opts ["-m" "kaocha.runner"]}}

:deps
{com.taoensso/nippy {:mvn/version "2.14.0"}
{bb-pod-racer {:git/url "git@github.com:justone/bb-pod-racer.git"
:sha "7d7784a72324bf7e8ae0cca9ebc93dde24d92fb9"}
com.taoensso/nippy {:mvn/version "2.14.0"}
org.clojure/tools.cli {:mvn/version "1.0.194"}}

:paths ["src" "resources"]}
23 changes: 17 additions & 6 deletions src/brisk/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[clojure.string :as string]
[clojure.tools.cli :refer [parse-opts]]

[pod-racer.core :as pod]
[taoensso.nippy :as nippy]

[brisk.lib.opts :as opts]
Expand Down Expand Up @@ -91,12 +92,22 @@
(and (not (:thaw options)) (not (:freeze options)))
{:exit 1}))))

(def pod-config
{:pod/namespaces
[{:pod/ns "pod.brisk"
:pod/vars [{:var/name "freeze-to-file"
:var/fn #(count (nippy/freeze-to-file %1 %2))}
{:var/name "thaw-from-file"
:var/fn nippy/thaw-from-file}]}]})

(defn -main [& args]
(let [parsed (parse-opts args cli-options)
{:keys [options]} parsed]
(or (when-some [errors (find-errors parsed)]
(->> (opts/format-help progname help parsed errors)
(opts/print-and-exit)))
(cond
(:freeze options) (freeze options)
(:thaw options) (thaw options)))))
(if (System/getenv "BABASHKA_POD")
(pod/launch pod-config)
(or (when-some [errors (find-errors parsed)]
(->> (opts/format-help progname help parsed errors)
(opts/print-and-exit)))
(cond
(:freeze options) (freeze options)
(:thaw options) (thaw options))))))

0 comments on commit 0f6cc4a

Please sign in to comment.