Factor replica and sync configuration into simple owned structs

This commit is contained in:
Dustin J. Mitchell 2020-11-28 16:57:32 -05:00
parent 87596bb1f0
commit 8af7ba286d
16 changed files with 81 additions and 23 deletions

View file

@ -38,7 +38,7 @@ define_subcommand! {
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
let t = command
.get_replica()
.get_replica()?
.new_task(Status::Pending, self.description.clone())
.unwrap();
println!("added task {}", t.get_uuid());

View file

@ -20,7 +20,7 @@ define_subcommand! {
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
command.get_replica().gc()?;
command.get_replica()?.gc()?;
println!("garbage collected.");
Ok(())
}

View file

@ -30,7 +30,7 @@ define_subcommand! {
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
let mut replica = command.get_replica();
let mut replica = command.get_replica()?;
let task = shared::get_task(&mut replica, &self.task)?;
let uuid = task.get_uuid();

View file

@ -23,7 +23,7 @@ define_subcommand! {
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
let mut replica = command.get_replica();
let mut replica = command.get_replica()?;
let mut t = Table::new();
t.set_format(table::format());
t.set_titles(row![b->"id", b->"description"]);

View file

@ -36,7 +36,7 @@ define_subcommand! {
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
let mut replica = command.get_replica();
let mut replica = command.get_replica()?;
let task = shared::get_task(&mut replica, &self.task)?;
let mut task = task.into_mut(&mut replica);

View file

@ -25,7 +25,7 @@ define_subcommand! {
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
let working_set = command.get_replica().working_set().unwrap();
let working_set = command.get_replica()?.working_set().unwrap();
let mut t = Table::new();
t.set_format(table::format());
t.set_titles(row![b->"id", b->"description"]);

View file

@ -2,7 +2,7 @@ use clap::Arg;
use failure::{format_err, Fallible};
use std::env;
use std::ffi::OsString;
use taskchampion::{server, taskstorage, Replica, Task, Uuid};
use taskchampion::{server, Replica, ReplicaConfig, ServerConfig, Task, Uuid};
pub(super) fn task_arg<'a>() -> Arg<'a, 'a> {
Arg::with_name("task")
@ -46,20 +46,29 @@ impl CommandInvocation {
// -- utilities for command invocations
pub(super) fn get_replica(&self) -> Replica {
pub(super) fn get_replica(&self) -> Fallible<Replica> {
// temporarily use $TASK_DB to locate the taskdb
let taskdb_dir = env::var_os("TASK_DB").unwrap_or_else(|| OsString::from("/tmp/tasks"));
Replica::new(Box::new(taskstorage::KVStorage::new(taskdb_dir).unwrap()))
let replica_config = ReplicaConfig {
taskdb_dir: taskdb_dir.into(),
};
Ok(Replica::from_config(replica_config)?)
}
pub(super) fn get_server(&self) -> Fallible<impl server::Server> {
pub(super) fn get_server(&self) -> Fallible<Box<dyn server::Server>> {
// temporarily use $SYNC_SERVER_ORIGIN for the sync server
let sync_server_origin = env::var_os("SYNC_SERVER_ORIGIN")
let origin = env::var_os("SYNC_SERVER_ORIGIN")
.map(|osstr| osstr.into_string().unwrap())
.unwrap_or_else(|| String::from("http://localhost:8080"));
Ok(server::RemoteServer::new(
sync_server_origin,
Uuid::parse_str("d5b55cbd-9a82-4860-9a39-41b67893b22f").unwrap(),
))
let client_id = env::var_os("SYNC_SERVER_CLIENT_ID")
.ok_or_else(|| format_err!("SYNC_SERVER_CLIENT_ID not set"))?;
let client_id = client_id
.into_string()
.map_err(|_| format_err!("SYNC_SERVER_CLIENT_ID is not a valid UUID"))?;
let client_id = Uuid::parse_str(&client_id)?;
Ok(server::from_config(ServerConfig::Remote {
origin,
client_id,
})?)
}
}

View file

@ -21,7 +21,7 @@ define_subcommand! {
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
let mut replica = command.get_replica();
let mut replica = command.get_replica()?;
let mut server = command.get_server()?;
replica.sync(&mut server)?;
Ok(())