From 7bcf634c789c00a95237ad74e5f923a4214020c8 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Sun, 13 Mar 2022 14:21:33 -0500 Subject: can make, run, and publish drafts --- lib/client.lisp | 106 ++++++++++++++++++++++++++++++++++-------------------- lib/oneliner.lisp | 15 ++++++++ 2 files changed, 82 insertions(+), 39 deletions(-) (limited to 'lib') diff --git a/lib/client.lisp b/lib/client.lisp index db19bda..a72b991 100644 --- a/lib/client.lisp +++ b/lib/client.lisp @@ -125,7 +125,6 @@ running the body. If such a oneliner can be found." ;;; ADDING ONELINERS - (defun add-new-oneliner () (api-token) ;; fails with error if not set. ;; read each field required to make a onelienr in from a prompt. @@ -171,31 +170,28 @@ running the body. If such a oneliner can be found." (when (y-or-n-p "Provide an explanation?") (string-from-editor (format nil "~a~%~%" oneliner-string))))) - (if draft-name - ;; if this is a draft, save it to disk. - (progn - (put-draft draft-name - (make-oneliner - :oneliner oneliner-string - :name name - :tags tags - :brief brief - :explanation explanation - :runstyle runstyle)) - (format t "Saved draft ~a~%" draft-name)) - ;; otherwise, format the oneliner as json and make a request - ;; to create a new oneliner in the wiki - (api:request-with - (:body (jonathan:to-json - (list :oneliner oneliner-string - :name (if (plusp (length name)) name :null) - :tags tags - :brief brief - :explanation explanation - :runstyle runstyle)) - :content-type "application/json") - (api:post--oneliner :token (api-token)) ;TODO: update api to return the instance created. - (format t "Added Oneliner~%"))))) + (let ((local + (make-oneliner + :oneliner oneliner-string + :name name + :tags tags + :brief brief + :explanation explanation + :runstyle runstyle))) + (if draft-name + ;; if this is a draft, save it to disk. + (progn + (put-draft draft-name local) + (format t "Saved draft ~a~%Do `ol --draft run ~a` to test~%" + draft-name + draft-name)) + ;; otherwise, format the oneliner as json and make a request + ;; to create a new oneliner in the wiki + (api:request-with + (:body (oneliner-to-json local) + :content-type "application/json") + (api:post--oneliner :token (api-token)) ;TODO: update api to return the instance created. + (format t "Added Oneliner~%")))))) ;;; EDITING ONELINERS @@ -216,13 +212,23 @@ running the body. If such a oneliner can be found." :expect 'valid-oneliner-name-p :retry-text "Must begin with a letter contain only letters, numbers, - and _." :prefill (or (oneliner-name ol) "")))) + + (draft-name + (unless (y-or-n-p "Upload edits immediately instead of keeping a draft?") + (if (plusp (length name)) + name + (prompt "No name was provided, name this draft: " + :expect 'valid-oneliner-name-p + :retry-text "Must begin with a letter contain only letters, numbers, - and _.")))) (brief (prompt "Brief Description: " :expect 'valid-brief-description-p :retry-text "Too long. Must be <= 72 characters: " :prefill (oneliner-brief ol))) + (init-tags (parse-oneliner-tags oneliner-string)) + (tags (progn (format t "Tags include: ~{~a ~}~%" init-tags) @@ -240,22 +246,44 @@ running the body. If such a oneliner can be found." :expect 'valid-runstyle-p :retry-text "Must be (auto or manual): " :prefill (oneliner-runstyle ol)))) + (explanation - (when (y-or-n-p "Provide an explanation?") + (when (y-or-n-p "Alter the explanation?") (string-from-editor (oneliner-explanation ol))))) - (let ((new-item - (list :oneliner oneliner-string - :tags tags - :brief brief - :name (if (plusp (length name)) name :null) - :explanation explanation - :runstyle runstyle))) - (api:request-with - (:body (jonathan:to-json new-item) - :content-type "application/json") + + (let ((local + (make-oneliner + :id (oneliner-id ol) + :oneliner oneliner-string + :name (if (plusp (length name)) name :null) + :tags tags + :brief brief + :explanation explanation + :runstyle runstyle))) + (if draft-name + (progn + (put-draft draft-name local) + (format t "Saved draft ~a~%Do `ol --draft run ~a` to test~%" + draft-name + draft-name)) + (api:request-with + (:body (oneliner-to-json-body local) + :content-type "application/json") + (api:patch--oneliner-entry-edit (oneliner-id ol) :token (api-token)) + ;(merge-oneliners (list new-item)) ;;TODO: this is broken, wait for API update. + (format t "Edits accepted~%"))))))) + +(defun publish-draft (name) + (when-draft (ol name) + (api:request-with + (:body (oneliner-to-json-body ol) + :content-type "application/json") + (if (oneliner-id ol) (api:patch--oneliner-entry-edit (oneliner-id ol) :token (api-token)) - (merge-oneliners (list new-item)) ;;TODO: this is broken, wait for API update. - (format t "OK~%")))))) + (api:post--oneliner :token (api-token))) + ;; if that worked, no http error occured, so this next part will run + (drop-draft name) + (format t "Draft ~a published and removed from drafts.~%" name)))) ;;; ADMIN OF ONELINER ENTRIES diff --git a/lib/oneliner.lisp b/lib/oneliner.lisp index 4828b2d..668a82c 100644 --- a/lib/oneliner.lisp +++ b/lib/oneliner.lisp @@ -94,3 +94,18 @@ (string-trim '(#\space) (alexandria-2:subseq* (oneliner-brief ol) x (+ x *term-width*))))) (format t "~%~a~%~%" (oneliner-oneliner ol)))) + +;;;; json serialization + +(defun oneliner-to-json-body (ol) + "Takes a oneliner structure and produces some json suitable for +sending to the server. ID and some other fields are omitted." + (jonathan:to-json + (list :oneliner (oneliner-oneliner ol) + :tags (oneliner-tags ol) + :brief (oneliner-brief ol) + :name (if (plusp (length (oneliner-name ol))) + (oneliner-name ol) + :null) + :explanation (oneliner-explanation ol) + :runstyle (oneliner-runstyle ol)))) -- cgit v1.2.3