diff --git a/src/cli_cms/cli_utils.clj b/src/cli_cms/cli_utils.clj index 802db2f..f228159 100644 --- a/src/cli_cms/cli_utils.clj +++ b/src/cli_cms/cli_utils.clj @@ -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))) diff --git a/src/cli_cms/delete.clj b/src/cli_cms/delete.clj index 218f5de..8793e78 100644 --- a/src/cli_cms/delete.clj +++ b/src/cli_cms/delete.clj @@ -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)))) diff --git a/src/cli_cms/edit.clj b/src/cli_cms/edit.clj index cab0a7b..0a1f074 100644 --- a/src/cli_cms/edit.clj +++ b/src/cli_cms/edit.clj @@ -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)))) diff --git a/src/cli_cms/main.clj b/src/cli_cms/main.clj index b1eb2a5..d5f24fd 100644 --- a/src/cli_cms/main.clj +++ b/src/cli_cms/main.clj @@ -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.")))