get edit command started with snippet select and editing
This commit is contained in:
parent
c4ff9a86b6
commit
e71062bb87
4 changed files with 50 additions and 7 deletions
|
|
@ -4,8 +4,15 @@
|
|||
[clojure.pprint :refer [pprint]]
|
||||
[babashka.process :refer [shell]]))
|
||||
|
||||
(defn select [l]
|
||||
(str/trim (:out (apply shell {:out :string} (concat '("gum" "filter") l)))))
|
||||
(defn select
|
||||
"Select from a list of options using gum filter.
|
||||
Args:
|
||||
l list of {:item any :value string}"
|
||||
[l]
|
||||
(let [values (map :value l)
|
||||
selected-value (str/trim (:out (apply shell {:out :string} (concat '("gum" "filter") values))))
|
||||
selected-item (:item (first (filter #(= (:value %) selected-value) l)))]
|
||||
selected-item))
|
||||
|
||||
;; (defn print [text]
|
||||
;; (shell (format "gum style --foreground 212 %s" text)))
|
||||
|
|
|
|||
|
|
@ -14,7 +14,5 @@
|
|||
(http/delete (str (:backend-host config) "/api/snippet") {:query-params {"id" id}}))
|
||||
|
||||
(defn run []
|
||||
(let [snippets (fetch-snippets)
|
||||
selected-slug (utils/select (map :slug snippets))
|
||||
selected-snippet (first (filter #(= (:slug %) selected-slug) snippets))]
|
||||
(let [selected-snippet (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
|
||||
(delete-snippet (:id selected-snippet))))
|
||||
|
|
|
|||
|
|
@ -1 +1,38 @@
|
|||
(ns cli-cms.edit)
|
||||
(ns cli-cms.edit
|
||||
(:require
|
||||
[babashka.http-client :as http]
|
||||
[clojure.pprint :refer [pprint]]
|
||||
[cli-cms.config :refer [config]]
|
||||
[cheshire.core :as json]
|
||||
[cli-cms.cli-utils :as utils]))
|
||||
|
||||
(defn fetch-snippets []
|
||||
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
||||
(json/parse-string (:body res) true)))
|
||||
|
||||
;; (defn fetch-snippet [id]
|
||||
;; (let [res (http/get (str (:backend-host config) "/api/snippet") {:query-params {"id" id}})]
|
||||
;; (json/parse-string (:body res) true)))
|
||||
(defn prompt-for-edit-type []
|
||||
(utils/select '({:value "title" :item :title}
|
||||
{:value "markdown" :item :markdown}
|
||||
{:value "slug" :item :slug}
|
||||
{:value "tags" :item :tags}
|
||||
{:value "done" :item :done})))
|
||||
|
||||
;; (defn send-patch [{:keys [title slug markdown tags]}]
|
||||
;; (http/post (str (:backend-host config) "/api/snippet")
|
||||
;; {:headers {:content-type "application/json"}
|
||||
;; :body (json/encode {:title title :slug slug :markdown markdown :tags tags})}))
|
||||
|
||||
(defn edit [snippet]
|
||||
(case (prompt-for-edit-type)
|
||||
:title (edit (assoc snippet :title (utils/prompt-for "title")))
|
||||
:slug (edit (assoc snippet :slug (utils/prompt-for "slug")))
|
||||
:tags (edit (assoc snippet :tags (utils/prompt-for-many "tags")))
|
||||
:markdown (edit (assoc snippet :markdown (utils/prompt-for-long-form "markdown")))
|
||||
:done snippet))
|
||||
|
||||
(defn run []
|
||||
(let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
|
||||
(pprint (edit snippet-to-edit))))
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
(ns cli-cms.main
|
||||
(:require
|
||||
[cli-cms.create :as create]
|
||||
[cli-cms.edit :as edit]
|
||||
[cli-cms.delete :as delete]))
|
||||
|
||||
(defn -main [& args]
|
||||
(case (first args)
|
||||
"create" (create/run)
|
||||
"delete" (delete/run)
|
||||
"edit" (println "TODO: implment edit snippet")
|
||||
"edit" (edit/run)
|
||||
(println "Missing command. Try create, edit, or delete.")))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue