diff --git a/src/config.rs b/src/config.rs index f7fa32d..4851750 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ mod file_config; mod filters; use crate::BoxedError; -use clap::{arg, value_parser, Command}; +use clap::{arg, value_parser, ArgAction, Command}; use file_config::FileConfig; use std::{path::PathBuf, time::Duration}; @@ -17,6 +17,7 @@ pub struct Config { pub poll_time_window: Duration, pub idle_bucket_name: String, pub active_window_bucket_name: String, + pub mock_server: bool, filters: Vec, } @@ -42,6 +43,9 @@ impl Config { arg!(--"poll-time-window" "Period between sending heartbeats to the server for idle activity") .value_parser(value_parser!(u32)) .default_value(defaults::poll_time_window_seconds().to_string()), + arg!(--"mock-server" "Don't communicate to the ActivityWatch server") + .value_parser(value_parser!(bool)) + .action(ArgAction::SetTrue), ]) .get_matches(); @@ -60,6 +64,7 @@ impl Config { idle_bucket_name, active_window_bucket_name, filters: config.client.filters, + mock_server: *matches.get_one("mock-server").unwrap(), }) } diff --git a/src/main.rs b/src/main.rs index 74e31f5..bde1328 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,10 +106,17 @@ fn main() -> Result<(), BoxedError> { let client = ReportClient::new(Config::from_cli()?)?; let client = Arc::new(client); - info!( - "Sending to server {}:{}", - client.config.host, client.config.port - ); + if client.config.mock_server { + warn!( + "Not sending to server {}:{}", + client.config.host, client.config.port + ); + } else { + info!( + "Sending to server {}:{}", + client.config.host, client.config.port + ); + } info!( "Idle timeout: {} seconds", client.config.idle_timeout.as_secs() diff --git a/src/report_client.rs b/src/report_client.rs index 72dcfeb..0aaf369 100644 --- a/src/report_client.rs +++ b/src/report_client.rs @@ -14,8 +14,11 @@ pub struct ReportClient { impl ReportClient { pub fn new(config: Config) -> Result { let client = AwClient::new(&config.host, &config.port.to_string(), "awatcher"); - Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?; - Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?; + + if !config.mock_server { + Self::create_bucket(&client, &config.idle_bucket_name, "afkstatus")?; + Self::create_bucket(&client, &config.active_window_bucket_name, "currentwindow")?; + } Ok(Self { client, config }) } @@ -39,12 +42,14 @@ impl ReportClient { data, }; + if self.config.mock_server { + return Ok(()); + } + let pulsetime = (self.config.idle_timeout + self.config.poll_time_idle).as_secs_f64(); self.client .heartbeat(&self.config.idle_bucket_name, &event, pulsetime) - .map_err(|_| "Failed to send heartbeat")?; - - Ok(()) + .map_err(|_| "Failed to send heartbeat".into()) } pub fn send_active_window(&self, app_id: &str, title: &str) -> Result<(), BoxedError> { @@ -73,6 +78,10 @@ impl ReportClient { data, }; + if self.config.mock_server { + return Ok(()); + } + let interval_margin = self.config.poll_time_idle.as_secs_f64() + 1.0; self.client .heartbeat(