From 2ca45803e53efd259ad281a4c6b1f295d65b71fb Mon Sep 17 00:00:00 2001 From: Felisp Date: Tue, 13 Aug 2024 02:57:32 +0200 Subject: [PATCH] Add support for config file, implement CLI args --- project.clj | 2 +- src/rss_thread_watch/core.clj | 63 ++++++++++++++++++++-------------- src/rss_thread_watch/utils.clj | 22 ++++++++++++ 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/project.clj b/project.clj index 3345fd6..c9a1d3b 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject rss-thread-watch "0.3.7-SNAPSHOT" +(defproject rss-thread-watch "0.3.8-SNAPSHOT" :description "RSS based thread watcher" :url "http://example.com/FIXME" :license {:name "AGPL-3.0-only" diff --git a/src/rss_thread_watch/core.clj b/src/rss_thread_watch/core.clj index 6802b1f..6a08fe5 100644 --- a/src/rss_thread_watch/core.clj +++ b/src/rss_thread_watch/core.clj @@ -23,7 +23,7 @@ [rss-thread-watch.utils :as u]) (:gen-class)) -(def VERSION "0.3.7") +(def VERSION "0.3.8") ;; Internal default config (def CONFIG-DEFAULT @@ -85,38 +85,49 @@ (defn get-some-config "Attempts to get config somehow, - first from command line argument - then from ./config.edn file - lastly uses default internal" - ;; args do not include path to executable so first arg - ;; should be config file - [cmd-args] + first from [custom-file], if it's nil, + then from ./config.edn file. + If is neither exists, default internal one is used." + [custom-file] (config-fill-board-defaults - (let [file-to-try (u/nil?-else (first cmd-args) + ;; TODO: There has to be try/catch for when file is invalid edn + ;; This is gonna be done when config validation comes in Beta 2 + (let [file-to-try (u/nil?-else custom-file "./config.edn")] - (u/when-else (load-config file-to-try) - (println "WARN: Using default internal config because suggessted file: '" file-to-try "' not found.") + (u/when-else (load-config file-to-try) + CONFIG-DEFAULT)))) -;; Todo: Add option to write default config to stdout (defn -main "Entry point, starts webserver" [& args] - ;; Todo: Think of a way to start repeated download for every catalog efficiently - (let [config (get-some-config args)] - ;; Init the few globals we have - (reset! watcher/GLOBAL-CONFIG config) - (reset! feed/boards-enabled-cache (set (keys (get config :boards-enabled)))) - (reset! watcher/chod-threads-cache (watcher/generate-chod-cache-structure config)) - (println args) - (clojure.pprint/pprint config) - ;; Needs to be redone and probably removed from here - ;; (set-interval (fn [] - ;; (println "Starting cache update") - ;; (watcher/update-board-cache! (:target config) (:starting-page config))) - ;; (* 1000 (:refresh-delay config))) - (jetty/run-jetty (rp/wrap-params feed/http-handler) {:port (:port CONFIG-DEFAULT) - :join? true}))) + (let [parsed-args (parse-opts args cli-options) + options (get parsed-args :options)] + (when-let [err (get parsed-args :errors)] + (println "Error: " err) + (System/exit 1)) + (when (get options :version) + (println "RSS Thread Watcher " VERSION " Licensed under AGPL-3.0-only") + (System/exit 0)) + (when (get options :help) + (println "RSS Thread Watcher help:\n" (get parsed-args :summary)) + (System/exit 0)) + (when (get options :print-default-config) + (println ";;Default internal config file from RSS Thread Watcher " VERSION) + (clojure.pprint/pprint CONFIG-DEFAULT) + ;; In case someone was copying by hand, this might be useful + (println ";;END of Default internal config file") + (System/exit 0)) + + (let [config (get-some-config (:config options))] + ;; TODO: probably refactor to use separate config.clj file when validation will be added + ;; Init the few globals we have + (reset! watcher/GLOBAL-CONFIG config) + (reset! feed/boards-enabled-cache (set (keys (get config :boards-enabled)))) + (reset! watcher/chod-threads-cache (watcher/generate-chod-cache-structure config)) + (clojure.pprint/pprint config) + (jetty/run-jetty (rp/wrap-params feed/http-handler) {:port (:port CONFIG-DEFAULT) + :join? true})))) ;; Docs: https://github.com/ring-clojure/ring/wiki/Getting-Started (defn repl-main diff --git a/src/rss_thread_watch/utils.clj b/src/rss_thread_watch/utils.clj index daed328..db53c12 100644 --- a/src/rss_thread_watch/utils.clj +++ b/src/rss_thread_watch/utils.clj @@ -77,3 +77,25 @@ (empty m) (for [[key val] m] [key (f key val)]))) + +(defn expand-home + "Expands ~ to home directory" + ;;modified from sauce: https://stackoverflow.com/questions/29585928/how-to-substitute-path-to-home-for + [s] + (if (clojure.string/starts-with? s "~") + (clojure.string/replace-first s "~" (System/getProperty "user.home")) + s)) + +(defn expand-path + [s] + (if (clojure.string/starts-with? s "./") + (clojure.string/replace-first s "." (System/getProperty "user.dir")) + (expand-home s))) + +(defn file-exists? + "Returns true if file exists" + [file] + (let [path (if (vector? file) + (first file) + file)] + (.exists (clojure.java.io/file (expand-path path)))))