Add CLI arguments in addition to env vars.
This commit is contained in:
parent
502029dc80
commit
dbdcaf4cf0
2
deps.edn
2
deps.edn
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
org.clojure/math.numeric-tower { :mvn/version "0.0.5" }
|
org.clojure/math.numeric-tower { :mvn/version "0.0.5" }
|
||||||
|
|
||||||
|
org.clojure/tools.cli { :mvn/version "1.0.206" }
|
||||||
|
|
||||||
camel-snake-kebab/camel-snake-kebab { :mvn/version "0.4.2" }
|
camel-snake-kebab/camel-snake-kebab { :mvn/version "0.4.2" }
|
||||||
}
|
}
|
||||||
:aliases {
|
:aliases {
|
||||||
|
|
|
@ -62,16 +62,13 @@ in {
|
||||||
environment = {
|
environment = {
|
||||||
PRICEBOT_EXCHANGE_HOST = cfg.exchange-host;
|
PRICEBOT_EXCHANGE_HOST = cfg.exchange-host;
|
||||||
PRICEBOT_BEBOT_URL = cfg.mattermost-url;
|
PRICEBOT_BEBOT_URL = cfg.mattermost-url;
|
||||||
PRICEBOT_BEBOT_AUTH_TOKEN_FILE = "%d/auth.token";
|
|
||||||
PRICEBOT_BEBOT_CHANNEL_ID = opts.mattermost-channel-id;
|
PRICEBOT_BEBOT_CHANNEL_ID = opts.mattermost-channel-id;
|
||||||
PRICEBOT_TARGET_CURRENCY = opts.currency;
|
PRICEBOT_TARGET_CURRENCY = opts.currency;
|
||||||
PRICEBOT_NOTIFY_USER = opts.notify-user;
|
PRICEBOT_NOTIFY_USER = opts.notify-user;
|
||||||
};
|
};
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStart = pkgs.writeShellScript "launch-pricebot-${currency}.sh" ''
|
ExecStart = pkgs.writeShellScript "launch-pricebot-${currency}.sh" ''
|
||||||
echo "CREDENTIALS_DIRECTORY $CREDENTIALS_DIRECTORY"
|
${pricebot}/bin/pricebot --bebot-auth-token-file=$CREDENTIALS_DIRECTORY/auth.token
|
||||||
echo "CREDENTIALS_PATH $CREDENTIALS_PATH"
|
|
||||||
${pricebot}/bin/pricebot
|
|
||||||
'';
|
'';
|
||||||
DynamicUser = true;
|
DynamicUser = true;
|
||||||
PrivateTmp = true;
|
PrivateTmp = true;
|
||||||
|
|
|
@ -9,7 +9,9 @@
|
||||||
[fudo-clojure.result :as result :refer [map-success send-success dispatch-result success? unwrap]]
|
[fudo-clojure.result :as result :refer [map-success send-success dispatch-result success? unwrap]]
|
||||||
[fudo-clojure.logging :as log]
|
[fudo-clojure.logging :as log]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[clojure.math.numeric-tower :refer [floor]])
|
[clojure.math.numeric-tower :refer [floor]]
|
||||||
|
[clojure.tools.cli :refer [parse-opts]]
|
||||||
|
[camel-snake-kebab.core :refer [->SCREAMING_SNAKE_CASE]])
|
||||||
(:import java.math.RoundingMode
|
(:import java.math.RoundingMode
|
||||||
java.time.format.DateTimeFormatter
|
java.time.format.DateTimeFormatter
|
||||||
java.time.Duration
|
java.time.Duration
|
||||||
|
@ -158,24 +160,50 @@
|
||||||
(let [checks (create-checks logger)]
|
(let [checks (create-checks logger)]
|
||||||
(reify-bot hostname hostname currency checks)))
|
(reify-bot hostname hostname currency checks)))
|
||||||
|
|
||||||
(defn- getenv-or-fail [var]
|
(def cli-opts
|
||||||
(if-let [val (System/getenv var)]
|
[["-x" "--exchange-host HOST" "Hostname of the Coinbase Pro target host."]
|
||||||
val
|
["-b" "--bebot-url URL" "URL of the target Mattermost server."]
|
||||||
(throw (ex-info (str "Failed to read environment variable: " var) {}))))
|
["-a" "--bebot-auth-token-file FILE" "Path to file containing the Bebot authentication token."]
|
||||||
|
["-C" "--bebot-channel-id ID" "ID of channel in which to send price notifications."]
|
||||||
|
["-c" "--currency CURRENCY" "Cryptocurrency to watch for price fluctuations."]
|
||||||
|
["-u" "--notify-user USER" "User to notify of significant price fluctuations."]
|
||||||
|
["-h" "--help"]])
|
||||||
|
|
||||||
|
(defn get-key [opts k]
|
||||||
|
(if-let [opt (get opts k)]
|
||||||
|
[k opt]
|
||||||
|
[k (System/getenv (System/getenv (format "PRICEBOT_%s"
|
||||||
|
(-> k name ->SCREAMING_SNAKE_CASE))))]))
|
||||||
|
|
||||||
|
(defn exit! [status msg]
|
||||||
|
(println msg)
|
||||||
|
(System/exit status))
|
||||||
|
|
||||||
|
(defn get-args [keys args]
|
||||||
|
(let [input-opts (parse-opts args cli-opts)
|
||||||
|
opts (into {} (map (partial get-key input-opts) keys))
|
||||||
|
missing (filter (fn [k] (not (get opts k))) keys)]
|
||||||
|
(cond (:help opts) (exit! 0 (println (:summary opts)))
|
||||||
|
(:errors opts) (exit! 1 (println (str/join \newline (:errors opts))))
|
||||||
|
(not (empty? missing)) (exit! 2 (println (str "missing arguments: " (str/join " " (map name missing)))))
|
||||||
|
:else opts)))
|
||||||
|
|
||||||
(defn -main [& args]
|
(defn -main [& args]
|
||||||
(let [exchange-hostname (getenv-or-fail "PRICEBOT_EXCHANGE_HOST")
|
(let [required-keys [:exchange-host
|
||||||
bebot-host (getenv-or-fail "PRICEBOT_BEBOT_URL")
|
:currency
|
||||||
bebot-auth-token (-> (getenv-or-fail "PRICEBOT_BEBOT_AUTH_TOKEN_FILE")
|
:bebot-url
|
||||||
(slurp))
|
:bebot-auth-token-file
|
||||||
bebot-channel-id (getenv-or-fail "PRICEBOT_BEBOT_CHANNEL_ID")
|
:bebot-channel-id
|
||||||
target-currency (-> (getenv-or-fail "PRICEBOT_TARGET_CURRENCY")
|
:notify-user]
|
||||||
(str/lower-case)
|
{:keys [exchange-host
|
||||||
(keyword))
|
currency
|
||||||
notify-user (getenv-or-fail "PRICEBOT_NOTIFY_USER")]
|
bebot-url
|
||||||
(let [logger (bebot-log/make-logger bebot-host bebot-auth-token bebot-channel-id notify-user)
|
bebot-auth-token-file
|
||||||
|
bebot-channel-id
|
||||||
|
notify-user]} (get-args required-keys args)]
|
||||||
|
(let [logger (bebot-log/make-logger bebot-url (slurp bebot-auth-token-file) bebot-channel-id notify-user)
|
||||||
checks (create-checks logger)
|
checks (create-checks logger)
|
||||||
bot (reify-bot exchange-hostname target-currency)
|
bot (reify-bot exchange-host (-> currency str/lower-case keyword))
|
||||||
shutdown (chan)]
|
shutdown (chan)]
|
||||||
(.addShutdownHook (Runtime/getRuntime)
|
(.addShutdownHook (Runtime/getRuntime)
|
||||||
(Thread. (fn []
|
(Thread. (fn []
|
||||||
|
|
Loading…
Reference in New Issue