Skip to content

Commit c5b46ad

Browse files
authored
Cli tools env vars (#18)
* use another namespace for migrations * fix formatting, remove help from migration * add pure environment variables for source and target * add env var example to help * fix formatting
1 parent 19a3c97 commit c5b46ad

3 files changed

Lines changed: 98 additions & 50 deletions

File tree

deps.edn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
com.datomic/client-cloud {:mvn/version "0.8.105"}
66
com.cognitect/transit-clj {:mvn/version "0.8.313"}
77
com.taoensso/nippy {:mvn/version "3.1.1"}
8+
environ/environ {:mvn/version "1.2.0"}
89
org.clojure/tools.cli {:mvn/version "1.0.206"}}
9-
:tools/usage {:ns-default wanderung.core}
10+
:tools/usage {:ns-default wanderung.cli}
1011
:aliases
1112
{:run-m {:main-opts ["-m" "wanderung.core"]}
1213
:dev {:extra-deps {org.clojure/test.check {:mvn/version "0.9.0"}}

src/wanderung/cli.clj

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(ns wanderung.cli
2+
(:require [wanderung.core :as wc]
3+
[clojure.set :refer [rename-keys]]
4+
[environ.core :refer [env]])
5+
(:import [clojure.lang IExceptionInfo]))
6+
7+
(defn- run-it! [f]
8+
(try
9+
f
10+
(catch Throwable t
11+
(println (.getMessage t))
12+
(when-not (instance? IExceptionInfo t)
13+
(.printStackTrace t))
14+
(System/exit 1))
15+
(finally
16+
(shutdown-agents))))
17+
18+
(defn migrate [opts]
19+
(let [merged-opts (-> (select-keys env [:wanderung-source :wanderung-target])
20+
(rename-keys {:wanderung-source :source
21+
:wanderung-target :target})
22+
(merge opts))]
23+
(println "Running with opts: " merged-opts)
24+
(run-it! (wc/migration merged-opts))))
25+
26+
(def migration migrate)
27+
28+
(def m migrate)
29+
30+
(defn help
31+
([]
32+
(help {}))
33+
([_]
34+
(try
35+
(println "WANDERUNG")
36+
(println "---------")
37+
(println "Run migrations with Datahike to and from various sources")
38+
(println "USAGE:")
39+
(println "clj -Twanderung [function] [function args]")
40+
(println "FUNCTIONS:")
41+
(println "---------")
42+
(println "migrate/migration/m :source SOURCE :target TARGET")
43+
(println "Description: Migrates from given source file to a target file. Source and target must be either file path or environment variable. Use WANDERUNG_SOURCE and WANDERUNG_TARGET environment variables if you don't want to use any input.")
44+
(println "Example: clj -Twanderung migrate :source '\"./source-cfg.edn\"' :target '\"target-cfg.edn\"'")
45+
(println "Example: clj -Twanderung m :source 'SOURCE_CFG' :target 'TARGET_CFG'")
46+
(println "Example: WANDERUNG_SOURCE=./source-cfg.edn WANDERUNG_TARGET=./target-cfg.edn clj -Twanderung migrate'")
47+
(println "---------")
48+
(println "help/h")
49+
(println "Description: Prints this lovely help.")
50+
(println "Example: clj -Twanderung help")
51+
(catch Throwable t
52+
(println (.getMessage t))
53+
(when-not (instance? IExceptionInfo t)
54+
(.printStackTrace t))
55+
(System/exit 1))
56+
(finally
57+
(shutdown-agents)))))
58+
59+
(def h help)

src/wanderung/core.clj

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -93,55 +93,43 @@
9393
slurp
9494
read-string))
9595

