From 66105004e091b8940ad84e9fc152f11b92a66856 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 17 Dec 2022 23:27:41 +0100 Subject: [PATCH] add basic github --- Cargo.lock | 11 ++++++++++ Cargo.toml | 3 ++- crates/github/Cargo.toml | 13 +++++++++++ crates/github/src/auth.rs | 13 +++++++++++ crates/github/src/gh.rs | 38 ++++++++++++++++++++++++++++++++ crates/github/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++ crates/sourcegraph/src/lib.rs | 4 +++- src/main.rs | 2 ++ 8 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 crates/github/Cargo.toml create mode 100644 crates/github/src/auth.rs create mode 100644 crates/github/src/gh.rs create mode 100644 crates/github/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c0cd0ed..e25e09b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,6 +105,16 @@ dependencies = [ "wasi", ] +[[package]] +name = "github" +version = "0.1.0" +dependencies = [ + "clap", + "dirs", + "eyre", + "util", +] + [[package]] name = "hermit-abi" version = "0.2.6" @@ -300,6 +310,7 @@ version = "0.1.0" dependencies = [ "clap", "eyre", + "github", "sourcegraph", "tldr", "util", diff --git a/Cargo.toml b/Cargo.toml index a95549d..6c90b50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [workspace] -members = ["crates/tldr", "crates/util", "crates/sourcegraph"] +members = ["crates/tldr", "crates/util", "crates/sourcegraph", "crates/github"] [workspace.dependencies] clap = { version = "4.0.29", features = ["cargo"] } @@ -15,6 +15,7 @@ walkdir = "2.3.2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +github = { path = "crates/github" } tldr = { path = "crates/tldr" } sourcegraph = { path = "crates/sourcegraph" } util = { path = "crates/util" } diff --git a/crates/github/Cargo.toml b/crates/github/Cargo.toml new file mode 100644 index 0000000..0548883 --- /dev/null +++ b/crates/github/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "github" +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/github/src/auth.rs b/crates/github/src/auth.rs new file mode 100644 index 0000000..d4498d0 --- /dev/null +++ b/crates/github/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(&["gh", "auth", "login"])?; + + Ok(()) + } +} diff --git a/crates/github/src/gh.rs b/crates/github/src/gh.rs new file mode 100644 index 0000000..a33551e --- /dev/null +++ b/crates/github/src/gh.rs @@ -0,0 +1,38 @@ +pub struct Gh; + +impl Gh { + fn run(external: &str, args: &clap::ArgMatches) -> eyre::Result<()> { + let 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!["gh", external]; + cmd_args.append(&mut raw.iter().map(|s| &**s).collect()); + + util::shell::run(cmd_args.as_slice())?; + + Ok(()) + } +} + +impl util::Cmd for Gh { + fn cmd() -> eyre::Result { + Ok(clap::Command::new("gh").allow_external_subcommands(true)) + } + + fn exec(args: &clap::ArgMatches) -> eyre::Result<()> { + match args.subcommand() { + Some((external, args)) => Self::run(external, args), + _ => { + util::shell::run(&["gh"])?; + + Err(eyre::anyhow!("missing argument")) + } + } + } +} diff --git a/crates/github/src/lib.rs b/crates/github/src/lib.rs new file mode 100644 index 0000000..03d6fa9 --- /dev/null +++ b/crates/github/src/lib.rs @@ -0,0 +1,41 @@ +mod auth; +mod gh; + +pub struct GitHub; + +impl GitHub { + fn run(external: &str, args: &clap::ArgMatches) -> eyre::Result<()> { + let 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!["gh", external]; + cmd_args.append(&mut raw.iter().map(|s| &**s).collect()); + + util::shell::run(cmd_args.as_slice())?; + + Ok(()) + } +} + +impl util::Cmd for GitHub { + fn cmd() -> eyre::Result { + Ok(clap::Command::new("github") + .subcommands(&[auth::Auth::cmd()?, gh::Gh::cmd()?]) + .allow_external_subcommands(true)) + } + + fn exec(args: &clap::ArgMatches) -> eyre::Result<()> { + match args.subcommand() { + Some(("auth", subm)) => auth::Auth::exec(subm), + Some(("gh", subm)) => gh::Gh::exec(subm), + Some((external, args)) => Self::run(external, args), + _ => Err(eyre::anyhow!("missing argument")), + } + } +} diff --git a/crates/sourcegraph/src/lib.rs b/crates/sourcegraph/src/lib.rs index 2350e02..05f691e 100644 --- a/crates/sourcegraph/src/lib.rs +++ b/crates/sourcegraph/src/lib.rs @@ -5,7 +5,9 @@ pub struct Sourcegraph; impl Sourcegraph { fn run() -> eyre::Result<()> { - Ok(()) + util::shell::run(&["src"])?; + + Err(eyre::anyhow!("missing argument")) } } diff --git a/src/main.rs b/src/main.rs index 4b3c120..1d88cc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ fn main() -> eyre::Result<()> { prereqs::prereqs()?, tldr::Tldr::cmd()?, sourcegraph::Sourcegraph::cmd()?, + github::GitHub::cmd()?, ]) .get_matches(); @@ -16,6 +17,7 @@ fn main() -> eyre::Result<()> { Some(("prereqs", subcmd)) => prereqs_exec(subcmd), Some(("tldr", subcmd)) => tldr::Tldr::exec(subcmd), Some(("sourcegraph", subcmd)) => sourcegraph::Sourcegraph::exec(subcmd), + Some(("github", subcmd)) => github::GitHub::exec(subcmd), _ => Err(eyre::anyhow!("no command selected!")), } }