add basic github

This commit is contained in:
2022-12-17 23:27:41 +01:00
parent 3300bb1ee1
commit 66105004e0
8 changed files with 123 additions and 2 deletions

13
crates/github/Cargo.toml Normal file
View File

@@ -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

13
crates/github/src/auth.rs Normal file
View File

@@ -0,0 +1,13 @@
pub struct Auth;
impl util::Cmd for Auth {
fn cmd() -> eyre::Result<clap::Command> {
Ok(clap::Command::new("auth"))
}
fn exec(_: &clap::ArgMatches) -> eyre::Result<()> {
util::shell::run(&["gh", "auth", "login"])?;
Ok(())
}
}

38
crates/github/src/gh.rs Normal file
View File

@@ -0,0 +1,38 @@
pub struct Gh;
impl Gh {
fn run(external: &str, args: &clap::ArgMatches) -> eyre::Result<()> {
let raw = args
.get_many::<std::ffi::OsString>("")
.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::<Vec<String>>();
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<clap::Command> {
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"))
}
}
}
}

41
crates/github/src/lib.rs Normal file
View File

@@ -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::<std::ffi::OsString>("")
.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::<Vec<String>>();
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<clap::Command> {
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")),
}
}
}

View File

@@ -5,7 +5,9 @@ pub struct Sourcegraph;
impl Sourcegraph {
fn run() -> eyre::Result<()> {
Ok(())
util::shell::run(&["src"])?;
Err(eyre::anyhow!("missing argument"))
}
}