From 314c480c419745780191039cb162a40d7ec5b717 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Mon, 9 Mar 2026 09:24:28 +0100 Subject: [PATCH] restructure modules --- snippets_cms.bb | 4 +-- snippets_mcp.bb | 4 +-- src/cli/delete.clj | 18 ----------- src/cli/view.clj | 27 ---------------- src/snippets_cms/backend_connect.clj | 32 +++++++++++++++++++ src/{cli => snippets_cms}/config.clj | 2 +- src/{cli => snippets_cms}/mcp.clj | 26 +++------------ src/{cli/main.clj => snippets_cms/tui.clj} | 10 +++--- .../tui_actions}/create.clj | 13 ++------ src/snippets_cms/tui_actions/delete.clj | 8 +++++ .../tui_actions}/edit.clj | 26 ++++----------- src/snippets_cms/tui_actions/view.clj | 16 ++++++++++ 12 files changed, 80 insertions(+), 106 deletions(-) delete mode 100644 src/cli/delete.clj delete mode 100644 src/cli/view.clj create mode 100644 src/snippets_cms/backend_connect.clj rename src/{cli => snippets_cms}/config.clj (91%) rename src/{cli => snippets_cms}/mcp.clj (84%) rename src/{cli/main.clj => snippets_cms/tui.clj} (66%) rename src/{cli => snippets_cms/tui_actions}/create.clj (50%) create mode 100644 src/snippets_cms/tui_actions/delete.clj rename src/{cli => snippets_cms/tui_actions}/edit.clj (70%) create mode 100644 src/snippets_cms/tui_actions/view.clj diff --git a/snippets_cms.bb b/snippets_cms.bb index 77c7c3a..2197135 100755 --- a/snippets_cms.bb +++ b/snippets_cms.bb @@ -1,7 +1,7 @@ #!/usr/bin/env bb (ns snippets-cms - (:require [cli.main])) + (:require [snippets-cms.tui])) (when (= *file* (System/getProperty "babashka.file")) - (apply cli.main/-main *command-line-args*)) + (apply snippets-cms.tui/-main *command-line-args*)) diff --git a/snippets_mcp.bb b/snippets_mcp.bb index e5c0685..4759772 100755 --- a/snippets_mcp.bb +++ b/snippets_mcp.bb @@ -1,7 +1,7 @@ #!/usr/bin/env bb (ns snippets-mcp - (:require [cli.mcp])) + (:require [snippets-cms.mcp])) (when (= *file* (System/getProperty "babashka.file")) - (apply cli.mcp/-main *command-line-args*)) + (apply snippets-cms.mcp/-main *command-line-args*)) diff --git a/src/cli/delete.clj b/src/cli/delete.clj deleted file mode 100644 index 3b0359d..0000000 --- a/src/cli/delete.clj +++ /dev/null @@ -1,18 +0,0 @@ -(ns cli.delete - (:require - [com.travisshears.gum-utils :as utils] - [cli.config :refer [config]] - [clojure.pprint :refer [pprint]] - [babashka.http-client :as http] - [cheshire.core :as json])) - -(defn fetch-snippets [] - (let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))] - (json/parse-string (:body res) true))) - -(defn delete-snippet [id] - (http/delete (str (:backend-host config) "/api/snippet") {:query-params {"id" id}})) - -(defn run [] - (let [selected-snippet (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))] - (delete-snippet (:id selected-snippet)))) diff --git a/src/cli/view.clj b/src/cli/view.clj deleted file mode 100644 index 9e25274..0000000 --- a/src/cli/view.clj +++ /dev/null @@ -1,27 +0,0 @@ -(ns cli.view - (:require - [babashka.http-client :as http] - [clojure.pprint :refer [pprint]] - [cli.config :refer [config]] - [cheshire.core :as json] - [clojure.string :as str] - [com.travisshears.gum-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 view [snippet] - (utils/color-print (str "title: " (:title snippet)) "4") - (utils/color-print (str "slug: " (:slug snippet)) "4") - (utils/color-print (str "tags: " (str/join ", " (:tags snippet))) "4") - (utils/color-print "markdown: " "4") - (utils/print-markdown (:markdown snippet))) - -(defn run [] - (let [snippet-to-view (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))] - (view (fetch-snippet (:id snippet-to-view))))) diff --git a/src/snippets_cms/backend_connect.clj b/src/snippets_cms/backend_connect.clj new file mode 100644 index 0000000..584c7e5 --- /dev/null +++ b/src/snippets_cms/backend_connect.clj @@ -0,0 +1,32 @@ +(ns snippets-cms.backend-connect + (:require + [clojure.edn :as edn] + [babashka.http-client :as http] + [snippets-cms.config :refer [config]])) + +(defn- get-request [path params] + (let [res (http/get (str (:backend-host config) path) + {:headers {"Accept" "application/edn"} + :query-params params})] + (edn/read-string (:body res)))) + +(defn fetch-snippets [& [limit]] + (let [params (if limit {:limit limit} {})] + (get-request "/api/snippets" params))) + +(defn fetch-snippet [id] + (get-request "/api/snippet" {:id id})) + +(defn delete-snippet [id] + (http/delete (str (:backend-host config) "/api/snippet") {:query-params {"id" id}})) + +(defn update-snippet [id patch] + (http/patch (str (:backend-host config) "/api/snippet") + {:query-params {:id id} + :headers {:content-type "application/edn"} + :body (pr-str patch)})) + +(defn create-snippet [{:keys [title slug markdown tags]}] + (http/post (str (:backend-host config) "/api/snippet") + {:headers {:content-type "application/edn"} + :body (pr-str {:title title :slug slug :markdown markdown :tags tags})})) diff --git a/src/cli/config.clj b/src/snippets_cms/config.clj similarity index 91% rename from src/cli/config.clj rename to src/snippets_cms/config.clj index 39602a7..624a2a1 100644 --- a/src/cli/config.clj +++ b/src/snippets_cms/config.clj @@ -1,4 +1,4 @@ -(ns cli.config +(ns snippets-cms.config (:require [babashka.fs :as fs] [clojure.string :as str])) diff --git a/src/cli/mcp.clj b/src/snippets_cms/mcp.clj similarity index 84% rename from src/cli/mcp.clj rename to src/snippets_cms/mcp.clj index 6de2fce..2e77928 100644 --- a/src/cli/mcp.clj +++ b/src/snippets_cms/mcp.clj @@ -1,9 +1,9 @@ -(ns cli.mcp +(ns snippets-cms.mcp (:require - [babashka.http-client :as http] - [cli.config :refer [config]] - [clojure.edn :as edn] - [cheshire.core :as json])) + [cheshire.core :as json] + [snippets-cms.backend-connect :refer [create-snippet + fetch-snippet + fetch-snippets]])) ;; Tool definitions (def available-tools @@ -32,22 +32,6 @@ :description "Array of tags for categorizing the snippet"}} :required ["title" "slug" "markdown" "tags"]}}]) -;; interact with backend api -(defn fetch-snippets [] - (let [res (http/get (str (:backend-host config) "/api/snippets"))] - (json/parse-string (:body res) true))) - -(defn fetch-snippet [id] - (let [res (http/get (str (:backend-host config) "/api/snippet") - {:headers {"Accept" "application/edn"} - :query-params {:id id}})] - (edn/read-string (:body res)))) - -(defn create-snippet [{:keys [title slug markdown tags]}] - (http/post (str (:backend-host config) "/api/snippet") - {:headers {:content-type "application/edn"} - :body (pr-str {:title title :slug slug :markdown markdown :tags tags})})) - ;; Tool implementations (defn mcp-res [fn args] (try diff --git a/src/cli/main.clj b/src/snippets_cms/tui.clj similarity index 66% rename from src/cli/main.clj rename to src/snippets_cms/tui.clj index 28f2b08..ede6c65 100644 --- a/src/cli/main.clj +++ b/src/snippets_cms/tui.clj @@ -1,10 +1,10 @@ -(ns cli.main +(ns snippets-cms.tui (:require - [cli.create :as create] - [cli.edit :as edit] - [cli.delete :as delete] + [snippets-cms.tui-actions.create :as create] + [snippets-cms.tui-actions.edit :as edit] + [snippets-cms.tui-actions.delete :as delete] [com.travisshears.gum-utils :as utils] - [cli.view :as view])) + [snippets-cms.tui-actions.view :as view])) (defn color-test [] (doseq [color (range 0 255)] diff --git a/src/cli/create.clj b/src/snippets_cms/tui_actions/create.clj similarity index 50% rename from src/cli/create.clj rename to src/snippets_cms/tui_actions/create.clj index 102818a..441f251 100644 --- a/src/cli/create.clj +++ b/src/snippets_cms/tui_actions/create.clj @@ -1,16 +1,7 @@ -(ns cli.create +(ns snippets-cms.tui-actions.create (:require [com.travisshears.gum-utils :as utils] - [clojure.string :as str] - [cli.config :refer [config]] - [babashka.http-client :as http] - [cheshire.core :as json] - [babashka.process :refer [shell]])) - -(defn create-snippet [{: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})})) + [snippets-cms.backend-connect :refer [create-snippet]])) (defn run [] (let [title (utils/prompt-for "title") diff --git a/src/snippets_cms/tui_actions/delete.clj b/src/snippets_cms/tui_actions/delete.clj new file mode 100644 index 0000000..8925a4d --- /dev/null +++ b/src/snippets_cms/tui_actions/delete.clj @@ -0,0 +1,8 @@ +(ns snippets-cms.tui-actions.delete + (:require + [com.travisshears.gum-utils :as utils] + [snippets-cms.backend-connect :refer [delete-snippet fetch-snippets]])) + +(defn run [] + (let [selected-snippet (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))] + (delete-snippet (:id selected-snippet)))) diff --git a/src/cli/edit.clj b/src/snippets_cms/tui_actions/edit.clj similarity index 70% rename from src/cli/edit.clj rename to src/snippets_cms/tui_actions/edit.clj index 6c16f20..760b905 100644 --- a/src/cli/edit.clj +++ b/src/snippets_cms/tui_actions/edit.clj @@ -1,15 +1,9 @@ -(ns cli.edit +(ns snippets-cms.tui-actions.edit (:require - [babashka.http-client :as http] - [clojure.pprint :refer [pprint]] - [cli.config :refer [config]] - [cli.view :as view] - [cheshire.core :as json] - [com.travisshears.gum-utils :as utils])) - -(defn fetch-snippets [] - (let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))] - (json/parse-string (:body res) true))) + [com.travisshears.gum-utils :as utils] + [snippets-cms.backend-connect :refer [fetch-snippet fetch-snippets + update-snippet]] + [snippets-cms.tui-actions.view :as view])) (defn prompt-for-edit-type [] (utils/select (map #(hash-map :value (name %) :item %) '(:title @@ -19,12 +13,6 @@ :add-tags :done)))) -(defn send-patch [id patch] - (http/patch (str (:backend-host config) "/api/snippet") - {:query-params {:id id} - :headers {:content-type "application/json"} - :body (json/encode patch)})) - (defn edit ([snippet] (edit snippet {})) @@ -46,6 +34,6 @@ (defn run [] (let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))] - (send-patch (:id snippet-to-edit) (edit snippet-to-edit)) + (update-snippet (:id snippet-to-edit) (edit snippet-to-edit)) (println "Snippet updated successfully") - (view/view (view/fetch-snippet (:id snippet-to-edit))))) + (view/view (fetch-snippet (:id snippet-to-edit))))) diff --git a/src/snippets_cms/tui_actions/view.clj b/src/snippets_cms/tui_actions/view.clj new file mode 100644 index 0000000..87bccf4 --- /dev/null +++ b/src/snippets_cms/tui_actions/view.clj @@ -0,0 +1,16 @@ +(ns snippets-cms.tui-actions.view + (:require + [clojure.string :as str] + [com.travisshears.gum-utils :as utils] + [snippets-cms.backend-connect :refer [fetch-snippet fetch-snippets]])) + +(defn view [snippet] + (utils/color-print (str "title: " (:title snippet)) "4") + (utils/color-print (str "slug: " (:slug snippet)) "4") + (utils/color-print (str "tags: " (str/join ", " (:tags snippet))) "4") + (utils/color-print "markdown: " "4") + (utils/print-markdown (:markdown snippet))) + +(defn run [] + (let [snippet-to-view (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets 25)))] + (view (fetch-snippet (:id snippet-to-view)))))