implement backend command

This commit is contained in:
Abraham Toriz 2021-07-26 20:07:11 -05:00
parent 4011cac6e9
commit 8cc1eb1cef
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
4 changed files with 39 additions and 1 deletions

View File

@ -18,6 +18,7 @@ pub mod month;
pub mod list;
pub mod out;
pub mod resume;
pub mod backend;
pub trait Command<'a> {
type Args: TryFrom<&'a ArgMatches<'a>>;

25
src/commands/backend.rs Normal file
View File

@ -0,0 +1,25 @@
use std::process::{Command, Stdio};
use crate::error::Result;
use crate::config::Config;
use crate::error::Error::*;
pub struct BackendCommand;
impl BackendCommand {
pub fn handle(config: &Config) -> Result<()> {
let status = Command::new("sqlite3")
.arg(&config.database_file)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output().map_err(|e| Sqlite3CommandFailed(e))?
.status;
if status.success() {
Ok(())
} else {
Err(Sqlite3CommandFailedUnkown)
}
}
}

View File

@ -158,6 +158,14 @@ Is there a permissions issue? Here's the underlaying error:
path: PathBuf,
error: String,
},
#[error("Trying to run system command sqlite3 failed with error:
{0}")]
Sqlite3CommandFailed(std::io::Error),
#[error("Trying to run system command sqlite3 failed")]
Sqlite3CommandFailedUnkown,
}
pub type Result<T> = result::Result<T, Error>;

View File

@ -13,12 +13,16 @@ use tiempo::commands::{
Command, r#in::InCommand, display::DisplayCommand, sheet::SheetCommand,
today::TodayCommand, yesterday::YesterdayCommand, week::WeekCommand,
month::MonthCommand, list::ListCommand, out::OutCommand,
resume::ResumeCommand,
resume::ResumeCommand, backend::BackendCommand,
};
fn error_trap(matches: ArgMatches) -> error::Result<()> {
let config = Config::read()?;
if let Some(_matches) = matches.subcommand_matches("backend") {
return BackendCommand::handle(&config);
}
let mut conn = SqliteDatabase::from_path_or_create(&config.database_file)?;
let mut out = io::stdout();
let mut err = io::stderr();