项目作者: agilecreativity

项目描述 :
Simple wrapper to clj-ssh
高级语言: Clojure
项目地址: git://github.com/agilecreativity/swiza-sh.git
创建时间: 2019-08-31T15:25:03Z
项目社区:https://github.com/agilecreativity/swiza-sh

开源协议:

下载


swiza-sh

Clojars Project
Dependencies Status
ClojarsDownloads

Clojure library designed to wrap the execution of shell script to make it easy to manipulate the result.

The library rely on the awesome power of clj-common-exec which wrap the functionality of apache-common-exec

Installation

Just include the following line in your dependency vector

  1. [net.b12n/swiza-sh "0.1.3"]

Available Apis

  • sh-cmd - run a single shell command
  • sh-cmds - run multiple shell commands
  • run-cmds - run multiple shell commands (alternative api)
  • sh-exec - run command with callback function

Example Usages

  1. (require '[b12n.swiza.commons :refer [expand-path]])
  2. (require '[b12n.swiza.sh.core :refer [sh-cmd sh-cmds run-cmds sh-exec]])
  • run-cmds : run multiple commands optionally stop on first error
  1. ;; a)
  2. (run-cmds {:dir \".\"
  3. :cmds [\"ls -alt\"
  4. \"find . -type f -iname \"*.clj\"]})
  5. ;; b) Similar to the first usage, but stop on the first error
  6. (run-cmds {:dir \".\"
  7. :ignore-error? false
  8. :cmds [\"ls -alt\"
  9. \"invalid-command\"
  10. \"find . -type f -iname \"*.clj\"]})
  11. ;; c) Run multiple command using that run in start from different directory (ignore error)
  12. ;; e.g. don't specify `:dir` option
  13. (run-cmds {:cmds [\"ls -alt\"
  14. \"find . -type f -iname \"*.clj\"]})
  15. ;; d) Same as above but stop on first error
  16. (run-cmds {:ignore-error? false
  17. :cmds [\"ls -alt\"
  18. \"find . -type f -iname \"*.clj\"]})"
  • sh-cmds : run multiple command using the original api

Build multiple Clojure projects using Leiningen

  1. (sh-cmds [{:cmd ["lein" "deps" ":tree"] :opts {:dir (expand-path "~/apps/swiza/swiza-commons")}}
  2. {:cmd ["lein" "deps" ":tree"] :opts {:dir (expand-path "~/apps/swiza/swiza-jenkins")}}
  3. {:cmd ["lein" "deps" ":tree"] :opts {:dir (expand-path "~/apps/swiza/swiza-aws")}}])

Build and run GraalVM native image from Clojure

  1. ;; Build and run the GraalVM project using `lein native-image`
  2. (let [base-dir (expand-path "~/apps/swizz/swiza-graal")]
  3. (sh-cmds [{:cmd ["lein" "deps" ":tree"] :opts {:dir base-dir}}
  4. {:cmd ["lein" "native-image"] :opts {:dir base-dir}}
  5. {:cmd [(format "%s/target/default+uberjar/swiza-graal" base-dir)]}]))

You will get output like:

  1. (["drwxr-xr-x" "7" "bchoomnuan" "staff" "224" "Aug" "31" "11:01" "."]
  2. ["drwxr-xr-x" "60" "bchoomnuan" "staff" "1920" "Aug" "31" "09:22" ".."]
  3. ["-rw-r--r--" "1" "bchoomnuan" "staff" "5" "Aug" "31" "09:53" ".nrepl-port"]
  4. ["-rw-r--r--" "1" "bchoomnuan" "staff" "1328" "Aug" "31" "11:01" "README.md"]
  5. ["-rw-r--r--" "1" "bchoomnuan" "staff" "2691" "Aug" "30" "22:56" "project.clj"]
  6. ["drwxr-xr-x" "4" "bchoomnuan" "staff" "128" "Aug" "23" "22:34" "src"]
  7. ["drwxr-xr-x" "4" "bchoomnuan" "staff" "128" "Aug" "24" "01:16" "target"])

If you like to post-process the result of a single output then try sh-exec

  1. (sh-exec ["ls" "-al"]
  2. {:opts {:dir (expand-path ".")}
  3. :success-fn (fn [x]
  4. (if-let [lines (clojure.string/split x #"\n")]
  5. (map #(clojure.string/split % #"\s+") (rest lines))))})
  1. ;; More example
  2. (if-let [files (sh-exec ["find" (expand-path "~/apps/pro-scripts")
  3. "-type" "f"
  4. "-iname" "*.sh"]
  5. {:success-fn (fn [line]
  6. (if-let [result (str/split line #"\n")]
  7. result))
  8. :opts {:dir (expand-path "~/apps/pro-scripts")}})]
  9. (doseq [file files]
  10. (println file)))

Development

  1. git clone git@github.com:agilecreativity/swiza-sh.git
  2. cd swiza-sh && lein deps :tree
  3. lein repl
  4. ;; then hackaway