Skip to content

Commit

Permalink
fix: moved --sync-dir arg into sync-advanced subcommand, fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 29, 2023
1 parent 81f84d9 commit 1bd5602
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 40 deletions.
2 changes: 2 additions & 0 deletions aw-sync/src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::fs;
use std::path::PathBuf;

// TODO: This could be refactored to share logic with aw-server/src/dirs.rs
// TODO: add proper config support
#[allow(dead_code)]
pub fn get_config_dir() -> Result<PathBuf, ()> {
let mut dir = appdirs::user_config_dir(Some("activitywatch"), None, false)?;
dir.push("aw-sync");
Expand Down
7 changes: 7 additions & 0 deletions aw-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ pub use sync::sync_datastores;
pub use sync::sync_run;
pub use sync::SyncSpec;

mod sync_wrapper;
pub use sync_wrapper::push;
pub use sync_wrapper::{pull, pull_all};

mod accessmethod;
pub use accessmethod::AccessMethod;

mod dirs;
mod util;
54 changes: 29 additions & 25 deletions aw-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ struct Opts {
/// Enable debug logging.
#[clap(long)]
verbose: bool,

/// Full path to sync directory.
/// If not specified, exit.
#[clap(long)]
sync_dir: String,

/// Full path to sync db file
/// Useful for syncing buckets from a specific db file in the sync directory.
/// Must be a valid absolute path to a file in the sync directory.
#[clap(long)]
sync_db: Option<String>,
}

#[derive(Subcommand)]
Expand All @@ -88,14 +77,27 @@ enum Commands {
/// Format: YYYY-MM-DD
#[clap(long)]
start_date: Option<String>,

/// Specify buckets to sync using a comma-separated list.
/// If not specified, all buckets will be synced.
#[clap(long)]
buckets: Option<String>,

/// Mode to sync in. Can be "push", "pull", or "both".
/// Defaults to "both".
#[clap(long, default_value = "both")]
mode: String,

/// Full path to sync directory.
/// If not specified, exit.
#[clap(long)]
sync_dir: String,

/// Full path to sync db file
/// Useful for syncing buckets from a specific db file in the sync directory.
/// Must be a valid absolute path to a file in the sync directory.
#[clap(long)]
sync_db: Option<String>,
},
/// List buckets and their sync status.
List {},
Expand All @@ -109,18 +111,6 @@ fn main() -> Result<(), Box<dyn Error>> {

aw_server::logging::setup_logger(true, verbose).expect("Failed to setup logging");

let sync_directory = if opts.sync_dir.is_empty() {
println!("No sync directory specified, exiting...");
std::process::exit(1);
} else {
Path::new(&opts.sync_dir)
};
info!("Using sync dir: {}", sync_directory.display());

if let Some(sync_db) = &opts.sync_db {
info!("Using sync db: {}", sync_db);
}

let port = if opts.testing && opts.port == DEFAULT_PORT {
"5666"
} else {
Expand Down Expand Up @@ -159,7 +149,21 @@ fn main() -> Result<(), Box<dyn Error>> {
start_date,
buckets,
mode,
sync_dir,
sync_db,
} => {
let sync_directory = if sync_dir.is_empty() {
println!("No sync directory specified, exiting...");
std::process::exit(1);
} else {
Path::new(&sync_dir)
};
info!("Using sync dir: {}", sync_directory.display());

if let Some(sync_db) = &sync_db {
info!("Using sync db: {}", sync_db);
}

let start: Option<DateTime<Utc>> = start_date.as_ref().map(|date| {
println!("{}", date.clone());
chrono::NaiveDate::parse_from_str(&date.clone(), "%Y-%m-%d")
Expand All @@ -176,7 +180,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.as_ref()
.map(|b| b.split(',').map(|s| s.to_string()).collect());

let sync_db: Option<PathBuf> = opts.sync_db.as_ref().map(|db| {
let sync_db: Option<PathBuf> = sync_db.as_ref().map(|db| {
let db_path = Path::new(db);
if !db_path.is_absolute() {
panic!("Sync db path must be absolute");
Expand Down Expand Up @@ -205,7 +209,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}

// List all buckets
Commands::List {} => sync::list_buckets(&client, sync_directory),
Commands::List {} => sync::list_buckets(&client),
}?;

// Needed to give the datastores some time to commit before program is shut down.
Expand Down
4 changes: 3 additions & 1 deletion aw-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Resul
}

#[allow(dead_code)]
pub fn list_buckets(client: &AwClient, sync_directory: &Path) -> Result<(), String> {
pub fn list_buckets(client: &AwClient) -> Result<(), String> {
let sync_directory = crate::dirs::get_sync_dir().map_err(|_| "Could not get sync dir")?;
let sync_directory = sync_directory.as_path();
let info = client.get_info().map_err(|e| e.to_string())?;

// FIXME: Incorrect device_id assumption?
Expand Down
9 changes: 4 additions & 5 deletions aw-sync/src/sync_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use std::fs;
use std::net::TcpStream;

use crate::sync::{sync_run, SyncMode, SyncSpec};
use crate::util::{get_hostname, get_remotes, get_server_port};
use aw_client_rust::blocking::AwClient;

pub fn pull_all(testing: bool) -> Result<(), Box<dyn Error>> {
let hostnames = get_remotes()?;
let hostnames = crate::util::get_remotes()?;
for host in hostnames {
pull(&host, testing)?
}
Expand All @@ -19,7 +18,7 @@ pub fn pull(host: &str, testing: bool) -> Result<(), Box<dyn Error>> {
info!("Pulling data from sync server {}", host);

// Port of the main server
let port = get_server_port(testing)?;
let port = crate::util::get_server_port(testing)?;

// Check if server is running
if TcpStream::connect(("localhost", port)).is_err() {
Expand Down Expand Up @@ -78,8 +77,8 @@ pub fn pull(host: &str, testing: bool) -> Result<(), Box<dyn Error>> {
}

pub fn push(testing: bool) -> Result<(), Box<dyn Error>> {
let hostname = get_hostname()?;
let port = get_server_port(testing)?.to_string();
let hostname = crate::util::get_hostname()?;
let port = crate::util::get_server_port(testing)?.to_string();

let sync_dir = crate::dirs::get_sync_dir()
.map_err(|_| "Could not get sync dir")?
Expand Down
9 changes: 4 additions & 5 deletions aw-sync/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::dirs::get_server_config_path;
use std::boxed::Box;
use std::error::Error;
use std::fs;
use std::fs::File;
use std::io::Read;
use std::{fs, path::PathBuf};

pub fn get_hostname() -> Result<String, Box<dyn Error>> {
let hostname = gethostname::gethostname()
Expand All @@ -15,8 +14,8 @@ pub fn get_hostname() -> Result<String, Box<dyn Error>> {
/// Returns the port of the local aw-server instance
pub fn get_server_port(testing: bool) -> Result<u16, Box<dyn Error>> {
// TODO: get aw-server config more reliably
let aw_server_conf =
get_server_config_path(testing).map_err(|_| "Could not get aw-server config path")?;
let aw_server_conf = crate::dirs::get_server_config_path(testing)
.map_err(|_| "Could not get aw-server config path")?;
let fallback: u16 = if testing { 5666 } else { 5600 };
let port = if aw_server_conf.exists() {
let mut file = File::open(&aw_server_conf)?;
Expand All @@ -37,7 +36,7 @@ pub fn get_server_port(testing: bool) -> Result<u16, Box<dyn Error>> {
/// Return all remotes in the sync folder
pub fn get_remotes() -> Result<Vec<String>, Box<dyn Error>> {
let sync_root_dir = crate::dirs::get_sync_dir().map_err(|_| "Could not get sync dir")?;
let hostnames = fs::read_dir(&sync_root_dir)?
let hostnames = fs::read_dir(sync_root_dir)?
.filter_map(Result::ok)
.filter(|entry| entry.path().is_dir())
.filter_map(|entry| {
Expand Down
5 changes: 3 additions & 2 deletions aw-sync/test-sync-pull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ function sync_host() {
continue
fi

AWSYNCPARAMS="--port $PORT --sync-dir $SYNCDIR --sync-db $db"
AWSYNC_ARGS="--port $PORT"
AWSYNC_ARGS_ADV="--sync-dir $SYNCDIR --sync-db $db"
BUCKETS="aw-watcher-window_$host,aw-watcher-afk_$host"

echo "Syncing $db to $host"
cargo run --bin aw-sync -- $AWSYNCPARAMS sync-advanced --mode pull --buckets $BUCKETS
cargo run --bin aw-sync -- $AWSYNC_ARGS sync-advanced $AWSYNC_ARGS_ADV --mode pull --buckets $BUCKETS
# TODO: If there are no buckets from the expected host, emit a warning at the end.
# (push-script should not have created them to begin with)
done
Expand Down
5 changes: 3 additions & 2 deletions aw-sync/test-sync-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ else
fi

SYNCDIR="$HOME/ActivityWatchSync/$HOSTNAME"
AWSYNCPARAMS="--port $PORT --sync-dir $SYNCDIR"
AWSYNC_ARGS="--port $PORT"
AWSYNC_ARGS_ADV="--sync-dir $SYNCDIR"

# NOTE: Only sync window and AFK buckets, for now
cargo run --bin aw-sync --release -- $AWSYNCPARAMS sync-advanced --mode push --buckets aw-watcher-window_$HOSTNAME,aw-watcher-afk_$HOSTNAME
cargo run --bin aw-sync --release -- $AWSYNC_ARGS sync-advanced $AWSYNC_ARGS_ADV --mode push --buckets aw-watcher-window_$HOSTNAME,aw-watcher-afk_$HOSTNAME

0 comments on commit 1bd5602

Please sign in to comment.