read / write state

This commit is contained in:
Michael Jerger 2024-04-05 10:42:54 +02:00
parent 678b75ae6f
commit 56bc215f26
2 changed files with 35 additions and 1 deletions

1
.gitignore vendored
View file

@ -111,3 +111,4 @@ venv.bak/
.lsp/ .lsp/
.calva/ .calva/
.cpcache/ .cpcache/
infrastructure/backup/image/resources/backup-repository-state.edn

View file

@ -0,0 +1,33 @@
#! /usr/bin/env bb
(ns restic-management
(:require
[clojure.spec.alpha :as s]
[clojure.java.io :as io]
[clojure.edn :as edn]))
(s/def ::state string?)
(s/def ::backup-repository-state
(s/keys :req-un [::state]))
(def state {:state ""})
(defn store-backup-repository-state [s]
(spit "backup-repository-state.edn" s))
(defn read-backup-repository-state []
(try
(with-open [r (io/reader "backup-repository-state.edn")]
(edn/read (java.io.PushbackReader. r)))
(catch java.io.IOException e
(printf "Couldn't open '%s': %s\n" "backup-repository-state.edn" (.getMessage e)))
(catch RuntimeException e
(printf "Error parsing edn file '%s': %s\n" "backup-repository-state.edn" (.getMessage e)))))
(println (read-backup-repository-state))
(println (:state (read-backup-repository-state)))
(println (s/valid? ::backup-repository-state (read-backup-repository-state)))