Created
May 8, 2020 10:25
-
-
Save jeroenvandijk/b63bc62ea150ec1969edb5dfd0754e0e to your computer and use it in GitHub Desktop.
Babashka spire pod wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bb | |
user=> (babashka.pods/load-pod "./simple-pod.bb") | |
nil | |
user=> (require '[pod.babashka.spire :as spire]) | |
nil | |
user=> (spire/magic! ) | |
java.lang.NullPointerException | |
#error { | |
:cause "Invalid netstring. Unexpected end of input." | |
:via | |
[{:type java.io.EOFException | |
:message "Invalid netstring. Unexpected end of input." | |
:at [babashka.impl.bencode.core$read_byte invokeStatic "core.clj" 90]}] | |
:trace | |
[[babashka.impl.bencode.core$read_byte invokeStatic "core.clj" 90] | |
[babashka.impl.bencode.core$read_token invokeStatic "core.clj" 239] | |
[babashka.impl.bencode.core$read_bencode invokeStatic "core.clj" 256] | |
[babashka.impl.pods$read invokeStatic "pods.clj" 21] | |
[babashka.impl.pods$processor invokeStatic "pods.clj" 35] | |
[babashka.impl.pods$load_pod$fn__14730 invoke "pods.clj" 147] | |
[sci.impl.vars$binding_conveyor_fn$fn__7579 invoke "vars.cljc" 142] | |
[clojure.core$binding_conveyor_fn$fn__5758 invoke "core.clj" 2032] | |
[clojure.lang.AFn call "AFn.java" 18] | |
[java.util.concurrent.FutureTask run "FutureTask.java" 266] | |
[java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1149] | |
[java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 624] | |
[java.lang.Thread run "Thread.java" 748] | |
[com.oracle.svm.core.thread.JavaThreads threadStartRoutine "JavaThreads.java" 497] | |
[com.oracle.svm.core.posix.thread.PosixJavaThreads pthreadStartRoutine "PosixJavaThreads.java" 193]]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(ns pod.babashka.spire | |
(:refer-clojure :exclude [read read-string]) | |
(:require [bencode.core :as bencode] | |
[clojure.edn :as edn]) | |
(:import [java.io PushbackInputStream]) | |
(:gen-class)) | |
(def stdin (PushbackInputStream. System/in)) | |
(def lookup | |
{'pod.babashka.pod.babashka.spire/magic! (fn [& args] | |
(println "Let's build something beautiful!") | |
;; TODO your spire magic here | |
)}) | |
(defn write [v] | |
(bencode/write-bencode System/out v) | |
(.flush System/out)) | |
(defn read-string [^"[B" v] | |
(String. v)) | |
(defn read [] | |
(bencode/read-bencode stdin)) | |
(defn -main [& _args] | |
(loop [] | |
(let [message (try (read) | |
(catch java.io.EOFException _ | |
::EOF))] | |
(when-not (identical? ::EOF message) | |
(let [op (get message "op") | |
op (read-string op) | |
op (keyword op)] | |
(case op | |
:describe (do (write {"format" "edn" | |
"namespaces" [{"name" "pod.babashka.spire" | |
"vars" [{"name" "magic!"}]}] | |
"ops" {"shutdown" {}}}) | |
(recur)) | |
:invoke (let [var (-> (get message "var") | |
read-string | |
symbol) | |
id (-> (get message "id") | |
read-string) | |
args (get message "args") | |
args (read-string args) | |
args (edn/read-string args)] | |
(write {"value" (pr-str (apply (lookup var) args)) | |
"id" id | |
"status" ["done"]}) | |
(recur)) | |
:shutdown (System/exit 0))))))) | |
(-main) | |
(comment | |
(require '[pod.babashka.spire :as spire]) | |
(spire/magic! :here) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment