From f40922030ca24c5d9dee59b4ee873bea6e0b5a0d Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Wed, 31 Jan 2024 16:55:09 +0100 Subject: [PATCH] Use clap for cli --- Cargo.lock | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 30 +++++++++----- 3 files changed, 133 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02a8aab..4fbcd34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,54 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2faccea4cc4ab4a667ce676a30e8ec13922a692c99bb8f5b11f1502c72e04220" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "async-stream" version = "0.3.5" @@ -248,6 +296,52 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "convert_case" version = "0.4.0" @@ -754,6 +848,7 @@ name = "healthpot" version = "0.1.0" dependencies = [ "chrono", + "clap", "dotenv", "duplicate", "futures", @@ -771,6 +866,12 @@ dependencies = [ "tokio", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.4" @@ -1949,6 +2050,12 @@ dependencies = [ "loom", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "subtle" version = "2.5.0" @@ -2353,6 +2460,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index f8f1739..13b5acc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ rocket = { version = "0.5", features = ["secrets"] } rocket_dyn_templates = { version = "0.1.0", features = ["handlebars"] } tokio = { version = "1", features = ["fs"] } reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] } +clap = { version = "4.4.18", features = ["derive", "env"] } diff --git a/src/main.rs b/src/main.rs index 90e5154..0490556 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,30 @@ mod health; mod routes; +use clap::Parser; use dotenv::dotenv; use health::HealthState; use rocket::fs::FileServer; use rocket_dyn_templates::Template; +use std::io; +use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; -use std::{env, io}; use tokio::{fs, task, time::sleep}; -fn start_poller(state: Arc) { - task::spawn(async move { - loop { - state.update().await; - sleep(Duration::from_secs(state.config.update_period)).await; - } - }); +#[derive(Parser)] +struct Opt { + /// Path to the config file. + #[clap(short, long, env = "CONFIG_PATH", default_value = "config.ron")] + config: PathBuf, } #[rocket::main] async fn main() -> io::Result<()> { dotenv().ok(); + let opt = Opt::parse(); - let config_path = env::var("CONFIG_PATH").unwrap_or("config.ron".into()); - - let config = fs::read_to_string(&config_path) + let config = fs::read_to_string(&opt.config) .await .expect("failed to read config file"); let config = ron::from_str(&config).expect("failed to parse config file"); @@ -43,3 +42,12 @@ async fn main() -> io::Result<()> { Ok(()) } + +fn start_poller(state: Arc) { + task::spawn(async move { + loop { + state.update().await; + sleep(Duration::from_secs(state.config.update_period)).await; + } + }); +}