From 25667d883d08e372f7a1b164c8681c9771795454 Mon Sep 17 00:00:00 2001 From: anatoly Date: Fri, 13 Nov 2015 21:27:03 -0500 Subject: [PATCH] adding mount/start-with-args --- project.clj | 11 ++++++++++- src/mount/mount.clj | 12 +++++++++++- test/app/app.clj | 17 ++++++++++++++--- test/app/nyse.clj | 23 ++++++++++------------- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/project.clj b/project.clj index 02151eb..0b1f0cf 100644 --- a/project.clj +++ b/project.clj @@ -15,4 +15,13 @@ :profiles {:dev {:source-paths ["dev" "test/app"] :dependencies [[yesql "0.5.1"] [org.clojure/tools.nrepl "0.2.11"] - [com.datomic/datomic-free "0.9.5327" :exclusions [joda-time]]]}}) + [org.clojure/tools.cli "0.3.3"] + [com.datomic/datomic-free "0.9.5327" :exclusions [joda-time]]]} + + ;; "test" is in sources here to just "demo" the uberjar without poluting mount "src" + :uberjar {:source-paths ["test/app"] + :dependencies [[org.clojure/tools.cli "0.3.3"] + [org.clojure/tools.nrepl "0.2.11"] + [com.datomic/datomic-free "0.9.5327" :exclusions [joda-time]]] + :main app + :aot :all}}) diff --git a/src/mount/mount.clj b/src/mount/mount.clj index 0bb42bc..7a47bdf 100644 --- a/src/mount/mount.clj +++ b/src/mount/mount.clj @@ -7,6 +7,7 @@ ;; (defonce ^:private session-id (System/currentTimeMillis)) (defonce ^:private mount-state 42) +(defonce ^:private -args (atom :no-args)) ;; mostly for command line args and external files (defonce ^:private state-seq (atom 0)) (defonce ^:private state-order (atom {})) @@ -57,6 +58,9 @@ (throw (RuntimeException. (str "could not stop [" name "] due to") t))))) (alter-meta! var assoc :started? false))) +;;TODO args might need more thinking +(defn args [] @-args) + (defn mount-state? [var] (= (-> var meta :mount-state) mount-state)) @@ -82,7 +86,7 @@ doall)) (defn start [& states] - (let [states (or states (find-all-states))] + (let [states (or (seq states) (find-all-states))] (bring states up <) :started)) @@ -90,3 +94,9 @@ (let [states (or states (find-all-states))] (bring states down >) :stopped)) + +(defn start-with-args [xs & states] + (reset! -args xs) + (if (first states) + (start states) + (start))) diff --git a/test/app/app.clj b/test/app/app.clj index 20c9eac..9db74d3 100644 --- a/test/app/app.clj +++ b/test/app/app.clj @@ -1,10 +1,12 @@ (ns app (:require [datomic.api :as d] [clojure.tools.nrepl.server :refer [start-server stop-server]] + [clojure.tools.cli :refer [parse-opts]] [mount :refer [defstate]] [app.utils.datomic :refer [touch]] [app.config :refer [app-config]] - [app.nyse :as nyse])) + [app.nyse :as nyse]) + (:gen-class)) ;; example on creating a network REPL (defn- start-nrepl [{:keys [host port]}] @@ -60,6 +62,15 @@ (defn create-nyse-schema [] (create-schema nyse/conn)) -;; example of an app entry point +;; "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)) + (mount/start-with-args + (parse-args args))) diff --git a/test/app/nyse.clj b/test/app/nyse.clj index 0a08019..9558c7e 100644 --- a/test/app/nyse.clj +++ b/test/app/nyse.clj @@ -4,18 +4,15 @@ [clojure.tools.logging :refer [info]] [app.config :refer [app-config]])) -(defn- new-connection [conf] - (info "conf: " conf) - (let [uri (get-in conf [:datomic :uri])] - (info "creating a connection to datomic:" uri) - (d/create-database uri) - (d/connect uri))) +(defn- new-connection [{:keys [datomic-uri]}] + (info "creating a connection to datomic:" datomic-uri) + (d/create-database datomic-uri) + (d/connect datomic-uri)) -(defn disconnect [conf conn] - (let [uri (get-in conf [:datomic :uri])] - (info "disconnecting from " uri) - (.release conn) ;; usually it's not released, here just to illustrate the access to connection on (stop) - (d/delete-database uri))) +(defn disconnect [{:keys [datomic-uri]} conn] + (info "disconnecting from " datomic-uri) + (.release conn) ;; usually it's not released, here just to illustrate the access to connection on (stop) + (d/delete-database datomic-uri)) -(defstate conn :start (new-connection app-config) - :stop (disconnect app-config conn)) +(defstate conn :start (new-connection (mount/args)) + :stop (disconnect (mount/args) conn))