Skip to content

Commit 8e8edb2

Browse files
committed
Add Variables section back in
1 parent 7ab305d commit 8e8edb2

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

Diff for: index.adoc

+56-1
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,66 @@ $ duct --main
331331
2024-11-23T18:59:14.080Z :report :tutorial.print/hello {:name "World"}
332332
----
333333

334-
When using the REPL, we get a more concise message.
334+
But when using the REPL, we get a more concise message.
335335

336336
[,shell]
337337
----
338338
user=> (go)
339339
:initiated
340340
:tutorial.print/hello {:name "World"}
341341
----
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

Comments
 (0)