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])) [dda.backup.infrastructure :as i]))
(s/def ::config (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])) :opt-un [::cd/certificate-file ::cd/directory ::cd/debug ::cd/dry-run]))
(defn-spec backup! nil? (defn-spec backup! nil?
[config ::config [config ::config]
files (s/* string?)]
(let [config-w-defaults (merge core/default config)] (let [config-w-defaults (merge core/default config)]
(mgm/unlock! config-w-defaults) (mgm/unlock! config-w-defaults)
(i/execute! (i/execute!
(domain/backup-files-command config-w-defaults files) (domain/backup-files-command config-w-defaults)
config-w-defaults) config-w-defaults)
(mgm/forget! config-w-defaults) (mgm/forget! config-w-defaults)
)) ))

View file

@ -4,8 +4,14 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd])) [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 (defn-spec backup-files-command ::cd/commands
[config ::cd/config [config ::config]
files (s/* string?)] (let [{:keys [files]} config]
(let [{:keys [certificate-file password-file restic-repository backup-path]} config]
(cd/repo-command config (into ["backup"] files)))) (cd/repo-command config (into ["backup"] files))))

View file

@ -1,26 +1,31 @@
(ns dda.backup.management.domain (ns dda.backup.management.domain
(:require (:require
[orchestra.core :refer [defn-spec]] [orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd])) [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 (defn-spec check-repo-command ::cd/commands
[config ::cd/config] [config ::config]
(cd/repo-command config ["check"])) (cd/repo-command config ["check"]))
(defn-spec init-repo-command ::cd/commands (defn-spec init-repo-command ::cd/commands
[config ::cd/config] [config ::config]
(cd/repo-command config ["init"])) (cd/repo-command config ["init"]))
(defn-spec unlock-repo-command ::cd/commands (defn-spec unlock-repo-command ::cd/commands
[config ::cd/config] [config ::config]
(cd/repo-command config ["--cleanup-cache" "unlock"])) (cd/repo-command config ["--cleanup-cache" "unlock"]))
(defn-spec list-snapshot-command ::cd/commands (defn-spec list-snapshot-command ::cd/commands
[config ::cd/config] [config ::config]
(cd/repo-command config ["snapshots"])) (cd/repo-command config ["snapshots"]))
(defn-spec forget-command ::cd/commands (defn-spec forget-command ::cd/commands
[config ::cd/config] [config ::config]
(let [{:keys [days-to-keep months-to-keep]} config] (let [{:keys [days-to-keep months-to-keep]} config]
(cd/repo-command config ["forget" "--group-by" "" (cd/repo-command config ["forget" "--group-by" ""
"--keep-last" "1" "--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" :backup-path "dir-at-repo"
:directory "dir-to-backup" :directory "dir-to-backup"
:days-to-keep 39 :days-to-keep 39
:months-to-keep 3} :months-to-keep 3
["file2" "file2"])))) :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"}))))