Skip to content

Latest commit

 

History

History
205 lines (152 loc) · 4.24 KB

reference.org

File metadata and controls

205 lines (152 loc) · 4.24 KB

Pallet Reference - Script

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))

Variables

There is an obvious mismatch between Clojure’s immutable data approach, and shell script’s mutable variables.

Substitution

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)

Assignment

Values can be defined with `set!`.

(stevedore/script
 (set! x 1))

Command Substitution

Similar to variable substitution, command substitution uses `deref`.

(stevedore/script
 (set! R @(cat "/etc/redhat-release")))

Loops

Shell script `for` loops are mapped to clojure`s `doseq`.

(stevedore/script
 (doseq [x ["a" "b" "c"]]
   (println @x)))

Functions

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))))

Arrays

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))

Unquoting

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)))

Expressions

We can also unquote arbitrary clojure expressions.

(stevedore/script
 (ls ~(str "/" "tmp")))

Splicing

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

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)))

Scripting Guide

An excellent reference on shell scripting is available at http://tldp.org/LDP/abs/html/.