Add lib section

This commit is contained in:
Demmie 2023-04-29 23:01:54 -04:00
parent caad19e60b
commit 1740ec00c6
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
5 changed files with 40 additions and 13 deletions

View File

@ -1,8 +1,18 @@
[package] [package]
name = "awatcher" name = "awatcher"
version = "0.1.0" version = "0.1.0"
authors = ["Demmie <2e3s19@gmail.com>"]
edition = "2021" edition = "2021"
[[bin]]
name = "awatcher"
path = "src/main.rs"
[lib]
name = "awatcher"
crate-type = ["lib"]
path = "src/lib.rs"
[dependencies] [dependencies]
aw-client-rust = { git = "https://github.com/ActivityWatch/aw-server-rust" } aw-client-rust = { git = "https://github.com/ActivityWatch/aw-server-rust" }
gethostname = "0.4.1" gethostname = "0.4.1"

View File

@ -53,7 +53,7 @@ impl Config {
]) ])
.get_matches(); .get_matches();
let config = FileConfig::new(&matches)?; let config = FileConfig::new_with_cli(&matches)?;
let hostname = gethostname::gethostname().into_string().unwrap(); let hostname = gethostname::gethostname().into_string().unwrap();
let idle_bucket_name = format!("aw-watcher-afk_{hostname}"); let idle_bucket_name = format!("aw-watcher-afk_{hostname}");

View File

@ -57,23 +57,16 @@ pub struct FileConfig {
} }
impl FileConfig { impl FileConfig {
pub fn new(matches: &ArgMatches) -> anyhow::Result<Self> { pub fn new(config_override: Option<PathBuf>) -> anyhow::Result<Self> {
let mut config_path: PathBuf = let mut config_path: PathBuf =
dirs::config_dir().ok_or(anyhow!("Config directory is unknown"))?; dirs::config_dir().ok_or(anyhow!("Config directory is unknown"))?;
config_path.push("awatcher"); config_path.push("awatcher");
config_path.push("config.toml"); config_path.push("config.toml");
if matches.contains_id("config") { if let Some(config_override) = config_override {
let config_file = matches.get_one::<String>("config"); config_path = config_override;
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();
}
}
} }
let mut config = if config_path.exists() { let config = if config_path.exists() {
debug!("Reading config at {}", config_path.display()); debug!("Reading config at {}", config_path.display());
let config_content = std::fs::read_to_string(&config_path).with_context(|| { let config_content = std::fs::read_to_string(&config_path).with_context(|| {
format!("Impossible to read config file {}", config_path.display()) format!("Impossible to read config file {}", config_path.display())
@ -122,6 +115,24 @@ impl FileConfig {
Self::default() Self::default()
}; };
Ok(config)
}
pub fn new_with_cli(matches: &ArgMatches) -> anyhow::Result<Self> {
let mut config_path = None;
if matches.contains_id("config") {
let config_file = matches.get_one::<String>("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); config.merge_cli(matches);
Ok(config) Ok(config)

6
src/lib.rs Normal file
View File

@ -0,0 +1,6 @@
#[macro_use]
extern crate log;
pub mod config;
pub mod report_client;
pub mod watchers;

View File

@ -1,4 +1,4 @@
use super::Config; use super::config::Config;
use anyhow::Context; use anyhow::Context;
use aw_client_rust::{AwClient, Event as AwEvent}; use aw_client_rust::{AwClient, Event as AwEvent};
use chrono::{DateTime, Duration, Utc}; use chrono::{DateTime, Duration, Utc};