我设法从https://stackoverflow.com/questions/56541688/how-to-run-an-interactive-cli-program-from-within-clojure中运行一个交互式CLI。
但我还需要设置环境变量,https://stackoverflow.com/questions/30574972/java-processbuilder-and-using-environment-variables建议使用pb.environment().put("ENV", "val")
,因此我认为应该是(-> pb .environment (put. env val))
(defn process-interactive-cli
[args & [working-directory envs]]
(let [process-builder (java.lang.ProcessBuilder. args)
inherit (java.lang.ProcessBuilder$Redirect/INHERIT)
environment (.environment process-builder)]
;; change CWD
(when working-directory
(.directory process-builder (io/file working-directory)))
;; set environment variables
(for [[env val] envs]
(.put environment env val))
(.redirectOutput process-builder inherit)
(.redirectError process-builder inherit)
(.redirectInput process-builder inherit)
;; print the executing command (~ set -x)
(apply println ">" args)
(.waitFor (.start process-builder))))
但是断调试这个给我
1. 未处理的java.lang.IllegalArgumentException No matching method put found taking 2 args for class java.lang.ProcessEnvironment$StringEnvironment