-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.clj
69 lines (56 loc) · 2.02 KB
/
cli.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(ns enviable.cli
(:require [enviable.reader :as core]
[enviable.cli.table :as table]
[enviable.cli.ansi :as ascii]))
(defn determine-status [{:keys [error?]
::core/keys [input parsed]}]
(case [error? (some? input) (some? parsed)]
[true false false] :status/missing
[true true false] :status/invalid
[false false true] :status/default
[false false false] :status/optional
:status/success
))
(defn flatten-results [{::core/keys [error ok] :as res}]
(concat (map #(assoc % :error? true) error)
(map #(assoc % :error? false) ok)))
(defn header-cell [text]
(table/cell text ascii/bold))
(defn name-cell [{::core/keys [name]}]
(table/cell name))
(def sensitive-value-cell
(table/cell "********"))
(defn input-cell [{::core/keys [input sensitive]}]
(if sensitive
sensitive-value-cell
(table/cell input)))
(defn value-cell [{::core/keys [parsed sensitive] :as val}]
(if sensitive
sensitive-value-cell
(table/cell parsed)))
(defn description-cell [{::core/keys [description]}]
(if description
(table/cell description)
(table/cell "-" ascii/fg-grey)))
(defn ns-cell [{::core/keys [ns]}]
(table/cell ns))
(defn status-cell [result]
(case (determine-status result)
:status/invalid (table/cell "Invalid" ascii/fg-red)
:status/missing (table/cell "Required" ascii/fg-yellow)
:status/default (table/cell "Default" ascii/fg-grey)
:status/optional (table/cell "Optional" ascii/fg-grey)
(table/cell "Loaded" ascii/fg-green)))
(defn result-str [result]
(let [results (flatten-results result)
cols [["Name" name-cell]
["Status" status-cell]
["Input" input-cell]
["Value" value-cell]
["Requested by" ns-cell]
["Description" description-cell]]]
(->> (for [[header value-cell-fn] cols]
(concat [(header-cell header)]
(map value-cell-fn results)))
(table/transpose)
(table/render-table))))