diff options
author | Conrad Ludgate <conradludgate@gmail.com> | 2020-10-05 17:20:48 +0100 |
---|---|---|
committer | Conrad Ludgate <conradludgate@gmail.com> | 2020-10-05 17:20:48 +0100 |
commit | 28287a63031ec44fdda0eea99d757a7047df5a41 (patch) | |
tree | 67b2b73507f7ccceea100e333c47c651636c7fba /src/main.rs | |
parent | 9232ac27ce6d820c87d240061c1525866d69b2b2 (diff) |
feat: use directories project data dir
chore: clean up some things
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 80 |
1 files changed, 50 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs index 769c3235..f080a924 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,45 +1,72 @@ use std::env; +use std::path::PathBuf; +use directories::ProjectDirs; +use eyre::{eyre, Result}; use structopt::StructOpt; -use eyre::Result; -#[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; #[derive(StructOpt)] #[structopt( - author="Ellie Huxtable <e@elm.sh>", - version="0.1.0", - about="Keep your shell history in sync" + author = "Ellie Huxtable <e@elm.sh>", + version = "0.1.0", + about = "Keep your shell history in sync" )] -enum Shync { +struct Shync { + #[structopt(long, parse(from_os_str), about = "db file path")] + db: Option<PathBuf>, + + #[structopt(subcommand)] + shync: ShyncCmd, +} + +#[derive(StructOpt)] +enum ShyncCmd { #[structopt( about="manipulate shell history", aliases=&["h", "hi", "his", "hist", "histo", "histor"], )] History(HistoryCmd), - #[structopt( - about="import shell history from file", - )] + #[structopt(about = "import shell history from file")] Import, - #[structopt( - about="start a shync server", - )] + #[structopt(about = "start a shync server")] Server, } impl Shync { - fn run(self, db: SqliteDatabase) -> Result<()> { - match self { - Shync::History(history) => history.run(db), - _ => Ok(()) + 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 db = SqliteDatabase::new(db_path)?; + + match self.shync { + ShyncCmd::History(history) => history.run(db), + _ => Ok(()), } } } @@ -50,9 +77,7 @@ enum HistoryCmd { about="add a new command to the history", aliases=&["a", "ad"], )] - Add { - command: Vec<String>, - }, + Add { command: Vec<String> }, #[structopt( about="list all items in history", @@ -64,14 +89,10 @@ enum HistoryCmd { impl HistoryCmd { fn run(self, db: SqliteDatabase) -> Result<()> { match self { - HistoryCmd::Add{command: words} => { + HistoryCmd::Add { command: words } => { let command = words.join(" "); - - let cwd = env::current_dir()?; - let h = History::new( - command.as_str(), - cwd.display().to_string().as_str(), - ); + let cwd = env::current_dir()?.display().to_string(); + let h = History::new(command, cwd); debug!("adding history: {:?}", h); db.save(h)?; @@ -79,7 +100,7 @@ impl HistoryCmd { Ok(()) } - HistoryCmd::List => db.list() + HistoryCmd::List => db.list(), } } } @@ -87,6 +108,5 @@ impl HistoryCmd { fn main() -> Result<()> { pretty_env_logger::init(); - let db = SqliteDatabase::new("~/.history.db")?; - Shync::from_args().run(db) + Shync::from_args().run() } |