-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.clj
50 lines (37 loc) · 1.36 KB
/
help.clj
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
47
48
49
(ns automigrate.help
(:require [clojure.string :as str]))
(def ^:private DOC-LINK
"https://github.com/abogoyavlensky/automigrate#documentation")
(def HELP-CMDS-ORDER
['make 'migrate 'list 'explain 'help])
(defn- fn-docstring
[public-methods fn-sym]
(-> public-methods (get fn-sym) (meta) :doc (str "\n")))
(defn- general-help
[public-methods]
(let [all-cmd-descs (reduce
(fn [acc cmd]
(let [cmd-desc (->> (fn-docstring public-methods cmd)
(str/split-lines)
(first))]
(conj acc (str " " cmd " - " cmd-desc))))
[]
HELP-CMDS-ORDER)]
(str/join
"\n"
(concat
["Auto-generated database migrations for Clojure.\n"
"Available commands:"]
all-cmd-descs
["\nRun 'help :cmd COMMAND' for more information on a command.\n"
(str "To get more info, check out automigrate documentation at " DOC-LINK)]))))
(defn- print-out-str
[doc-str]
(.write *out*
(str doc-str "\n")))
(defn show-help!
[{:keys [cmd]}]
(let [public-methods (ns-publics 'automigrate.core)]
(if (some? cmd)
(-> public-methods (fn-docstring cmd) (print-out-str))
(-> public-methods (general-help) (print-out-str)))))