retrogradeorbit / bootleg

Simple template processing command line tool to help build static websites
Eclipse Public License 2.0
255 stars 12 forks source link

A way to read environment variables inside a bootleg script #42

Closed chr15m closed 4 years ago

chr15m commented 4 years ago

Alternatively / additionally a way to pass flags in. Maybe a -f someflag=12 or even allowing command line eval as well as file input.

Basically I want a way to call my script and set a conditional from outside.

retrogradeorbit commented 4 years ago

Command line args are available in *ARGV* or in *command-line-args*:

$ bootleg -d -e '*command-line-args*'
("-d" "-e" "*command-line-args*")
$ bootleg -d -e '*ARGV*'
("-d" "-e" "*ARGV*")

The clojure System namespace is now available (java.lang.System https://docs.oracle.com/javase/8/docs/api/java/lang/System.html ) and this includes getenv:

$ FOO=bar bootleg -d -e '(System/getenv "FOO")'
"bar"

And I have included the clojure.tools.cli namespace that you can require as needed to process command line args, or reference directly:

$ bootleg -d -e '(clojure.tools.cli/parse-opts *command-line-args* [["-e" "--evaluate CODE" ""] ["-d" "--data" ""]])'
{:options {:data true,
           :evaluate "(clojure.tools.cli/parse-opts *command-line-args* [[\"-e\" \"--evaluate CODE\" \"\"] [\"-d\" \"--data\" \"\"]])"},
 :arguments [],
 :summary "  -e, --evaluate CODE\n  -d, --data",
 :errors nil}

If you want to pass in edn in a env var or command line you can use edamame's reader that is already available under parse-string:

$ FOO='{:a 1 :b 2}' bootleg -d -e '(-> "FOO" System/getenv parse-string :b)'
2
chr15m commented 4 years ago

Awesome, will test. Thank you.

retrogradeorbit commented 4 years ago

Removed *ARGV*. Although other lisps have this dynamic var it would be best to just have one way that matches clojure's.