From 0f6cc4a969e9eec4895a7c13137adbd40b0b2948 Mon Sep 17 00:00:00 2001 From: Nate Jones Date: Sat, 20 Jun 2020 21:47:25 -0700 Subject: [PATCH] Babashka pod support for freeze/thaw from files --- README.md | 20 ++++++++++++++++++-- deps.edn | 4 +++- src/brisk/main.clj | 23 +++++++++++++++++------ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cf9296b..0ef6a79 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/deps.edn b/deps.edn index e7ba99a..ce555d9 100644 --- a/deps.edn +++ b/deps.edn @@ -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"]} diff --git a/src/brisk/main.clj b/src/brisk/main.clj index 4789ef2..6cc1092 100644 --- a/src/brisk/main.clj +++ b/src/brisk/main.clj @@ -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] @@ -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))))))