Liking cljdoc? Tell your friends :D

swiza-sh

Clojars Project Dependencies Status

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

[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

(require '[b12n.swiza.commons :refer [expand-path]])
(require '[b12n.swiza.sh.core :refer [sh-cmd sh-cmds run-cmds sh-exec]])
  • run-cmds : run multiple commands optionally stop on first error
;; a)
(run-cmds {:dir \".\"
           :cmds [\"ls -alt\"
                  \"find . -type f -iname \"*.clj\"]})
;; b) Similar to the first usage, but stop on the first error
(run-cmds {:dir \".\"
           :ignore-error? false
           :cmds [\"ls -alt\"
                  \"invalid-command\"
                  \"find . -type f -iname \"*.clj\"]})

;; c) Run multiple command using that run in start from different directory (ignore error)
;; e.g. don't specify `:dir` option
(run-cmds {:cmds [\"ls -alt\"
                  \"find . -type f -iname \"*.clj\"]})

;; d) Same as above but stop on first error
(run-cmds {:ignore-error? false
             :cmds [\"ls -alt\"
                    \"find . -type f -iname \"*.clj\"]})"
  • sh-cmds : run multiple command using the original api

Build multiple Clojure projects using Leiningen

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

Build and run GraalVM native image from Clojure

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

You will get output like:

(["drwxr-xr-x" "7" "bchoomnuan" "staff" "224" "Aug" "31" "11:01" "."] 
 ["drwxr-xr-x" "60" "bchoomnuan" "staff" "1920" "Aug" "31" "09:22" ".."] 
 ["-rw-r--r--" "1" "bchoomnuan" "staff" "5" "Aug" "31" "09:53" ".nrepl-port"] 
 ["-rw-r--r--" "1" "bchoomnuan" "staff" "1328" "Aug" "31" "11:01" "README.md"] 
 ["-rw-r--r--" "1" "bchoomnuan" "staff" "2691" "Aug" "30" "22:56" "project.clj"] 
 ["drwxr-xr-x" "4" "bchoomnuan" "staff" "128" "Aug" "23" "22:34" "src"] 
 ["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

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

Development

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

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close