This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
/
cli.clj
137 lines (118 loc) · 4.64 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
(ns olorm.cli
(:require
[babashka.cli :as cli]
[babashka.fs :as fs]
[clojure.edn :as edn]
[clojure.string :as str]
[olorm.lib :as olorm]
[babashka.process :refer [shell]]))
(defn config-folder [] (str (fs/xdg-config-home) "/olorm"))
(defn config-file [] (str (config-folder) "/config.edn"))
(defn repo-path []
(-> (config-file)
slurp
edn/read-string
:repo-path))
(declare subcommands)
(defn olorm-help [{:keys [_opts]}]
(println "Available subcommands:")
(println)
(doseq [{:keys [cmds]} subcommands]
(when (and (seq cmds) (not= cmds ["lol"]))
(println (str " olorm " (str/join " " cmds))))))
(defn olorm-set-repo-path [{:keys [opts]}]
(let [repo-path (:repo-path opts)]
(when-not repo-path
(println (str/trim "
Usage:
olorm set-repo-path REPO_PATH
REPO_PATH is the path to the OLORM repo. The CLI can be used from any folder on
your system, so we need to know where to find OLORM pages.
"))
(System/exit 1))
(let [repo-path (fs/absolutize repo-path)]
(fs/create-dirs (config-folder))
(spit (config-file) (prn-str {:repo-path (str repo-path)})))))
(defn olorm-repo-path [{:keys [_opts]}]
(if (fs/exists? (config-file))
(println (repo-path))
(do
(println "Error: config file not found")
(System/exit 1))))
(defn olorm-create [{:keys [opts]}]
(when (or (:help opts) (:h opts))
(println (str/trim "
Usage:
$ olorm create [OPTION...]
Allowed options:
--disable-git-magic Disable running any Git commands. Useful for testing.
--dry-run Supress side effects and print commands instead
--help Show this helptext.
--no-git-magic Alias for --disable-git-magic
"))
(System/exit 0))
(let [repo-path (repo-path)
dispatch (fn [cmd & args]
(if (:dry-run opts)
(prn `(~cmd ~@args))
(apply (resolve cmd) args)))]
(when-not (or (:no-git-magic opts) (:disable-git-magic opts))
(dispatch `shell {:dir repo-path} "git pull --rebase"))
(let [next-number (inc (or (->> (olorm/olorms {:repo-path repo-path}) (map :number) sort last)
0))
olorm (olorm/->olorm {:repo-path repo-path :number next-number})
next-olorm-dir (olorm/path olorm)]
(dispatch `fs/create-dirs next-olorm-dir)
(let [next-index-md (olorm/index-md-path olorm)]
(dispatch `spit next-index-md (olorm/md-skeleton olorm))
(dispatch `spit (olorm/meta-path olorm) (prn-str {:git.user/email (olorm/git-user-email {:repo-path repo-path})
:created (olorm/today)
:uuid (olorm/uuid)}))
(dispatch `shell {:dir repo-path} (System/getenv "EDITOR") next-index-md)
(when-not (:disable-git-magic opts)
(dispatch `shell {:dir repo-path} "git add .")
(dispatch `shell {:dir repo-path} "git commit -m" (str "olorm-" (:number olorm)))
(dispatch `shell {:dir repo-path} "git push")))
(let [olorm-announce-nudge (str "Husk å publisere i #olorm-announce på Slack. Feks:"
"\n\n"
(str " OLORM-" (:number olorm) ": $DIN_TITTEL → https://serve.olorm.app.iterate.no/o/"
(:slug olorm) "/"))]
(println olorm-announce-nudge)))))
(defn lol [{:keys [opts]}]
(let [eval-or-show-work (fn [form]
(if (:dry-run opts)
(prn form)
(eval form)))]
(eval-or-show-work `(prn "trolololo"))
(eval-or-show-work `(shell "ls" ".."))))
(defn olorm-draw [{:keys [opts]}]
(let [pool (:pool opts)]
(when (or (:h opts)
(:help opts)
(not pool))
(println (str/trim "
Usage:
$ olorm draw POOL
POOL is a string that can contain the first letters of the OLORM authors.
Example usage:
$ olorm draw olr
Richard
"
))
(if (or (:h opts) (:help opts))
(System/exit 0)
(System/exit 1)))
(prn
(get (zipmap "olr" '(oddmund lars richard))
(rand-nth pool)))))
(def subcommands
[
{:cmds ["create"] :fn olorm-create}
{:cmds ["help"] :fn olorm-help}
{:cmds ["repo-path"] :fn olorm-repo-path}
{:cmds ["set-repo-path"] :fn olorm-set-repo-path :args->opts [:repo-path]}
{:cmds ["draw"] :fn olorm-draw :args->opts [:pool]}
{:cmds ["lol"] :fn lol}
{:cmds [] :fn olorm-help}])
(defn -main [& args]
(cli/dispatch subcommands args))