# hourly-cron.clj -rw-r--r-- 1.2 KiB View raw
                                                                                
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
#!/usr/bin/env bb

(require '[babashka.fs :as fs])
;; (require '[clojure.pprint :as pprint])
(require '[clojure.string])

(def periodic-file-names
  (filter
   (fn [file-name]
     (let [lines (fs/read-all-lines file-name)]
       (some #(re-find #"hourly1x" %) lines)))
   (fs/glob "." "**.hcl")))

(def step (int (Math/floor (/ 59 (count periodic-file-names)))))
(defn gen-cron [start] (format "%d * * * *" start))
(def crons-by-filename (map-indexed (fn [i file-name] (list (gen-cron (* i step)) file-name)) periodic-file-names))
(def content-by-filename (map
                          (fn [[cron-text file-name]]
                            (list (map (fn [line]
                                         (if (re-find #"hourly1x" line)
                                           (clojure.string/replace line #"@hourly1x" cron-text)
                                           line))
                                       (fs/read-all-lines file-name))
                                  (str "build/" file-name)))
                          crons-by-filename))

;; @hourly1x space them out  0 * * * *  to 59 * * * *
(doseq [[lines file-name] content-by-filename]
  (printf "Generating file %s\n" file-name)
  (fs/write-lines file-name lines))