aboutsummaryrefslogtreecommitdiff
path: root/lib/state.lisp
diff options
context:
space:
mode:
authorColin Okay <okay@toyful.space>2022-03-11 09:22:58 -0600
committerColin Okay <okay@toyful.space>2022-03-11 09:22:58 -0600
commit8f9b59690660f85aeae675989fe9fe6b1b830445 (patch)
treef05b323c50385de88a4762a9c1ee370ae6cf100f /lib/state.lisp
parente73fed7afb45d37a248f7b95d4a51bb4807a14a2 (diff)
separated app from lib systems; added with-client-state
Diffstat (limited to 'lib/state.lisp')
-rw-r--r--lib/state.lisp68
1 files changed, 47 insertions, 21 deletions
diff --git a/lib/state.lisp b/lib/state.lisp
index b0be21c..5bab3ed 100644
--- a/lib/state.lisp
+++ b/lib/state.lisp
@@ -15,7 +15,6 @@
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
-
(in-package :oneliners.cli)
;;; Config Struct
@@ -32,6 +31,36 @@
(defvar *cache* nil
"Holds cached oneliners as a list.")
+;;; LOADING AND SAVING STATE
+
+(defun config-file ()
+ "Returns the pahtname holding the location of the config file."
+ (merge-pathnames ".config/oneliners.config" (user-homedir-pathname)))
+
+(defun cached-oneliners-file ()
+ "Returns the pathname holding the location of the cache."
+ (merge-pathnames ".cache/oneliners.cache" (user-homedir-pathname)))
+
+(defun wipe-cache ()
+ "Deletes the cache, if present."
+ (uiop:delete-file-if-exists (cached-oneliners-file)))
+
+(defun write-config-to-disk ()
+ (print-to-file *config* (config-file)))
+
+(defun write-cache-to-disk ()
+ (print-to-file *cache* (cached-oneliners-file)))
+
+(defun read-config-file ()
+ "Read a configuration from the location returned by CONFIG-FILE. NIL
+if there is no such file"
+ (read-from-file (config-file)))
+
+(defun read-cache-file ()
+ "Read the cache from the location returned by
+CACHED-ONELINERS-FILE. NIL if there is no such file."
+ (read-from-file (cached-oneliners-file)))
+
;;; GETTING AND SETTING STATE, DYNAMICALLY BOUND
(defun merge-oneliners (new)
@@ -56,23 +85,20 @@
(string #'oneliner-name))
:test #'equal))
-;;; LOADING AND SAVING STATE
-
-(defun config-file ()
- "Returns the pahtname holding the location of the config file."
- (merge-pathnames ".config/oneliners.config" (user-homedir-pathname)))
-
-(defun cached-oneliners-file ()
- "Returns the pathname holding the location of the cache."
- (merge-pathnames ".cache/oneliners.cache" (user-homedir-pathname)))
-
-(defun wipe-cache ()
- "Deletes the cache, if present."
- (uiop:delete-file-if-exists (cached-oneliners-file)))
-
-(defun write-config-to-disk ()
- (print-to-file *config* (config-file)))
-
-(defun write-cache-to-disk ()
- (print-to-file *cache* (cached-oneliners-file)))
-
+;;; STATE LOADING MACRO
+
+(defmacro with-local-state (&body body)
+ "Binds the *config* and *cache* dynamic variables from disk, and
+sets the api's *host* variable. If BODY produces no errors, the "
+ `(let* ((*config* (read-config-file))
+ (*cache* (read-cache-file))
+ (api::*host* (config-host *config*)))
+ (assert *host* () "ol must be configured with a server host.")
+ (handler-case
+ (progn
+ ,@body
+ ;; only if there is no error do we save the local state.
+ (write-cache-to-disk)
+ (write-config-to-disk)))
+ (error (e)
+ (format *error-output* "~a~%" e))))