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"
:url "http://example.com/FIXME"
:license {:name "AGPL-3.0-only"

View file

@ -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.")
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)]
(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))
(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})))
:join? true}))))
;; Docs: https://github.com/ring-clojure/ring/wiki/Getting-Started
(defn repl-main

View file

@ -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)))))