aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/app.lisp7
-rw-r--r--lib/client.lisp106
-rw-r--r--lib/oneliner.lisp15
3 files changed, 89 insertions, 39 deletions
diff --git a/app/app.lisp b/app/app.lisp
index 86c7328..8e89a93 100644
--- a/app/app.lisp
+++ b/app/app.lisp
@@ -124,6 +124,10 @@ export EDITOR=/usr/bin/zile
(text :contents "Usage: ol edit <IDENTIFIER>")
(text :contents " ")
(text :contents "Interactively alter a oneliner and uplaod it to the server."))
+ (group (:header "PUBLISHING ONELINERS" :hidden t)
+ (text :contents "Usage: ol publish <DRAFT>")
+ (text :contents " ")
+ (text :contents "Submits a draft oneliner to the wiki server, and, when successful, deletes the draft."))
(group (:header "FLAGGING AND UNFLAGGING ONELINERS" :hidden t)
(text :contents "Usage: ol <flag | unflag> <IDENTIFIER>")
(text :contents " ")
@@ -171,6 +175,7 @@ export EDITOR=/usr/bin/zile
(text :contents "show")
(text :contents "new")
(text :contents "edit")
+ (text :contents "publish")
(text :contents "flag")
(text :contents "lock")
(text :contents "redeem")
@@ -228,6 +233,8 @@ than the users."
(cli:add-new-oneliner))
(:edit
(cli:edit-item id-or-name))
+ (:publish
+ (cli::publish-draft id-or-name))
(:flag
(cli:flag-item id-or-name))
(:unflag
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))))