From 8f9b59690660f85aeae675989fe9fe6b1b830445 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Fri, 11 Mar 2022 09:22:58 -0600 Subject: separated app from lib systems; added with-client-state --- lib/state.lisp | 68 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 21 deletions(-) (limited to 'lib/state.lisp') 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 . - (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)))) -- cgit v1.2.3