aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2020-10-05 20:36:15 +0100
committerEllie Huxtable <e@elm.sh>2020-10-05 20:38:34 +0100
commit9917d6c1e2a743d5666247d8e87c9791721c213e (patch)
tree088df4af1041d53bd1faa38d5542200689be57a1 /src/main.rs
parentUse bundled sqlite (diff)
parentMerge pull request #2 from conradludgate/main (diff)
downloadatuin-9917d6c1e2a743d5666247d8e87c9791721c213e.zip
Fix merge
...I forgot to push. oops.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs147
1 files changed, 92 insertions, 55 deletions
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()
}