This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.cljs
47 lines (40 loc) · 1.45 KB
/
cli.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns re-conf.cli
(:require-macros
[clojure.core.strint :refer (<<)])
(:require
[re-conf.resources.log :as log :refer (debug-on)]
[clojure.tools.cli :refer [parse-opts]]))
(def process (js/require "process"))
(def fs (js/require "fs"))
(defn into-categories [s]
(map keyword (clojure.string/split s #"\,")))
(defn- cli-options
[categories]
[["-d" "--debug" "debug (include profiling information)" :default false]
["-e" "--environment ENVIRONMENT" "environment file"
:validate [#(.existsSync fs %) "environment file is missing make sure that path is correct"]]
["-c" "--categories CATEGORIES" (<< "comma seperated list of categories from the available ~(map name (keys categories)) list")
:validate [#(every? (set (keys categories)) (into-categories %)) (<< "each category must be one of ~(keys categories)")]]
["-h" "--help"]])
(defn- pre-process
"catch help and errors"
[{:keys [summary errors options] :as m}]
(when-let [help (options :help)]
(println summary)
(.exit process 0))
(when-not (empty? errors)
(doseq [e errors]
(println e))
(.exit process 1))
(when-not (contains? options :environment)
(println "environment is missing")
(.exit process 1))
(when-not (contains? options :categories)
(println "categories are missing")
(.exit process 1))
(when (options :debug)
(debug-on))
m)
(defn parse-options
[args categories]
(pre-process (parse-opts args (cli-options categories))))