From 1740ec00c6f36a765e5b23ec5a6824bb35a8ec65 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Sat, 29 Apr 2023 23:01:54 -0400 Subject: [PATCH] Add lib section --- Cargo.toml | 10 ++++++++++ src/config.rs | 2 +- src/config/file_config.rs | 33 ++++++++++++++++++++++----------- src/lib.rs | 6 ++++++ src/report_client.rs | 2 +- 5 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 27b626f..7b74fde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,18 @@ [package] name = "awatcher" version = "0.1.0" +authors = ["Demmie <2e3s19@gmail.com>"] edition = "2021" +[[bin]] +name = "awatcher" +path = "src/main.rs" + +[lib] +name = "awatcher" +crate-type = ["lib"] +path = "src/lib.rs" + [dependencies] aw-client-rust = { git = "https://github.com/ActivityWatch/aw-server-rust" } gethostname = "0.4.1" diff --git a/src/config.rs b/src/config.rs index 00d7e7d..4a76db6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -53,7 +53,7 @@ impl Config { ]) .get_matches(); - let config = FileConfig::new(&matches)?; + let config = FileConfig::new_with_cli(&matches)?; let hostname = gethostname::gethostname().into_string().unwrap(); let idle_bucket_name = format!("aw-watcher-afk_{hostname}"); diff --git a/src/config/file_config.rs b/src/config/file_config.rs index 4b8949a..2757818 100644 --- a/src/config/file_config.rs +++ b/src/config/file_config.rs @@ -57,23 +57,16 @@ pub struct FileConfig { } impl FileConfig { - pub fn new(matches: &ArgMatches) -> anyhow::Result { + pub fn new(config_override: Option) -> anyhow::Result { let mut config_path: PathBuf = dirs::config_dir().ok_or(anyhow!("Config directory is unknown"))?; config_path.push("awatcher"); config_path.push("config.toml"); - if matches.contains_id("config") { - let config_file = matches.get_one::("config"); - if let Some(path) = config_file { - if let Err(e) = std::fs::metadata(path) { - warn!("Invalid config filename, using the default config: {e}"); - } else { - config_path = Path::new(path).to_path_buf(); - } - } + if let Some(config_override) = config_override { + config_path = config_override; } - let mut config = if config_path.exists() { + let config = if config_path.exists() { debug!("Reading config at {}", config_path.display()); let config_content = std::fs::read_to_string(&config_path).with_context(|| { format!("Impossible to read config file {}", config_path.display()) @@ -122,6 +115,24 @@ impl FileConfig { Self::default() }; + + Ok(config) + } + + pub fn new_with_cli(matches: &ArgMatches) -> anyhow::Result { + let mut config_path = None; + if matches.contains_id("config") { + let config_file = matches.get_one::("config"); + if let Some(path) = config_file { + if let Err(e) = std::fs::metadata(path) { + warn!("Invalid config filename, using the default config: {e}"); + } else { + config_path = Some(Path::new(path).to_path_buf()); + } + } + } + let mut config = Self::new(config_path)?; + config.merge_cli(matches); Ok(config) diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..360d577 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +#[macro_use] +extern crate log; + +pub mod config; +pub mod report_client; +pub mod watchers; diff --git a/src/report_client.rs b/src/report_client.rs index c560cd7..23276ea 100644 --- a/src/report_client.rs +++ b/src/report_client.rs @@ -1,4 +1,4 @@ -use super::Config; +use super::config::Config; use anyhow::Context; use aw_client_rust::{AwClient, Event as AwEvent}; use chrono::{DateTime, Duration, Utc};