mirror of
https://github.com/2e3s/awatcher.git
synced 2025-06-06 11:35:46 +00:00
Add lib section
This commit is contained in:
parent
caad19e60b
commit
1740ec00c6
10
Cargo.toml
10
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"
|
||||
|
@ -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}");
|
||||
|
@ -57,23 +57,16 @@ pub struct 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 =
|
||||
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::<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 = 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<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);
|
||||
|
||||
Ok(config)
|
||||
|
6
src/lib.rs
Normal file
6
src/lib.rs
Normal file
@ -0,0 +1,6 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub mod config;
|
||||
pub mod report_client;
|
||||
pub mod watchers;
|
@ -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};
|
||||
|
Loading…
x
Reference in New Issue
Block a user