diff options
| author | Ellie Huxtable <e@elm.sh> | 2020-10-05 20:36:15 +0100 |
|---|---|---|
| committer | Ellie Huxtable <e@elm.sh> | 2020-10-05 20:38:34 +0100 |
| commit | 9917d6c1e2a743d5666247d8e87c9791721c213e (patch) | |
| tree | 088df4af1041d53bd1faa38d5542200689be57a1 /src | |
| parent | Use bundled sqlite (diff) | |
| parent | Merge pull request #2 from conradludgate/main (diff) | |
| download | atuin-9917d6c1e2a743d5666247d8e87c9791721c213e.zip | |
Fix merge
...I forgot to push. oops.
Diffstat (limited to 'src')
| -rw-r--r-- | src/local/database.rs | 46 | ||||
| -rw-r--r-- | src/local/history.rs | 6 | ||||
| -rw-r--r-- | src/local/mod.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 147 |
4 files changed, 121 insertions, 80 deletions
diff --git a/src/local/database.rs b/src/local/database.rs index b84bf99c..b94a6445 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -1,12 +1,11 @@ use std::path::Path; use eyre::Result; -use shellexpand; -use rusqlite::{params, Connection}; use rusqlite::NO_PARAMS; +use rusqlite::{params, Connection}; -use super::history::History; +use crate::History; pub trait Database { fn save(&self, h: History) -> Result<()>; @@ -19,23 +18,25 @@ pub struct SqliteDatabase { conn: Connection, } -impl SqliteDatabase{ - pub fn new(path: &str) -> Result<SqliteDatabase> { - let path = shellexpand::full(path)?; +impl SqliteDatabase { + pub fn new(path: impl AsRef<Path>) -> Result<SqliteDatabase> { let path = path.as_ref(); - debug!("opening sqlite database at {:?}", path); - let create = !Path::new(path).exists(); + let create = !path.exists(); + if create { + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir)?; + } + } + let conn = Connection::open(path)?; if create { Self::setup_db(&conn)?; } - Ok(SqliteDatabase{ - conn: conn, - }) + Ok(SqliteDatabase { conn }) } fn setup_db(conn: &Connection) -> Result<()> { @@ -43,11 +44,11 @@ impl SqliteDatabase{ conn.execute( "create table if not exists history ( - id integer primary key, - timestamp integer not null, - command text not null, - cwd text not null - )", + id integer primary key, + timestamp integer not null, + command text not null, + cwd text not null + )", NO_PARAMS, )?; @@ -61,19 +62,22 @@ impl Database for SqliteDatabase { self.conn.execute( "insert into history ( - timestamp, + timestamp, command, cwd - ) values (?1, ?2, ?3)", - params![h.timestamp, h.command, h.cwd])?; + ) values (?1, ?2, ?3)", + params![h.timestamp, h.command, h.cwd], + )?; Ok(()) } fn list(&self) -> Result<()> { debug!("listing history"); - - let mut stmt = self.conn.prepare("SELECT timestamp, command, cwd FROM history")?; + + let mut stmt = self + .conn + .prepare("SELECT timestamp, command, cwd FROM history")?; let history_iter = stmt.query_map(params![], |row| { Ok(History { timestamp: row.get(0)?, diff --git a/src/local/history.rs b/src/local/history.rs index 61438b6d..e84d718c 100644 --- a/src/local/history.rs +++ b/src/local/history.rs @@ -8,11 +8,11 @@ pub struct History { } impl History { - pub fn new(command: &str, cwd: &str) -> History { + pub fn new(command: String, cwd: String) -> History { History { timestamp: chrono::Utc::now().timestamp_millis(), - command: command.to_string(), - cwd: cwd.to_string(), + command, + cwd, } } } diff --git a/src/local/mod.rs b/src/local/mod.rs index 8854aa8e..f587d016 100644 --- a/src/local/mod.rs +++ b/src/local/mod.rs @@ -1,2 +1,2 @@ -pub mod history; pub mod database; +pub mod history; diff --git a/src/main.rs b/src/main.rs index a9b08c00..2ec14a50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,75 +1,112 @@ use std::env; +use std::path::PathBuf; -use clap::{Arg, App, SubCommand}; -use eyre::Result; +use directories::ProjectDirs; +use eyre::{eyre, Result}; +use structopt::StructOpt; -#[macro_use] extern crate log; +#[macro_use] +extern crate log; use pretty_env_logger; mod local; -use local::history::History; use local::database::{Database, SqliteDatabase}; +use local::history::History; -fn main() -> Result<()> { - pretty_env_logger::init(); +#[derive(StructOpt)] +#[structopt( + author = "Ellie Huxtable <e@elm.sh>", + version = "0.1.0", + about = "Keep your shell history in sync" +)] +struct Shync { + #[structopt(long, parse(from_os_str), help = "db file path")] + db: Option<PathBuf>, + + #[structopt(subcommand)] + shync: ShyncCmd, +} - let db = SqliteDatabase::new("~/.history.db")?; +#[derive(StructOpt)] +enum ShyncCmd { + #[structopt( + about="manipulate shell history", + aliases=&["h", "hi", "his", "hist", "histo", "histor"], + )] + History(HistoryCmd), - let matches = App::new("Shync") - .version("0.1.0") - .author("Ellie Huxtable <e@elm.sh>") - .about("Keep your shell history in sync") - .subcommand( - SubCommand::with_name("history") - .aliases(&["h", "hi", "his", "hist", "histo", "histor"]) - .about("manipulate shell history") - .subcommand( - SubCommand::with_name("add") - .aliases(&["a", "ad"]) - .about("add a new command to the history") - .arg( - Arg::with_name("command") - .multiple(true) - .required(true) - ) - ) - .subcommand( - SubCommand::with_name("list") - .aliases(&["l", "li", "lis"]) - .about("list all items in history") - ) - ) - .subcommand( - SubCommand::with_name("import") - .about("import shell history from file") - ) - .subcommand( - SubCommand::with_name("server") - .about("start a shync server") - ) - .get_matches(); + #[structopt(about = "import shell history from file")] + Import, + #[structopt(about = "start a shync server")] + Server, +} - if let Some(m) = matches.subcommand_matches("history") { - if let Some(m) = m.subcommand_matches("add") { - let words: Vec<&str> = m.values_of("command").unwrap().collect(); - let command = words.join(" "); +impl Shync { + fn run(self) -> Result<()> { + let db_path = match self.db { + Some(db_path) => { + let path = db_path + .to_str() + .ok_or(eyre!("path {:?} was not valid UTF-8", db_path))?; + let path = shellexpand::full(path)?; + PathBuf::from(path.as_ref()) + } + None => { + let project_dirs = ProjectDirs::from("bike", "ellie", "shync").ok_or(eyre!( + "could not determine db file location\nspecify one using the --db flag" + ))?; + let root = project_dirs.data_dir(); + root.join("history.db") + } + }; - let cwd = env::current_dir()?; - let h = History::new( - command.as_str(), - cwd.display().to_string().as_str(), - ); + let db = SqliteDatabase::new(db_path)?; - debug!("adding history: {:?}", h); - db.save(h)?; - debug!("saved history to sqlite"); + match self.shync { + ShyncCmd::History(history) => history.run(db), + _ => Ok(()), } - else if let Some(_m) = m.subcommand_matches("list") { - db.list()?; + } +} + +#[derive(StructOpt)] +enum HistoryCmd { + #[structopt( + about="add a new command to the history", + aliases=&["a", "ad"], + )] + Add { command: Vec<String> }, + + #[structopt( + about="list all items in history", + aliases=&["l", "li", "lis"], + )] + List, +} + +impl HistoryCmd { + fn run(self, db: SqliteDatabase) -> Result<()> { + match self { + HistoryCmd::Add { command: words } => { + let command = words.join(" "); + let cwd = env::current_dir()?.display().to_string(); + let h = History::new(command, cwd); + + debug!("adding history: {:?}", h); + db.save(h)?; + debug!("saved history to sqlite"); + Ok(()) + } + + HistoryCmd::List => db.list(), } } +} + +fn main() -> Result<()> { + pretty_env_logger::init(); - Ok(()) + Shync::from_args().run() } |
