-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.clj
68 lines (50 loc) · 1.57 KB
/
file.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
(ns automigrate.util.file
"Utils for working with file system."
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str])
(:import [java.nio.file Paths]
[com.github.vertical_blank.sqlformatter SqlFormatter]
[com.github.vertical_blank.sqlformatter.languages Dialect]))
(def DEFAULT-ZERO-COUNT 4)
(defn list-files
[migrations-dir]
(->> (file-seq (io/file migrations-dir))
(filter #(.isFile %))
(sort)))
(defn read-edn
"Return edn data from file.
f - could be file path, file object or reader."
[f]
(edn/read-string (slurp f)))
(defn zfill
"Convert number to string and fill with zero form left."
([number]
(zfill number DEFAULT-ZERO-COUNT))
([number zero-count]
(format (str "%0" zero-count "d") number)))
(defn fmt-sql
[sql-str]
; TODO: switch to Dialect/StandardSql for other databases by default!
(.format (SqlFormatter/of Dialect/PostgreSql) sql-str))
(defn safe-println
([more]
(safe-println more ";"))
([more delimiter]
(.write *out*
(str (str/join (str delimiter "\n") more) "\n"))))
; TODO: reduce duplication with safe-println!
(defn safe-println-sql
([more]
(safe-println-sql more ";"))
([more delimiter]
(.write *out*
(str (str/join (str delimiter "\n\n") more) "\n"))))
(defn prn-err
[e]
(print (str (:message e) "\n")))
(defn join-path
"Join multiple pieces into single file path.
Origin implementation: https://clojureverse.org/t/how-to-join-file-paths/814"
[p & ps]
(str (.normalize (Paths/get p (into-array String ps)))))