aboutsummaryrefslogtreecommitdiff
path: root/lib/state.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/state.lisp')
-rw-r--r--lib/state.lisp57
1 files changed, 50 insertions, 7 deletions
diff --git a/lib/state.lisp b/lib/state.lisp
index 0c01d4d..676ff10 100644
--- a/lib/state.lisp
+++ b/lib/state.lisp
@@ -20,14 +20,36 @@
;;; Config Struct
(defstruct config
- handle
- api-token
- host
- shell)
+ (handle "")
+ (api-token "")
+ (host "")
+ (shell "bash"))
+
+;;; DYNAMIC VARS FOR CONFIG AND CACHE, AND SOME GETTERS
(defvar *config* nil
"Holds a config struct instance.")
+(defun api-token ()
+ (a:if-let (token (config-api-token *config*))
+ token
+ (error () "No API TOKEN")))
+
+(defun (setf api-token) (newvalue)
+ (setf (config-api-token *config*) newvalue))
+
+(defun handle ()
+ (config-handle *config*))
+
+(defun (setf handle) (newvalue)
+ (setf (config-handle *config*) newvalue))
+
+(defun host ()
+ (config-host *config*))
+
+(defun shell ()
+ (config-shell *config*))
+
(defvar *cache* nil
"Holds cached oneliners as a list.")
@@ -46,7 +68,10 @@
(uiop:delete-file-if-exists (cached-oneliners-file)))
(defun write-config-to-disk ()
- (print-to-file *config* (config-file)))
+ (print-to-file
+ (with-slots (handle api-token host shell) *config*
+ (list :handle handle :api-token api-token :host host :shell shell))
+ (config-file)))
(defun write-cache-to-disk ()
(print-to-file *cache* (cached-oneliners-file)))
@@ -54,13 +79,31 @@
(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)))
+ (a:when-let ((conf
+ (read-from-file (config-file))))
+ (apply 'make-config conf)))
(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)))
+(defun make-fresh-config ()
+ "Prompts the user to supply some values for a config file."
+ (format t "It seems you are calling `ol` for the first time. Running Setup~%~%")
+ (make-config
+ :host (prompt "Oneliner Server Host: "
+ :prefill "https://api.oneliners.wiki")
+ :shell (prompt "With which shell should oneliners be run? "
+ :prefill "bash")))
+
+(defun ensure-config ()
+ "Ensures that a configuration file exists on disk, prompting the
+user for some input if it does not."
+ (if (uiop:file-exists-p (config-file))
+ (read-config-file)
+ (make-fresh-config)))
+
;;; GETTING AND SETTING STATE, DYNAMICALLY BOUND
(defun merge-oneliners (new)
@@ -90,7 +133,7 @@ CACHED-ONELINERS-FILE. NIL if there is no such file."
(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))
+ `(let* ((*config* (ensure-config))
(*cache* (read-cache-file))
(api:*host* (config-host *config*)))
(assert api:*host* () "ol must be configured with a server host.")