Skip to content

Latest commit

 

History

History
130 lines (93 loc) · 3.84 KB

runtime-arguments.md

File metadata and controls

130 lines (93 loc) · 3.84 KB

Passing Runtime Arguments

This example lives in the with-args branch. If you'd like to follow along:

$ git checkout with-args
Switched to branch 'with-args'

Start with args

In order to pass runtime arguments, these could be -this x -that y params or -Dparam= or just a path to an external configuration file, mount has a special start-with-args function:

(defn -main [& args]
  (mount/start-with-args args))

Most of the time it is better to parse args before they "get in", so usually accepting args would look something like:

(defn -main [& args]
  (mount/start-with-args
    (parse-args args)))

where the parse-args is an app specific function.

Reading arguments

Once the arguments are passed to the app, they are available via:

(mount/args)

Which, unless the arguments were parsed or modified in the -main function, will return the original args that were passed to -main.

"Reading" example

Here is an example app that takes -main arguments and parses them with tools.cli:

;; "any" regular function to pass arguments
(defn parse-args [args]
  (let [opts [["-d" "--datomic-uri [datomic url]" "Datomic URL"
              :default "datomic:mem://mount"]
              ["-h" "--help"]]]
    (-> (parse-opts args opts)
        :options)))

;; example of an app entry point with arguments
(defn -main [& args]
  (mount/start-with-args
    (parse-args args)))

For the example sake the app reads arguments in two places:

(defstate conn :start (new-connection (mount/args))
               :stop (disconnect (mount/args) conn))
(defn load-config [path]
  ;; ...
  (if (:help (mount/args))
    (info "\n\nthis is a sample mount app to demo how to pass and read runtime arguments\n"))
  ;; ...)

"Uber" example

In order to demo all of the above, we'll build an uberjar:

$ lein do clean, uberjar
...
Created .. mount/target/mount-0.1.5-SNAPSHOT-standalone.jar

Since we have a default for a Datomic URI, it'll work with no arguments:

$ java -jar target/mount-0.1.5-SNAPSHOT-standalone.jar

22:12:03.290 [main] INFO  mount - >> starting..  app-config
22:12:03.293 [main] INFO  mount - >> starting..  conn
22:12:03.293 [main] INFO  app.nyse - creating a connection to datomic: datomic:mem://mount
22:12:03.444 [main] INFO  mount - >> starting..  nrepl

Now let's ask it to help us:

$ java -jar target/mount-0.1.5-SNAPSHOT-standalone.jar --help

22:13:48.798 [main] INFO  mount - >> starting..  app-config
22:13:48.799 [main] INFO  app.config -

this is a sample mount app to demo how to pass and read runtime arguments

22:13:48.801 [main] INFO  mount - >> starting..  conn
22:13:48.801 [main] INFO  app.nyse - creating a connection to datomic: datomic:mem://mount
22:13:48.946 [main] INFO  mount - >> starting..  nrepl

And finally let's connect to the Single Malt Database. It's Friday..

$ java -jar target/mount-0.1.5-SNAPSHOT-standalone.jar -d datomic:mem://single-malt-database

22:16:10.733 [main] INFO  mount - >> starting..  app-config
22:16:10.737 [main] INFO  mount - >> starting..  conn
22:16:10.737 [main] INFO  app.nyse - creating a connection to datomic: datomic:mem://single-malt-database
22:16:10.885 [main] INFO  mount - >> starting..  nrepl

Other usecases

Depending the requirements, these runtime arguments could take different shapes of forms. You would have a full control over what is passed to the app, the same way you have it without mount through -main [& args].