Parse command line args better
This commit is contained in:
parent
019d735167
commit
081a85379b
3 changed files with 56 additions and 11 deletions
1
Procfile
Normal file
1
Procfile
Normal file
|
@ -0,0 +1 @@
|
||||||
|
web: java $JVM_OPTS -cp target/grub-standalone.jar clojure.main -m grub.core --port $PORT production
|
|
@ -12,9 +12,11 @@
|
||||||
[ring/ring-core "1.2.0"]
|
[ring/ring-core "1.2.0"]
|
||||||
[hiccup "1.0.4"]
|
[hiccup "1.0.4"]
|
||||||
[prismatic/dommy "0.1.1"]
|
[prismatic/dommy "0.1.1"]
|
||||||
[com.novemberain/monger "1.5.0"]]
|
[com.novemberain/monger "1.5.0"]
|
||||||
|
[org.clojure/tools.cli "0.3.1"]]
|
||||||
:profiles {:dev {:dependencies [[speclj "2.5.0"]
|
:profiles {:dev {:dependencies [[speclj "2.5.0"]
|
||||||
[clj-webdriver "0.6.0"]]}}
|
[clj-webdriver "0.6.0"]]}}
|
||||||
|
:min-lein-version "2.0.0"
|
||||||
:plugins [[lein-cljsbuild "0.3.2"]
|
:plugins [[lein-cljsbuild "0.3.2"]
|
||||||
[lein-ring "0.8.6"]
|
[lein-ring "0.8.6"]
|
||||||
[speclj "2.5.0"]]
|
[speclj "2.5.0"]]
|
||||||
|
@ -30,4 +32,5 @@
|
||||||
:source-paths ["src/clj" "integration"]
|
:source-paths ["src/clj" "integration"]
|
||||||
:test-paths ["spec/clj"]
|
:test-paths ["spec/clj"]
|
||||||
:ring {:handler grub.core/app}
|
:ring {:handler grub.core/app}
|
||||||
|
:uberjar-name "grub-standalone.jar"
|
||||||
:main grub.core)
|
:main grub.core)
|
||||||
|
|
|
@ -11,7 +11,8 @@
|
||||||
[org.httpkit.server :as httpkit]
|
[org.httpkit.server :as httpkit]
|
||||||
[hiccup
|
[hiccup
|
||||||
[page :refer [html5]]
|
[page :refer [html5]]
|
||||||
[page :refer [include-js include-css]]]))
|
[page :refer [include-js include-css]]]
|
||||||
|
[clojure.tools.cli :refer [parse-opts]]))
|
||||||
|
|
||||||
(def js-file (atom "/js/grub_dev.js"))
|
(def js-file (atom "/js/grub_dev.js"))
|
||||||
|
|
||||||
|
@ -45,7 +46,6 @@
|
||||||
(def integration-test-port 3456)
|
(def integration-test-port 3456)
|
||||||
|
|
||||||
(defn start-server [port]
|
(defn start-server [port]
|
||||||
(println (str "Starting server on localhost:" port))
|
|
||||||
(httpkit/run-server app {:port port}))
|
(httpkit/run-server app {:port port}))
|
||||||
|
|
||||||
(defn run-integration-test []
|
(defn run-integration-test []
|
||||||
|
@ -53,19 +53,60 @@
|
||||||
(integration-test/run integration-test-port)
|
(integration-test/run integration-test-port)
|
||||||
(stop-server)))
|
(stop-server)))
|
||||||
|
|
||||||
(defn start-production-server []
|
(defn start-production-server [port]
|
||||||
(reset! js-file "/js/grub.js")
|
(reset! js-file "/js/grub.js")
|
||||||
(let [db-chan (db/connect-production-database)]
|
(let [db-chan (db/connect-production-database)]
|
||||||
(ws/pass-received-events-to-clients-and-db db-chan)
|
(ws/pass-received-events-to-clients-and-db db-chan)
|
||||||
(start-server default-port)))
|
(println (str "Starting production server on localhost:" port))
|
||||||
|
(start-server port)))
|
||||||
|
|
||||||
(defn start-development-server []
|
(defn start-development-server [port]
|
||||||
(let [db-chan (db/connect-development-database)]
|
(let [db-chan (db/connect-development-database)]
|
||||||
(ws/pass-received-events-to-clients-and-db db-chan)
|
(ws/pass-received-events-to-clients-and-db db-chan)
|
||||||
(start-server default-port)))
|
(println (str "Starting development server on localhost:" port))
|
||||||
|
(start-server port)))
|
||||||
|
|
||||||
|
(defn usage [options-summary]
|
||||||
|
(->> ["Usage: grub [options] action"
|
||||||
|
""
|
||||||
|
"Options:"
|
||||||
|
options-summary
|
||||||
|
""
|
||||||
|
"Actions:"
|
||||||
|
" dev[elopment] Start development server"
|
||||||
|
" prod[uction] Start production server"
|
||||||
|
" integration Run integration tests"]
|
||||||
|
(clojure.string/join \newline)))
|
||||||
|
|
||||||
|
(def cli-options
|
||||||
|
;; An option with a required argument
|
||||||
|
[["-p" "--port PORT" "Port number"
|
||||||
|
:default default-port
|
||||||
|
:parse-fn #(Integer/parseInt %)
|
||||||
|
:validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
|
||||||
|
;; A boolean option defaulting to nil
|
||||||
|
["-h" "--help"]])
|
||||||
|
|
||||||
|
(defn error-msg [errors]
|
||||||
|
(str "The following errors occurred while parsing your command:\n\n"
|
||||||
|
(clojure.string/join \newline errors)))
|
||||||
|
|
||||||
|
(defn exit [status msg]
|
||||||
|
(println msg)
|
||||||
|
(System/exit status))
|
||||||
|
|
||||||
(defn -main [& args]
|
(defn -main [& args]
|
||||||
(cond
|
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
|
||||||
(some #(= % "integration") args) (run-integration-test)
|
;; Handle help and error conditions
|
||||||
(some #(= % "production") args) (start-production-server)
|
(cond
|
||||||
:else (start-development-server)))
|
(:help options) (exit 0 (usage summary))
|
||||||
|
(not= (count arguments) 1) (exit 1 (usage summary))
|
||||||
|
errors (exit 1 (error-msg errors)))
|
||||||
|
;; Execute program with options
|
||||||
|
(case (first arguments)
|
||||||
|
"development" (start-development-server (:port options))
|
||||||
|
"dev" (start-development-server (:port options))
|
||||||
|
"production" (start-production-server (:port options))
|
||||||
|
"prod" (start-production-server (:port options))
|
||||||
|
"integration" (run-integration-test)
|
||||||
|
(exit 1 (usage summary)))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue