Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
juszczakn committed Feb 21, 2024
1 parent f3ba8d4 commit 20ce2ff
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions cljfmt/src/cljfmt/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,46 @@
(defn- directory? [path]
(some-> path io/file .getAbsoluteFile .isDirectory))

(defn- -load-config
[path]
(->> (some-> path read-config)
(merge default-config)
(convert-legacy-keys)))

(defn- deep-merge
"Recursively merges maps together. If all the maps supplied have nested maps
under the same keys, these nested maps are merged. Otherwise the value is
overwritten, as in `clojure.core/merge`."
{:arglists '([& maps])
:added "1.1.0"}
([])
([a] a)
([a b]
(when (or a b)
(letfn [(merge-entry [m e]
(let [k (key e)
v' (val e)]
(if (contains? m k)
(assoc m k (let [v (get m k)]
(if (and (map? v) (map? v'))
(deep-merge v v')
v')))
(assoc m k v'))))]
(reduce merge-entry (or a {}) (seq b)))))
([a b & more]
(reduce deep-merge (or a {}) (cons b more))))

(defn- load-extra-configs
"Load any :extra-configs specified in the base config file.
Note: is not recursive."
[path]
(loop [conf (-load-config path)
adtl (:extra-configs conf)]
(if (empty? adtl)
conf
(recur (deep-merge (-> adtl first -load-config) conf)
(rest adtl)))))

(defn load-config
"Load a configuration merged with a map of sensible defaults. May take
an path to a config file, or to a directory to search. If no argument
Expand All @@ -68,6 +108,4 @@
(let [path (if (directory? path)
(find-config-file path)
path)]
(->> (some-> path read-config)
(merge default-config)
(convert-legacy-keys)))))
(load-extra-configs path))))

0 comments on commit 20ce2ff

Please sign in to comment.