aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.lisp115
1 files changed, 56 insertions, 59 deletions
diff --git a/src/lib.lisp b/src/lib.lisp
index 20f75ea..2480dab 100644
--- a/src/lib.lisp
+++ b/src/lib.lisp
@@ -3,62 +3,71 @@
(defpackage oneliners.cli
(:use :cl)
(:local-nicknames (#:api #:oneliners.api-client)))
-(in-package :oneliners.cli)
-;;; CLI OPTIONS
-
-;; (opts:define-opts
-;; (:name :add
-;; :description "Intaractively add a oneliner to the a wiki."
-;; :long "add")
-;; (:name :tags
-;; :description "A comma separated list of tags to filter search results."
-;; :short #\t
-;; :long "tags"
-;; :arg-parser #'identity
-;; :meta-var "'T1, T2, ...'")
-;; (:name :limit
-;; :description "An integer. The maximum number of results to return."
-;; :short #\l
-;; :long "limit"
-;; :meta-var "N"
-;; :default 10
-;; :arg-parser #'parse-integer)
-;; (:name :edit
-;; :description "An integer, a result number. Interactively edit af command."
-;; :long "edit"
-;; :meta-var "RESULT"
-;; :arg-parser #'parse-integer)
-;; (:name :not-flagged
-;; :description "Filter flagged oneliners from the search results"
-;; :long "not-flagged"))
-
-(defparameter +help-suffix+
- "Unless RESULT is an integer, search for oneliners that involve each command in COMMANDS.
-E.g.
- # search for oneliners involving both 'find' and 'grep'
- ol find grep
-
-Otherwise, when RESULT is an integer, run the oneliner with index RESULT from the most
-recent search command, and supply ARGS to that oneliner, if provided.
-E.g.
- # run the third result from the last search with arguments foo and bar
- ol 3 foo bar")
-
-;; (defun help-text ()
-;; (opts:describe :prefix "Oneliners Wiki Command Line Tool"
-;; :args "[[RESULT ARGS | COMMANDS]"
-;; :usage-of "ol"
-;; :suffix +help-suffix+))
+(in-package :oneliners.cli)
;;; CONFIG AND RESULTS FILE LOCATIONS
+(defvar *config* nil
+ "A configuration plist")
+
+(defun make-config (&key host api-token)
+ (append (when host (list :host host))
+ (when api-token (list :api-token api-token))))
+
+(defun valid-config-p (config)
+ (and (listp config)
+ (evenp (length config))
+ (stringp (getf config :host))
+ t))
+
+(defun write-default-config-to-disk ()
+ (let ((conf-file (config-file)))
+ (ensure-directories-exist conf-file)
+ (with-open-file (out conf-file :direction :output)
+ (print (make-config :host "https://oneliners.wiki") out))))
+
+(defun fetch-config-from-disk ()
+ (let ((conf
+ (uiop:with-safe-io-syntax ()
+ (uiop:read-file-form (config-file)))))
+ (assert (valid-config-p conf) () "Invalid configuration file")
+ (setf *config* conf)))
+
+(defun ensure-config ()
+ (unless (uiop:file-exists-p (config-file))
+ (write-default-config-to-disk))
+ (fetch-config-from-disk))
+
+(defun host () (getf *config* :host))
+(defun api-token () (getf *config* :api-token))
+
(defun config-file ()
- (merge-pathnames ".config/oneliners.config" (user-homedir-pathname) ))
+ (merge-pathnames ".config/oneliners.config" (user-homedir-pathname)))
(defun last-search-file ()
(merge-pathnames ".last_oneliners_search" (user-homedir-pathname)))
+(defun fetch-nth-oneliner (n)
+ "Returns nil if there is no nth oneliner from the search history."
+ (when (uiop:file-exists-p (last-search-file))
+ (nth n (uiop:read-file-form (last-search-file)))))
+
+;;; SEARCHNG THE WIKI
+
+(defun search-for-oneliners (terms limit not-flagged-p)
+ (assert (loop for term in terms never (find #\, term) ))
+ (handler-case
+ (api:request-with
+ (:host (host))
+ (api:get--search :tags (str:join ",")
+ :limit limit
+ :notflagged (if not-flagged-p "true" "false")))
+ (dex:http-request-failed (e)
+ (format *error-output* "~a -- ~a"
+ (dex:response-status e)
+ (dex:response-body e)))))
+
;;; RUNNING COMMANDS
(defun parent-process-name ()
@@ -103,15 +112,3 @@ printed to OUTPUT-STREAM. "
do (princ (read-line shell-output) output-stream)
(terpri output-stream))))))
-;;; main
-
-;; (defun main ()
-;; (handler-case
-;; (multiple-value-bind (options free-args) (opts:get-opts)
-;; (print (list :options options :free-args free-args))
-;; (terpri)
-;; (uiop:quit))
-;; (unix-opts:unknown-option (err)
-;; (declare (ignore err))
-;; (princ (help-text))
-;; (terpri))))