aboutsummaryrefslogtreecommitdiff
path: root/lib/state.lisp
diff options
context:
space:
mode:
authorColin Okay <okay@toyful.space>2022-03-11 08:35:55 -0600
committerColin Okay <okay@toyful.space>2022-03-11 08:35:55 -0600
commit08ba2769abb7a36817a725d30d64cfd36f5bcf32 (patch)
tree3068a5d9e1a4afd06a5f8b236658d4094e30dfef /lib/state.lisp
parent62afeb3f21d3d8e8db045a001271e686e944a049 (diff)
separated app and lib modules, -osicat dep, +packages.lisp
Diffstat (limited to 'lib/state.lisp')
-rw-r--r--lib/state.lisp62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/state.lisp b/lib/state.lisp
new file mode 100644
index 0000000..b98f5ba
--- /dev/null
+++ b/lib/state.lisp
@@ -0,0 +1,62 @@
+;;;; state.lisp -- functions for dealing with client state
+
+(in-package :oneliners.cli)
+
+;;; Config Struct
+
+(defstruct config
+ handle
+ api-token
+ host
+ shell)
+
+(defvar *config* nil
+ "Holds a config struct instance.")
+
+(defvar *cache* nil
+ "Holds cached oneliners as a list.")
+
+;;; GETTING AND SETTING STATE, DYNAMICALLY BOUND
+
+(defun merge-oneliners (new)
+ "Modifies *CACHE*. Merge updated oneliners into the *cache*, ensuring to remove old versions."
+ (setf *cache*
+ (nconc
+ new
+ (delete-if
+ (lambda (old-oneliner)
+ (find (oneliner-id old-oneliner)
+ new
+ :key #'oneliner-id
+ :test #'equal))
+ *cache*))))
+
+(defun get-cached (id-or-name)
+ "Looks up a oneliner instance by ID-OR-NAME using the current binding of *cache*. "
+ (find id-or-name
+ *cache*
+ :key (etypecase id-or-name
+ (integer #'oneliner-id)
+ (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)))
+