From 8dafe0d268bfa3eff76c9e365577b3fc08d25462 Mon Sep 17 00:00:00 2001 From: Ralf Schmitt Date: Tue, 7 May 2024 12:56:14 +0200 Subject: [PATCH] Reformat files with zprint --- build.clj | 79 ++++++++++++++--------------------- deps.edn | 56 ++++++++++++------------- dev/user.clj | 9 ++-- src/prune_backups/cli.clj | 75 +++++++++++++-------------------- src/prune_backups/proto.clj | 6 ++- src/prune_backups/rotate.clj | 56 ++++++++----------------- src/prune_backups/tarsnap.clj | 29 +++++-------- src/prune_backups/zfs.clj | 34 +++++++-------- tests.edn | 4 +- 9 files changed, 142 insertions(+), 206 deletions(-) diff --git a/build.clj b/build.clj index d08e450..ed4c2b9 100644 --- a/build.clj +++ b/build.clj @@ -1,74 +1,59 @@ ;; Run this script with "clj -T:build uber" (ns build - (:require [clojure.tools.build.api :as b] - [babashka.fs :as fs])) + (:require [babashka.fs :as fs] + [clojure.tools.build.api :as b])) (def lib 'com.github.schmir/prune-backups) (def git-branch (b/git-process {:git-args "branch --show-current"})) -(def version (str (if (= git-branch "main") - nil - (str git-branch "-")) - "v0.1." (b/git-count-revs nil))) +(def version + (str (if (= git-branch "main") nil (str git-branch "-")) "v0.1." (b/git-count-revs nil))) (def class-dir "target/classes") (def meta-clj (format "%s/prune_backups/meta.clj" class-dir)) (def basis (b/create-basis {:project "deps.edn"})) (def uber-file (format "target/%s.jar" (name lib))) (def uberscript-file (format "target/%s" (name lib))) -(let [d (delay (b/delete {:path "target"}))] - (defn clean [_] - @d)) +(let [d (delay (b/delete {:path "target"}))] (defn clean [_] @d)) (defn meta-ns [] - (str "(ns prune-backups.meta)" \newline - "(def version " (pr-str version) ")" \newline - \newline)) + (str "(ns prune-backups.meta)" \newline "(def version " (pr-str version) ")" \newline \newline)) -(defn uber [_] +(defn uber + [_] (clean nil) - (b/copy-dir {:src-dirs ["src" "resources"] - :target-dir class-dir}) + (b/copy-dir {:src-dirs ["src" "resources"], :target-dir class-dir}) (spit meta-clj (meta-ns)) - (b/compile-clj {:basis basis - :ns-compile '[prune-backups.cli] - :class-dir class-dir}) - (b/uber {:class-dir class-dir - :uber-file uber-file - :basis basis - :main 'prune-backups.cli})) + (b/compile-clj {:basis basis, :ns-compile '[prune-backups.cli], :class-dir class-dir}) + (b/uber {:class-dir class-dir, :uber-file uber-file, :basis basis, :main 'prune-backups.cli})) (defn read-source [] - (apply str (->> ["proto.clj" "rotate.clj" "tarsnap.clj" "zfs.clj" "cli.clj"] - (map (fn [s] - (str ";; --- " s \newline - (slurp (str "src/prune_backups/" s)) - \newline)))))) + (apply str + (->> ["proto.clj" "rotate.clj" "tarsnap.clj" "zfs.clj" "cli.clj"] + (map (fn [s] + (str ";; --- " s \newline (slurp (str "src/prune_backups/" s)) \newline)))))) (defn prelude [] - (str - "#!/usr/bin/env bb" \newline - ";; -*- mode: clojure -*-" \newline - ";; prune-backups " version \newline - ";;" \newline - ";; DO NOT EDIT THIS FILE" \newline - ";;" \newline - "" \newline)) - - -(defn uberscript [_] + (str "#!/usr/bin/env bb" + \newline + ";; -*- mode: clojure -*-" + \newline + ";; prune-backups " + version + \newline + ";;" \newline + ";; DO NOT EDIT THIS FILE" \newline + ";;" \newline + "" \newline)) + + +(defn uberscript + [_] (clean nil) (fs/create-dirs "target") - (spit uberscript-file - (str - (prelude) - (meta-ns) - (read-source))) + (spit uberscript-file (str (prelude) (meta-ns) (read-source))) (fs/set-posix-file-permissions uberscript-file "rwxr-xr-x")) -(defn uberall [_] - (uber nil) - (uberscript nil) - (spit "target/VERSION" version)) +(defn uberall [_] (uber nil) (uberscript nil) (spit "target/VERSION" version)) diff --git a/deps.edn b/deps.edn index 9ab65cd..b155c60 100644 --- a/deps.edn +++ b/deps.edn @@ -1,34 +1,32 @@ -{:paths ["src"] - :deps {org.clojure/clojure {:mvn/version "1.11.1"} - org.babashka/cli {:mvn/version "0.7.53"} - babashka/fs {:mvn/version "0.5.20"} - babashka/process {:mvn/version "0.5.21"}} - :aliases {:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"} - babashka/fs {:mvn/version "0.5.20"}} - :ns-default build} - :test - {:extra-paths ["test"] - :extra-deps {io.github.cognitect-labs/test-runner - {:git/tag "v0.5.1" :git/sha "dfb30dd"}} - :main-opts ["-m" "cognitect.test-runner"] - :exec-fn cognitect.test-runner.api/test} +{:paths ["src"], + :deps {org.clojure/clojure {:mvn/version "1.11.1"}, + org.babashka/cli {:mvn/version "0.7.53"}, + babashka/fs {:mvn/version "0.5.20"}, + babashka/process {:mvn/version "0.5.21"}}, + :aliases {:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}, + babashka/fs {:mvn/version "0.5.20"}}, + :ns-default build}, + :test {:extra-paths ["test"], + :extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1", + :git/sha "dfb30dd"}}, + :main-opts ["-m" "cognitect.test-runner"], + :exec-fn cognitect.test-runner.api/test}, :outdated #_:clj-kondo/ignore - {:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"} - org.slf4j/slf4j-nop {:mvn/version "2.0.9"}} ;; silence SLF4J warning - :main-opts ["-m" "antq.core"]} + {:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"}, + org.slf4j/slf4j-nop {:mvn/version "2.0.9"}}, ;; silence SLF4J warning + :main-opts ["-m" "antq.core"]}, - :dev - {:extra-paths ["test" "dev"] - :extra-deps {org.clojure/tools.namespace {:mvn/version "1.4.4"} - lambdaisland/kaocha {:mvn/version "1.87.1366"} - org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J warning - }} + :dev {:extra-paths ["test" "dev"], + :extra-deps {org.clojure/tools.namespace {:mvn/version "1.4.4"}, + lambdaisland/kaocha {:mvn/version "1.87.1366"}, + org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J + ;; warning + }}, - :kaocha - {:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"} - org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J warning - } - :main-opts ["-m" "kaocha.runner"]}} - } + :kaocha {:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}, + org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J + ;; warning + }, + :main-opts ["-m" "kaocha.runner"]}}} diff --git a/dev/user.clj b/dev/user.clj index 79ec8cd..73486d8 100644 --- a/dev/user.clj +++ b/dev/user.clj @@ -1,9 +1,8 @@ (ns user #_:clj-kondo/ignore - (:require - [clojure.test] - [clojure.tools.namespace.repl :as nsrepl] - [kaocha.repl])) + (:require [clojure.test] + [clojure.tools.namespace.repl :as nsrepl] + [kaocha.repl])) (clojure.tools.namespace.repl/set-refresh-dirs "src" "test" "dev") ;; (clojure.tools.namespace.repl/disable-reload!) @@ -16,4 +15,4 @@ (nsrepl/refresh) (kaocha.repl/run-all) ;;(clojure.test/run-all-tests #"fin\..*") - ) +) diff --git a/src/prune_backups/cli.clj b/src/prune_backups/cli.clj index fc8b034..04b6b02 100644 --- a/src/prune_backups/cli.clj +++ b/src/prune_backups/cli.clj @@ -1,88 +1,71 @@ (ns prune-backups.cli - (:require [clojure.edn :as edn] + (:require [babashka.cli :as cli] + [clojure.edn :as edn] [clojure.string :as str] [prune-backups.meta :as meta] - [prune-backups.rotate :as rotate] [prune-backups.proto :as proto] + [prune-backups.rotate :as rotate] [prune-backups.tarsnap :as tarsnap] - [prune-backups.zfs :as zfs] - [babashka.cli :as cli]) + [prune-backups.zfs :as zfs]) (:gen-class)) (set! *warn-on-reflection* true) (def spec - {:config - {:desc "Path to edn config file"} - :version - {:desc "Show version information"} - :help - {:desc "Show help message" - :alias :h}}) + {:config {:desc "Path to edn config file"}, + :version {:desc "Show version information"}, + :help {:desc "Show help message", :alias :h}}) (defn print-help [] (println "Usage: prune-backups [options]") - (println (cli/format-opts {:spec spec :order [:config :version :help]})) + (println (cli/format-opts {:spec spec, :order [:config :version :help]})) (System/exit 0)) (defn find-archives-with-prefix [prefix archives] - (filter (fn [a] (str/starts-with? (:archive a) prefix)) - archives)) + (filter (fn [a] (str/starts-with? (:archive a) prefix)) archives)) (defn rotate - ([cfg] - (rotate cfg (proto/list-backups (:backup-set cfg)))) + ([cfg] (rotate cfg (proto/list-backups (:backup-set cfg)))) ([cfg all-archives] (->> (:prefixes cfg) (mapcat (fn [prefix] (let [archives (find-archives-with-prefix prefix all-archives) - res (rotate/rotate-backups archives :datetime (:rotate cfg))] - (println (str (pr-str prefix) ": " (count res) " archives, destroying " (count (filter rotate/drop? res)))) + res (rotate/rotate-backups archives :datetime (:rotate cfg))] + (println (str (pr-str prefix) + ": " (count res) + " archives, destroying " (count (filter rotate/drop? res)))) res)))))) (defn run [bs] (println (pr-str bs)) (let [archives (rotate bs) - destroy (filter rotate/drop? archives)] + destroy (filter rotate/drop? archives)] ;; (println (count archives) "archives, destroying" (count destroy)) ;; (pprint/pprint archives) - (doseq [d destroy] - (proto/destroy-backup (:backup-set bs) d)))) + (doseq [d destroy] (proto/destroy-backup (:backup-set bs) d)))) (defn tarsnap-reader [m] - (-> m - (dissoc :configfile) - (assoc :backup-set (tarsnap/->TarsnapBackups (:configfile m))))) + (-> m (dissoc :configfile) (assoc :backup-set (tarsnap/->TarsnapBackups (:configfile m))))) -(defn zfs-reader - [m] - (-> m - (assoc :backup-set (zfs/->ZFS)))) +(defn zfs-reader [m] (-> m (assoc :backup-set (zfs/->ZFS)))) (def custom-readers - {:readers {'prune-backups/tarsnap tarsnap-reader - 'prune-backups/zfs zfs-reader}}) + {:readers {'prune-backups/tarsnap tarsnap-reader, 'prune-backups/zfs zfs-reader}}) -(defn -main [& args] - (try - (let [{:keys [help version config]} (cli/parse-opts args - {:spec spec - :restrict (keys spec)})] - (when version - (println meta/version) - (System/exit 0)) - (when help - (print-help)) +(defn -main + [& args] + (try (let [{:keys [help version config]} (cli/parse-opts args + {:spec spec, :restrict (keys spec)})] + (when version (println meta/version) (System/exit 0)) + (when help (print-help)) - (->> config slurp (edn/read-string custom-readers) run)) - (System/exit 0) - (finally - (shutdown-agents)))) + (->> config slurp (edn/read-string custom-readers) run)) + (System/exit 0) + (finally (shutdown-agents)))) -(when (= *file* (System/getProperty "babashka.file")) - (apply -main *command-line-args*)) +(when (= *file* (System/getProperty "babashka.file")) (apply -main *command-line-args*)) diff --git a/src/prune_backups/proto.clj b/src/prune_backups/proto.clj index 170bafe..c713509 100644 --- a/src/prune_backups/proto.clj +++ b/src/prune_backups/proto.clj @@ -1,5 +1,7 @@ (ns prune-backups.proto) (defprotocol BackupSet - (list-backups [this] "list all backups") - (destroy-backup [this backup] "destroy the given backup")) + (list-backups [this] + "list all backups") + (destroy-backup [this backup] + "destroy the given backup")) diff --git a/src/prune_backups/rotate.clj b/src/prune_backups/rotate.clj index d87fe66..80f7255 100644 --- a/src/prune_backups/rotate.clj +++ b/src/prune_backups/rotate.clj @@ -9,15 +9,11 @@ [^java.time.LocalDateTime dt] (.truncatedTo dt java.time.temporal.ChronoUnit/MINUTES)) -(defn hour-key - [^java.time.LocalDateTime dt] - (.truncatedTo dt java.time.temporal.ChronoUnit/HOURS)) +(defn hour-key [^java.time.LocalDateTime dt] (.truncatedTo dt java.time.temporal.ChronoUnit/HOURS)) (defn day-key [^java.time.LocalDateTime dt] - (.. dt - (truncatedTo java.time.temporal.ChronoUnit/DAYS) - (toLocalDate))) + (.. dt (truncatedTo java.time.temporal.ChronoUnit/DAYS) (toLocalDate))) (defn week-key [^java.time.LocalDateTime dt] @@ -33,49 +29,33 @@ (truncatedTo java.time.temporal.ChronoUnit/DAYS) (toLocalDate))) -(defn year-key - [^java.time.LocalDateTime dt] - (.getYear dt)) +(defn year-key [^java.time.LocalDateTime dt] (.getYear dt)) (defn keep-first-drop-rest [part] - (cons (assoc (first part) - ::keep? true) - (map (fn [elem] - (assoc elem - ::drop? true)) - (rest part)))) + (cons (assoc (first part) ::keep? true) (map (fn [elem] (assoc elem ::drop? true)) (rest part)))) (defn preserve* [p keyfn num-preserve] - (let [partitions (take num-preserve (partition-by keyfn (:rest p))) - this-result (mapcat keep-first-drop-rest partitions) + (let [partitions (take num-preserve (partition-by keyfn (:rest p))) + this-result (mapcat keep-first-drop-rest partitions) num-considered (count this-result)] - {:result (concat (:result p) this-result) - :rest (drop num-considered (:rest p))})) + {:result (concat (:result p) this-result), :rest (drop num-considered (:rest p))})) (defn drop-rest [p] - {:result (concat (:result p) - (mapv (fn [el] (assoc el ::drop? true)) - (:rest p))) - :rest nil}) + {:result (concat (:result p) (mapv (fn [el] (assoc el ::drop? true)) (:rest p))), :rest nil}) -(defn sort-backups - [xs get-datetime] - (sort-by get-datetime - (fn [a b] (compare b a)) - xs)) +(defn sort-backups [xs get-datetime] (sort-by get-datetime (fn [a b] (compare b a)) xs)) (defn rotate-backups [xs get-datetime & {:keys [minutely hourly daily weekly monthly yearly]}] - (cond-> {:result nil - :rest (sort-backups xs get-datetime)} - minutely (preserve* (comp minute-key get-datetime) minutely) - hourly (preserve* (comp hour-key get-datetime) hourly) - daily (preserve* (comp day-key get-datetime) daily) - weekly (preserve* (comp week-key get-datetime) weekly) - monthly (preserve* (comp month-key get-datetime) monthly) - yearly (preserve* (comp year-key get-datetime) yearly) - true drop-rest - true :result)) + (cond-> {:result nil, :rest (sort-backups xs get-datetime)} + minutely (preserve* (comp minute-key get-datetime) minutely) + hourly (preserve* (comp hour-key get-datetime) hourly) + daily (preserve* (comp day-key get-datetime) daily) + weekly (preserve* (comp week-key get-datetime) weekly) + monthly (preserve* (comp month-key get-datetime) monthly) + yearly (preserve* (comp year-key get-datetime) yearly) + true drop-rest + true :result)) diff --git a/src/prune_backups/tarsnap.clj b/src/prune_backups/tarsnap.clj index 9862cc8..a5a5e40 100644 --- a/src/prune_backups/tarsnap.clj +++ b/src/prune_backups/tarsnap.clj @@ -1,14 +1,12 @@ (ns prune-backups.tarsnap - (:require [clojure.string :as str] - [prune-backups.proto :as proto] - [babashka.process :as p])) + (:require [babashka.process :as p] + [clojure.string :as str] + [prune-backups.proto :as proto])) (set! *warn-on-reflection* true) (let [date-formatter (java.time.format.DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss")] - (defn parse-local-date - [s] - (java.time.LocalDateTime/parse s date-formatter))) + (defn parse-local-date [s] (java.time.LocalDateTime/parse s date-formatter))) (defn parse-list-archive [s] @@ -16,23 +14,18 @@ str/split-lines (mapv (fn [line] (let [[archive datetime] (str/split line #"\t")] - {:archive archive - :datetime (parse-local-date datetime)}))))) + {:archive archive, :datetime (parse-local-date datetime)}))))) (defn- tarsnap-process [configfile & params] - (let [cmd (if configfile - ["tarsnap" "--configfile" configfile] - ["tarsnap"]) + (let [cmd (if configfile ["tarsnap" "--configfile" configfile] ["tarsnap"]) cmd (into cmd params)] (p/check (p/process cmd {:out :string})))) (defrecord TarsnapBackups [configfile] proto/BackupSet - (list-backups [_] - (->> (tarsnap-process configfile "-v" "--list-archives") - :out - parse-list-archive)) - (destroy-backup [_ {:keys [archive]}] - (println "Destroying" archive) - (tarsnap-process configfile "-d" "-f" archive))) + (list-backups [_] + (->> (tarsnap-process configfile "-v" "--list-archives") :out parse-list-archive)) + (destroy-backup [_ {:keys [archive]}] + (println "Destroying" archive) + (tarsnap-process configfile "-d" "-f" archive))) diff --git a/src/prune_backups/zfs.clj b/src/prune_backups/zfs.clj index 9e27451..e9cd68e 100644 --- a/src/prune_backups/zfs.clj +++ b/src/prune_backups/zfs.clj @@ -1,14 +1,14 @@ (ns prune-backups.zfs - (:require [clojure.string :as str] - [prune-backups.proto :as proto] - [babashka.process :as p])) + (:require [babashka.process :as p] + [clojure.string :as str] + [prune-backups.proto :as proto])) (let [default-zone (java.time.ZoneId/systemDefault)] (defn epoch-second-to-local-date [e] - (let [inst (java.time.Instant/ofEpochSecond e) + (let [inst (java.time.Instant/ofEpochSecond e) zoned (.atZone inst default-zone) - dt (.toLocalDateTime zoned)] + dt (.toLocalDateTime zoned)] dt))) (defn parse-snapshot-list @@ -18,23 +18,21 @@ (mapv (fn [line] (let [[timestamp snapshot] (str/split line #"\t") timestamp (Long/parseLong timestamp)] - {:timestamp timestamp - :archive snapshot - :datetime (epoch-second-to-local-date timestamp)}))))) + {:timestamp timestamp, + :archive snapshot, + :datetime (epoch-second-to-local-date timestamp)}))))) (defn list-snapshots "zfs list -p -H -t snapshot -o creation,name,type,used" [] - (->>(p/process ["zfs" "list" "-p" "-H" "-t" "snapshot" "-o" "creation,name"] {:out :string}) - p/check - :out - parse-snapshot-list)) + (->> (p/process ["zfs" "list" "-p" "-H" "-t" "snapshot" "-o" "creation,name"] {:out :string}) + p/check + :out + parse-snapshot-list)) (defrecord ZFS [] proto/BackupSet - (list-backups [_] - (list-snapshots)) - (destroy-backup [_ b] - (println "deleting" (:archive b)) - (->> (p/process ["zfs" "destroy" "-r" (:archive b)]) - p/check))) + (list-backups [_] (list-snapshots)) + (destroy-backup [_ b] + (println "deleting" (:archive b)) + (->> (p/process ["zfs" "destroy" "-r" (:archive b)]) p/check))) diff --git a/tests.edn b/tests.edn index 9f8c03d..698028c 100644 --- a/tests.edn +++ b/tests.edn @@ -1,4 +1,2 @@ -#kaocha/v1 -{:color? true - :reporter [kaocha.report/documentation]} +#kaocha/v1 {:color? true, :reporter [kaocha.report/documentation]} ;:reporter [kaocha.report/dots]}