From 34038f6aaad493c6d1cd5b26a40249245c048df6 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Mon, 5 Oct 2020 11:52:03 +0100 Subject: [PATCH] chore: use structopt wrapper instead of building clap by hand --- Cargo.lock | 110 +++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- src/main.rs | 133 +++++++++++++++++++++++++++++----------------------- 3 files changed, 183 insertions(+), 62 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b8cb07..07527f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,6 +188,15 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1", ] +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "hermit-abi" version = "0.1.16" @@ -220,9 +229,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.78" +version = "0.2.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa7087f49d294270db4e1928fc110c976cd4b9e5a16348e0a1df09afa99e6c98" +checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743" [[package]] name = "libsqlite3-sys" @@ -305,12 +314,54 @@ dependencies = [ "log", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + [[package]] name = "quick-error" version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -387,12 +438,12 @@ name = "shync" version = "0.1.0" dependencies = [ "chrono", - "clap", "eyre", "log", "pretty_env_logger", "rusqlite", "shellexpand", + "structopt", ] [[package]] @@ -407,6 +458,41 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "structopt" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33f6461027d7f08a13715659b2948e1602c31a3756aeae9378bfe7518c72e82" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c92e775028122a4b3dd55d58f14fc5120289c69bee99df1d117ae30f84b225c9" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c51d92969d209b54a98397e1b91c8ae82d8c87a7bb87df0b29aa2ad81454228" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "termcolor" version = "1.1.0" @@ -445,12 +531,24 @@ dependencies = [ "winapi", ] +[[package]] +name = "unicode-segmentation" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" + [[package]] name = "unicode-width" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + [[package]] name = "vcpkg" version = "0.2.10" @@ -463,6 +561,12 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 169a2c9..f91f12c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,8 @@ edition = "2018" [dependencies] log = "0.4" pretty_env_logger = "0.4" -clap = "2.33.3" chrono = "0.4.19" eyre = "0.6.1" shellexpand = "2.0.0" rusqlite = "0.24.0" +structopt = "0.3.15" diff --git a/src/main.rs b/src/main.rs index a9b08c0..769c323 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::env; -use clap::{Arg, App, SubCommand}; +use structopt::StructOpt; use eyre::Result; #[macro_use] extern crate log; @@ -11,65 +11,82 @@ mod local; use local::history::History; use local::database::{Database, SqliteDatabase}; +#[derive(StructOpt)] +#[structopt( + author="Ellie Huxtable ", + version="0.1.0", + about="Keep your shell history in sync" +)] +enum Shync { + #[structopt( + about="manipulate shell history", + aliases=&["h", "hi", "his", "hist", "histo", "histor"], + )] + History(HistoryCmd), + + #[structopt( + about="import shell history from file", + )] + Import, + + #[structopt( + about="start a shync server", + )] + Server, +} + +impl Shync { + fn run(self, db: SqliteDatabase) -> Result<()> { + match self { + Shync::History(history) => history.run(db), + _ => Ok(()) + } + } +} + +#[derive(StructOpt)] +enum HistoryCmd { + #[structopt( + about="add a new command to the history", + aliases=&["a", "ad"], + )] + Add { + command: Vec, + }, + + #[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()?; + let h = History::new( + command.as_str(), + cwd.display().to_string().as_str(), + ); + + debug!("adding history: {:?}", h); + db.save(h)?; + debug!("saved history to sqlite"); + Ok(()) + } + + HistoryCmd::List => db.list() + } + } +} + fn main() -> Result<()> { pretty_env_logger::init(); let db = SqliteDatabase::new("~/.history.db")?; - - let matches = App::new("Shync") - .version("0.1.0") - .author("Ellie Huxtable ") - .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(); - - - 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(" "); - - let cwd = env::current_dir()?; - let h = History::new( - command.as_str(), - cwd.display().to_string().as_str(), - ); - - debug!("adding history: {:?}", h); - db.save(h)?; - debug!("saved history to sqlite"); - } - else if let Some(_m) = m.subcommand_matches("list") { - db.list()?; - } - } - - Ok(()) + Shync::from_args().run(db) }