Pallet scripts are written using an embedding of shell script in Clojure, and allows for abstraction of operating system variations.
At it’s simplest, the script
macro takes one or more forms and converts them
to shell script.
(require
'[pallet.stevedore :as stevedore])
(stevedore/script
(ls))
There is an obvious mismatch between Clojure’s immutable data approach, and shell script’s mutable variables.
For accessing the value of a script variable, stevedore uses `deref` or `@`, which are also used for command substitution.
(stevedore/script
(deref TMPDIR))
If the variable isn’t set, a default can be supplied.
(stevedore/script
@TMPDIR-/tmp)
Values can be defined with `set!`.
(stevedore/script
(set! x 1))
Similar to variable substitution, command substitution uses `deref`.
(stevedore/script
(set! R @(cat "/etc/redhat-release")))
Shell script `for` loops are mapped to clojure`s `doseq`.
(stevedore/script
(doseq [x ["a" "b" "c"]]
(println @x)))
Calling a function follows clojure’s normal syntax.
(stevedore/script
(foo x y))
Defining a function follows a simplified version of clojure’s `defn`, and does not allow for destructuring. Arguments are named and automatically mapped to the shell function’s numbered arguments.
(stevedore/script
(script (defn foo [x y] (bar x))))
Array literals are generated from clojure’s `vector` notation.
(stevedore/script
[1 "2" (quoted 3) :four])
Array access uses `aget` and `aset`.
(stevedore/script
(aget foo 1))
(stevedore/script
(aset foo 1 :foo))
One advantage of embedding script in clojure is that we can escape back to clojure to substitute in values.
(let [tmp "/tmp"]
(stevedore/script
(ls ~tmp)))
We can also unquote arbitrary clojure expressions.
(stevedore/script
(ls ~(str "/" "tmp")))
Sometimes we want to add the contents of a collection into a script, without demarking the boundaries of the collection.
(let [files ["a" "b" "c"]]
(stevedore/script
(ls ~@files)))
Script functions are defined with `pallet.script/defscript` and provide multimethods for shell script generation. In pallet this is used to abstract away script differences between operating systems.
(require
'[pallet.stevedore :as stevedore]
'[pallet.script :as script])
(script/defscript ls [& args])
(stevedore/defimpl ls :default [& args]
(ls ~@args))
(stevedore/defimpl ls [:windows] [& args]
(dir ~@args))
(script/with-template [:windows]
(stevedore/script
(ls a b c)))
An excellent reference on shell scripting is available at http://tldp.org/LDP/abs/html/.