aboutsummaryrefslogtreecommitdiff
path: root/lib/state.lisp
blob: 0c01d4d8a257b0cff9cb637fe3fe3ed07b8f59ac (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
;;;; state.lisp -- functions for dealing with client state

;; Copyright (C) 2022  Colin Okay

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.

;; 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

(defstruct config
  handle
  api-token
  host
  shell)

(defvar *config* nil
  "Holds a config struct instance.")

(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)
  "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))

;;; 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 api:*host* () "ol must be configured with a server host.")
     (set-term-width)
     (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)))))