From 53acc339b38d8b966915a10f22611145688d0084 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sun, 8 Jun 2025 20:35:06 +0200 Subject: [PATCH] add delete command --- src/cli_cms/cli_utils.clj | 8 ++++++-- src/cli_cms/delete.clj | 34 ++++++++++++++++++++++++++++++++++ src/cli_cms/main.clj | 5 +++-- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/cli_cms/delete.clj diff --git a/src/cli_cms/cli_utils.clj b/src/cli_cms/cli_utils.clj index 38a7cfa..802db2f 100644 --- a/src/cli_cms/cli_utils.clj +++ b/src/cli_cms/cli_utils.clj @@ -1,10 +1,14 @@ (ns cli-cms.cli-utils (:require [clojure.string :as str] + [clojure.pprint :refer [pprint]] [babashka.process :refer [shell]])) -(defn print [text] - (shell (format "gum style --foreground 212 %s" text))) +(defn select [l] + (str/trim (:out (apply shell {:out :string} (concat '("gum" "filter") l))))) + +;; (defn print [text] +;; (shell (format "gum style --foreground 212 %s" text))) (defn prompt-for [placeholder] (str/trim (-> (shell {:out :string} (format "gum input --placeholder='%s'" placeholder)) :out))) diff --git a/src/cli_cms/delete.clj b/src/cli_cms/delete.clj new file mode 100644 index 0000000..49dd706 --- /dev/null +++ b/src/cli_cms/delete.clj @@ -0,0 +1,34 @@ +(ns cli-cms.delete + (:require + [cli-cms.cli-utils :as utils] + [clojure.pprint :refer [pprint]] + [babashka.http-client :as http] + [cheshire.core :as json])) + +;; (defn create-snippet [{:keys [title slug markdown tags]}] +;; (http/post "http://localhost:8080/api/snippet" +;; {:headers {:content-type "application/json"} +;; :body (json/encode {:title title :slug slug :markdown markdown :tags tags})})) +;; + +(defn fetch-snippets [] + (let [res (http/get "http://localhost:8080/api/snippet?limit=25")] + (json/parse-string (:body res) true))) + +(defn delete-snippet [id] + (http/delete "http://localhost:8080/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))] + (delete-snippet (:id selected-snippet)))) + +;; (some #(when (> % 5) %) [1 3 7 2]) + ;; (some #(when) (utils/select (map :slug snippets))))) + ;; (utils/print "Please enter a title:") + ;; (println "Please enter a title:") + ;; (println (format "TODO: create post with title: %s and slug %s" title slug)) + ;; (println tags) + ;; (println markdown) + ;; (create-snippet {:title title :slug slug :markdown markdown :tags tags}))) diff --git a/src/cli_cms/main.clj b/src/cli_cms/main.clj index bd48c0e..b1eb2a5 100644 --- a/src/cli_cms/main.clj +++ b/src/cli_cms/main.clj @@ -1,10 +1,11 @@ (ns cli-cms.main (:require [cli-cms.create :as create] - [babashka.http-client :as http])) + [cli-cms.delete :as delete])) (defn -main [& args] (case (first args) "create" (create/run) + "delete" (delete/run) "edit" (println "TODO: implment edit snippet") - (println "Missing command. Try 'create' or 'edit'."))) + (println "Missing command. Try create, edit, or delete.")))