Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added sync dir config via global --sync-dir cli param and AW_SYNC_DIR env var #457

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions aw-sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,28 @@ Was originally prototyped as a PR to aw-server: https://github.com/ActivityWatch

## Usage

This will start a daemon which both pulls and pushes events with the sync directory.
This will start a daemon which pulls and pushes events with the sync directory (`~/ActivityWatchSync` by default) every 5 minutes:

```sh
cargo run --bin aw-sync
aw-sync
```

For more options, see `cargo run --bin aw-sync -- --help`.
For more options, see `aw-sync --help`.

---
### Setting up sync

Once you have aw-sync running, you need to set up syncing with the sync directory using your preferred syncing tool.

The default sync directory is `~/ActivityWatchSync`, but you can change it using the `--sync-dir` option or by setting the `AW_SYNC_DIR` environment variable.

### Running from source

If you want to run it from source, in the root of the repository run:

```sh
cargo run --bin aw-sync
```
For more options, see `cargo run --bin aw-sync -- --help`.

## FAQ

Expand Down
5 changes: 4 additions & 1 deletion aw-sync/src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub fn get_server_config_path(testing: bool) -> Result<PathBuf, ()> {
}

pub fn get_sync_dir() -> Result<PathBuf, Box<dyn Error>> {
// TODO: make this configurable
// if AW_SYNC_DIR is set, use that
if let Ok(dir) = std::env::var("AW_SYNC_DIR") {
return Ok(PathBuf::from(dir));
}
let home_dir = home_dir().ok_or("Unable to read home_dir")?;
Ok(home_dir.join("ActivityWatchSync"))
}
28 changes: 16 additions & 12 deletions aw-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ struct Opts {
#[clap(long)]
testing: bool,

/// Full path to sync directory.
/// If not specified, use AW_SYNC_DIR env var, or default to ~/ActivityWatchSync
#[clap(long)]
sync_dir: Option<PathBuf>,

/// Enable debug logging.
#[clap(long)]
verbose: bool,
Expand Down Expand Up @@ -89,11 +94,6 @@ enum Commands {
#[clap(long, default_value = "both")]
mode: sync::SyncMode,

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

/// 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.
Expand Down Expand Up @@ -121,6 +121,16 @@ fn main() -> Result<(), Box<dyn Error>> {

aw_server::logging::setup_logger("aw-sync", opts.testing, verbose)?;

// if sync_dir, set env var
if let Some(sync_dir) = opts.sync_dir {
if !sync_dir.is_absolute() {
Err("Sync dir must be absolute")?
}

info!("Using sync dir: {}", &sync_dir.display());
std::env::set_var("AW_SYNC_DIR", sync_dir);
}

let port = opts
.port
.map(|a| Ok(a))
Expand Down Expand Up @@ -160,15 +170,9 @@ fn main() -> Result<(), Box<dyn Error>> {
start_date,
buckets,
mode,
sync_dir,
sync_db,
} => {
if !sync_dir.is_absolute() {
Err("Sync dir must be absolute")?
}

info!("Using sync dir: {}", &sync_dir.display());

let sync_dir = dirs::get_sync_dir()?;
if let Some(db_path) = &sync_db {
info!("Using sync db: {}", &db_path.display());

Expand Down
Loading