Add support for config file, implement CLI args

This commit is contained in:
Felisp 2024-08-13 02:57:32 +02:00
parent ec73dd0c1a
commit 2ca45803e5
3 changed files with 60 additions and 27 deletions

View file

@ -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" :description "RSS based thread watcher"
:url "http://example.com/FIXME" :url "http://example.com/FIXME"
:license {:name "AGPL-3.0-only" :license {:name "AGPL-3.0-only"

View file

@ -23,7 +23,7 @@
[rss-thread-watch.utils :as u]) [rss-thread-watch.utils :as u])
(:gen-class)) (:gen-class))
(def VERSION "0.3.7") (def VERSION "0.3.8")
;; Internal default config ;; Internal default config
(def CONFIG-DEFAULT (def CONFIG-DEFAULT
@ -85,38 +85,49 @@
(defn get-some-config (defn get-some-config
"Attempts to get config somehow, "Attempts to get config somehow,
first from command line argument first from [custom-file], if it's nil,
then from ./config.edn file then from ./config.edn file.
lastly uses default internal" If is neither exists, default internal one is used."
;; args do not include path to executable so first arg [custom-file]
;; should be config file
[cmd-args]
(config-fill-board-defaults (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")] "./config.edn")]
(u/when-else (load-config file-to-try) (u/when-else (load-config file-to-try)
(println "WARN: Using default internal config because suggessted file: '" file-to-try "' not found.")
CONFIG-DEFAULT)))) CONFIG-DEFAULT))))
;; Todo: Add option to write default config to stdout
(defn -main (defn -main
"Entry point, starts webserver" "Entry point, starts webserver"
[& args] [& args]
;; Todo: Think of a way to start repeated download for every catalog efficiently (let [parsed-args (parse-opts args cli-options)
(let [config (get-some-config args)] options (get parsed-args :options)]
;; Init the few globals we have (when-let [err (get parsed-args :errors)]
(reset! watcher/GLOBAL-CONFIG config) (println "Error: " err)
(reset! feed/boards-enabled-cache (set (keys (get config :boards-enabled)))) (System/exit 1))
(reset! watcher/chod-threads-cache (watcher/generate-chod-cache-structure config)) (when (get options :version)
(println args) (println "RSS Thread Watcher " VERSION " Licensed under AGPL-3.0-only")
(clojure.pprint/pprint config) (System/exit 0))
;; Needs to be redone and probably removed from here (when (get options :help)
;; (set-interval (fn [] (println "RSS Thread Watcher help:\n" (get parsed-args :summary))
;; (println "Starting cache update") (System/exit 0))
;; (watcher/update-board-cache! (:target config) (:starting-page config))) (when (get options :print-default-config)
;; (* 1000 (:refresh-delay config))) (println ";;Default internal config file from RSS Thread Watcher " VERSION)
(jetty/run-jetty (rp/wrap-params feed/http-handler) {:port (:port CONFIG-DEFAULT) (clojure.pprint/pprint CONFIG-DEFAULT)
:join? true}))) ;; 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 ;; Docs: https://github.com/ring-clojure/ring/wiki/Getting-Started
(defn repl-main (defn repl-main

View file

@ -77,3 +77,25 @@
(empty m) (empty m)
(for [[key val] m] (for [[key val] m]
[key (f key val)]))) [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)))))