@@ -331,11 +331,66 @@ $ duct --main
331
331
2024-11-23T18:59:14.080Z :report :tutorial.print/hello {:name "World"}
332
332
----
333
333
334
- When using the REPL, we get a more concise message.
334
+ But when using the REPL, we get a more concise message.
335
335
336
336
[,shell]
337
337
----
338
338
user=> (go)
339
339
:initiated
340
340
:tutorial.print/hello {:name "World"}
341
341
----
342
+
343
+ === Variables
344
+
345
+ Sometimes we want to supply options from an external source, such as an
346
+ environment variable or command line option. Duct allows variables, or
347
+ *vars*, to be defined in the `duct.edn` configuration.
348
+
349
+ Currently our application outputs the same log message each time it's
350
+ run. Let's create a configuration var to customize that behavior.
351
+
352
+ [,clojure]
353
+ ----
354
+ {:vars
355
+ {name {:arg name, :env NAME, :type :str, :default "World"
356
+ :doc "The name of the person to greet"}}
357
+ :system
358
+ {:duct.module/logging {}
359
+ :tutorial.print/hello {:logger #ig/ref :duct/logger
360
+ :name #ig/var name}}}
361
+ ----
362
+
363
+ Then in the source file we can add the `:name` option that the var is
364
+ attached to.
365
+
366
+ [,clojure]
367
+ ----
368
+ (ns tutorial.print
369
+ (:require [duct.logger :as log]))
370
+
371
+ (defn hello [{:keys [logger name]}]
372
+ (log/report logger ::hello {:name name}))
373
+ ----
374
+
375
+ The default ensures that the application functions the same as before.
376
+
377
+ [,shell]
378
+ ----
379
+ $ duct --main
380
+ ✓ Initiating system...
381
+ 2024-11-23T23:53:47.069Z :report :tutorial.print/hello {:name "World"}
382
+ ----
383
+
384
+ But we can now customize the behavior via a command-line flag, `--name`,
385
+ or via an environment variable, `NAME`.
386
+
387
+ [,shell]
388
+ ----
389
+ $ duct --main --name=Clojurian
390
+ ✓ Initiating system...
391
+ 2024-11-24T04:45:19.521Z :report :tutorial.print/hello {:name "Clojurian"}
392
+
393
+ $ NAME=Clojurist duct --main
394
+ ✓ Initiating system...
395
+ 2024-11-24T04:45:54.211Z :report :tutorial.print/hello {:name "Clojurist"}
396
+ ----
0 commit comments