refactoring: mv params to map

This commit is contained in:
Michael Jerger 2024-08-16 19:13:11 +02:00
parent 95f021586a
commit a941665f06
7 changed files with 91 additions and 14 deletions

View file

@ -9,16 +9,15 @@
[dda.backup.infrastructure :as i]))
(s/def ::config
(s/keys :req-un [::cd/restic-repository ::cd/backup-path]
(s/keys :req-un [::domain/files ::cd/restic-repository ::cd/backup-path]
:opt-un [::cd/certificate-file ::cd/directory ::cd/debug ::cd/dry-run]))
(defn-spec backup! nil?
[config ::config
files (s/* string?)]
[config ::config]
(let [config-w-defaults (merge core/default config)]
(mgm/unlock! config-w-defaults)
(i/execute!
(domain/backup-files-command config-w-defaults files)
(domain/backup-files-command config-w-defaults)
config-w-defaults)
(mgm/forget! config-w-defaults)
))

View file

@ -4,8 +4,14 @@
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]))
(s/def ::files (s/+ string?))
(s/def ::config
(s/keys :req-un [::files
::cd/restic-repository ::cd/backup-path]
:opt-un [::cd/certificate-file ::cd/password-file ::cd/directory]))
(defn-spec backup-files-command ::cd/commands
[config ::cd/config
files (s/* string?)]
(let [{:keys [certificate-file password-file restic-repository backup-path]} config]
[config ::config]
(let [{:keys [files]} config]
(cd/repo-command config (into ["backup"] files))))

View file

@ -1,26 +1,31 @@
(ns dda.backup.management.domain
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]))
(s/def ::config
(s/keys :req-un [::cd/restic-repository ::cd/backup-path ::cd/days-to-keep ::cd/months-to-keep]
:opt-un [::cd/certificate-file ::cd/password-file]))
(defn-spec check-repo-command ::cd/commands
[config ::cd/config]
[config ::config]
(cd/repo-command config ["check"]))
(defn-spec init-repo-command ::cd/commands
[config ::cd/config]
[config ::config]
(cd/repo-command config ["init"]))
(defn-spec unlock-repo-command ::cd/commands
[config ::cd/config]
[config ::config]
(cd/repo-command config ["--cleanup-cache" "unlock"]))
(defn-spec list-snapshot-command ::cd/commands
[config ::cd/config]
[config ::config]
(cd/repo-command config ["snapshots"]))
(defn-spec forget-command ::cd/commands
[config ::cd/config]
[config ::config]
(let [{:keys [days-to-keep months-to-keep]} config]
(cd/repo-command config ["forget" "--group-by" ""
"--keep-last" "1"

View file

@ -0,0 +1,24 @@
(ns dda.backup.restore
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]
[dda.backup.restore.domain :as domain]
[dda.backup.core :as core]
[dda.backup.management :as mgm]
[dda.backup.infrastructure :as i]))
(def default (merge core/default {:snapshot-id "latest"}))
(s/def ::config
(s/keys :req-un [::domain/target-directory
::cd/restic-repository ::cd/backup-path]
:opt-un [::domain/snapshot-id ::cd/certificate-file ::cd/directory ::cd/debug ::cd/dry-run]))
(defn-spec restore! nil?
[config ::config]
(let [config-w-defaults (merge default config)]
(mgm/unlock! config-w-defaults)
(i/execute!
(domain/restore-dir-command config-w-defaults)
config-w-defaults)))

View file

@ -0,0 +1,19 @@
(ns dda.backup.restore.domain
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]))
(s/def ::target-directory string?)
(s/def ::snapshot-id string?)
(s/def ::config
(s/keys :req-un [::target-directory ::snapshot-id
::cd/restic-repository ::cd/backup-path]
:opt-un [::cd/certificate-file ::cd/password-file ::cd/directory]))
(defn-spec restore-dir-command ::cd/commands
[config ::config]
(let [{:keys [target-directory snapshot-id]} config]
(into [["rm" "-rf" target-directory]]
(cd/repo-command config ["restore" snapshot-id "--target" target-directory]))))

View file

@ -19,5 +19,5 @@
:backup-path "dir-at-repo"
:directory "dir-to-backup"
:days-to-keep 39
:months-to-keep 3}
["file2" "file2"]))))
:months-to-keep 3
:files ["file2" "file2"]}))))

View file

@ -0,0 +1,24 @@
(ns dda.backup.restore.domain-test
(:require
[clojure.test :refer [deftest is are testing run-tests]]
[clojure.spec.test.alpha :as st]
[dda.backup.restore.domain :as cut]))
(st/instrument `cut/restore-dir-command)
(deftest should-calculate-restore-dir
(is (= [["rm" "-rf" "dir-to-backup"]
["restic"
"-r"
"repo/dir-at-repo"
"-v"
"restore"
"latest"
"--target"
"dir-to-backup"]]
(cut/restore-dir-command {:restic-repository "repo"
:backup-path "dir-at-repo"
:target-directory "dir-to-backup"
:days-to-keep 39
:months-to-keep 3
:snapshot-id "latest"}))))