diff --git a/Cargo.lock b/Cargo.lock index f141b59..c0cd0ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,6 +227,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "sourcegraph" +version = "0.1.0" +dependencies = [ + "clap", + "dirs", + "eyre", + "util", +] + [[package]] name = "strsim" version = "0.10.0" @@ -290,6 +300,7 @@ version = "0.1.0" dependencies = [ "clap", "eyre", + "sourcegraph", "tldr", "util", ] diff --git a/Cargo.toml b/Cargo.toml index cd5e653..a95549d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [workspace] -members = ["crates/tldr", "crates/util"] +members = ["crates/tldr", "crates/util", "crates/sourcegraph"] [workspace.dependencies] clap = { version = "4.0.29", features = ["cargo"] } @@ -16,6 +16,7 @@ walkdir = "2.3.2" [dependencies] tldr = { path = "crates/tldr" } +sourcegraph = { path = "crates/sourcegraph" } util = { path = "crates/util" } clap.workspace = true diff --git a/crates/sourcegraph/Cargo.toml b/crates/sourcegraph/Cargo.toml new file mode 100644 index 0000000..d0d5258 --- /dev/null +++ b/crates/sourcegraph/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "sourcegraph" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +util = { path = "../util" } + +eyre.workspace = true +clap.workspace = true +dirs.workspace = true diff --git a/crates/sourcegraph/src/auth.rs b/crates/sourcegraph/src/auth.rs new file mode 100644 index 0000000..63d9e3a --- /dev/null +++ b/crates/sourcegraph/src/auth.rs @@ -0,0 +1,13 @@ +pub struct Auth; + +impl util::Cmd for Auth { + fn cmd() -> eyre::Result { + Ok(clap::Command::new("auth")) + } + + fn exec(_: &clap::ArgMatches) -> eyre::Result<()> { + util::shell::run(&["src", "login"])?; + + Ok(()) + } +} diff --git a/crates/sourcegraph/src/lib.rs b/crates/sourcegraph/src/lib.rs new file mode 100644 index 0000000..2350e02 --- /dev/null +++ b/crates/sourcegraph/src/lib.rs @@ -0,0 +1,25 @@ +mod auth; +mod search; + +pub struct Sourcegraph; + +impl Sourcegraph { + fn run() -> eyre::Result<()> { + Ok(()) + } +} + +impl util::Cmd for Sourcegraph { + fn cmd() -> eyre::Result { + Ok(clap::Command::new("sourcegraph") + .subcommands(&[auth::Auth::cmd()?, search::Search::cmd()?])) + } + + fn exec(args: &clap::ArgMatches) -> eyre::Result<()> { + match args.subcommand() { + Some(("auth", subm)) => auth::Auth::exec(subm), + Some(("search", subm)) => search::Search::exec(subm), + _ => Self::run(), + } + } +} diff --git a/crates/sourcegraph/src/search.rs b/crates/sourcegraph/src/search.rs new file mode 100644 index 0000000..d0c5065 --- /dev/null +++ b/crates/sourcegraph/src/search.rs @@ -0,0 +1,34 @@ +use std::{borrow::Borrow, ffi::OsString}; + +pub struct Search; + +impl util::Cmd for Search { + fn cmd() -> eyre::Result { + Ok(clap::Command::new("search") + .allow_external_subcommands(true) + .allow_missing_positional(true)) + } + + fn exec(args: &clap::ArgMatches) -> eyre::Result<()> { + match args.subcommand() { + Some((external, args)) => { + let mut raw = args + .get_many::("") + .ok_or(eyre::anyhow!("please pass some args to search"))? + .map(|s| s.as_os_str()) + .map(|s| s.to_string_lossy().to_string()) + .collect::>(); + let cmd = format!("src search {external} {}", raw.join(" ")); + println!("{cmd}"); + + let mut cmd_args = vec!["src", "search", "{external}"]; + cmd_args.append(&mut raw.iter().map(|s| &**s).collect()); + + util::shell::run(cmd_args.as_slice())?; + } + _ => todo!(), + } + + Ok(()) + } +} diff --git a/output.gif b/output.gif deleted file mode 100644 index 90a5002..0000000 Binary files a/output.gif and /dev/null differ diff --git a/src/main.rs b/src/main.rs index cb1f63b..4b3c120 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,17 @@ mod prereqs; fn main() -> eyre::Result<()> { let matches = clap::Command::new("toolkit") - .subcommands([prereqs::prereqs()?, tldr::Tldr::cmd()?]) + .subcommands([ + prereqs::prereqs()?, + tldr::Tldr::cmd()?, + sourcegraph::Sourcegraph::cmd()?, + ]) .get_matches(); match matches.subcommand() { Some(("prereqs", subcmd)) => prereqs_exec(subcmd), Some(("tldr", subcmd)) => tldr::Tldr::exec(subcmd), + Some(("sourcegraph", subcmd)) => sourcegraph::Sourcegraph::exec(subcmd), _ => Err(eyre::anyhow!("no command selected!")), } }