96-
(defn help
97-
([]
98-
(help {}))
99-
([_]
100-
(println "WANDERUNG")
101-
(println "---------")
102-
(println "Run migrations with Datahike to and from various sources")
103-
(println "USAGE:")
104-
(println "clj -Twanderung [function] [function args]")
105-
(println "FUNCTIONS:")
106-
(println "---------")
107-
(println "migration :source SOURCE :target TARGET")
108-
(println "Description: Migrates from given source file to a target file.")
109-
(println "Example: clj -Twanderung migration :source '\"./source-cfg.edn\"' :target '\"target-cfg.edn\"'")
110-
(println "---------")
111-
(println "help")
112-
(println "Description: Prints this lovely help.")
113-
(println "Example: clj -Twanderung help")))
114-
115-
(defn migration [{:keys [source target check] show-help :help}]
116-
(if show-help
117-
(help)
118-
(let [src-cfg (load-config source)
119-
tgt-cfg (load-config target)
120-
src-type (:wanderung/type src-cfg)
121-
tgt-type (:wanderung/type tgt-cfg)]
122-
(cond
123-
(not (multimethod-for-dispatch-value? datoms-from-storage src-type))
124-
(println "Cannot use" src-type "as source database.")
125-
126-
(not (multimethod-for-dispatch-value? datoms-to-storage tgt-type))
127-
(println "Cannot use" tgt-type "as target database.")
128-
129-
(:wanderung/read-only? tgt-cfg)
130-
(println "Cannot migrate to read-only database.")
131-
132-
:else (do
133-
(println "➜ Start migrating from" src-type "to" tgt-type "...")
134-
(migrate src-cfg tgt-cfg)
135-
(println " ✓ Done")
136-
(when check
137-
(if (multimethod-for-dispatch-value? datoms-from-storage tgt-type)
138-
(do
139-
(println "➜ Comparing datoms between source and target...")
140-
(if (datom/similar-datoms? (datoms-from-storage src-cfg)
141-
(datoms-from-storage tgt-cfg))
142-
(println " ✓ Success: Datoms look the same.")
143-
(println "ERROR: The datoms differ between source and target.")))
144-
(println "ERROR: The target does not support reading datoms"))))))))
96+
(defn migration [{:keys [source target check]}]
97+
(let [read-cfg (fn [cfg] (when (some? cfg)
98+
(if-let [path-from-env (some? (System/getenv cfg))]
99+
path-from-env
100+
(when (.exists (io/file cfg))
101+
cfg))))]
102+
(if-let [source (read-cfg source)]
103+
(if-let [target (read-cfg target)]
104+
(let [src-cfg (load-config source)
105+
tgt-cfg (load-config target)
106+
src-type (:wanderung/type src-cfg)
107+
tgt-type (:wanderung/type tgt-cfg)]
108+
(cond
109+
(not (multimethod-for-dispatch-value? datoms-from-storage src-type))
110+
(println "Cannot use" src-type "as source database.")
111+
112+
(not (multimethod-for-dispatch-value? datoms-to-storage tgt-type))
113+
(println "Cannot use" tgt-type "as target database.")
114+
115+
(:wanderung/read-only? tgt-cfg)
116+
(println "Cannot migrate to read-only database.")
117+
118+
:else (do
119+
(println "➜ Start migrating from" src-type "to" tgt-type "...")
120+
(migrate src-cfg tgt-cfg)
121+
(println " ✓ Done")
122+
(when check
123+
(if (multimethod-for-dispatch-value? datoms-from-storage tgt-type)
124+
(do
125+
(println "➜ Comparing datoms between source and target...")
126+
(if (datom/similar-datoms? (datoms-from-storage src-cfg)
127+
(datoms-from-storage tgt-cfg))
128+
(println " ✓ Success: Datoms look the same.")
129+
(println "ERROR: The datoms differ between source and target.")))
130+
(println "ERROR: The target does not support reading datoms"))))))
131+
(println "ERORR: Invalid target configuration. Must be either file path or environment variable."))
132+
(println "ERORR: Invalid source configuration. Must be either file path or environment variable."))))
145133

146134
(defn -main [& args]
147135
(let [{options :options

0 commit comments

Comments
 (0)