aboutsummaryrefslogtreecommitdiff
path: root/lib/state.lisp
blob: b98f5ba55b3b65afe429cdc400f6560f19417b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)))