Skip to content

Instantly share code, notes, and snippets.

@joncol
Created January 10, 2020 08:31
Show Gist options
  • Save joncol/d3cd3c49696e16a59dfec9b8dadeab17 to your computer and use it in GitHub Desktop.
Save joncol/d3cd3c49696e16a59dfec9b8dadeab17 to your computer and use it in GitHub Desktop.
No-exit
(ns nordea-deposits-scraping.main
(:gen-class)
(:require [clojure.spec.alpha :as s]
[clojure.tools.cli :refer [parse-opts]]
[clojure.tools.logging :as log]
[clojure.tools.logging.impl :as log-impl]
;; [duct.core :as duct]
nordea-deposits-scraping.app
[nordea-deposits-scraping.config :refer [read-config]]
nordea-deposits-scraping.postgres
[integrant.core :as ig]))
(alter-var-root
#'log/*logger-factory*
(fn [_]
(if (log-impl/class-found? "org.apache.log4j.Logger")
(log-impl/log4j-factory)
(throw (ex-info "System does not support logging through log4j" {})))))
(def ^:private cli-options
[["-m" "--migrate" "Run migrations"]
[nil "--server" "Run server"]
["-h" "--help"]])
(defn cli-opts->duct-config [cli-opts]
{[:duct/const :nds.cli/arguments] (:arguments cli-opts)
[:duct/const :nds.cli/opts] (:options cli-opts)})
(defn- run-system [cli-opts keys]
(-> (read-config)
(merge (cli-opts->duct-config cli-opts))
(ig/prep keys)
(ig/init keys)))
(defn- start [args]
(let [{:keys [summary options errors] :as opts} (parse-opts args cli-options)]
(cond
errors
(println errors "\n\n" summary)
(contains? options :help)
(println summary)
(contains? options :migrate)
(run-system opts [:nds/postgres])
#_(run-system opts [:nds/postgres-migrations])
(contains? options :server)
(run-system opts [:adapter/jetty])
:else
(println "Arguments not recognized, exiting."))))
(defn -main [& args]
(s/check-asserts true)
(try
(let [system (start args)]
(println "System:" system))
(catch Throwable e
(log/fatal (str e "Could not start system "))
(System/exit 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment