add basic github

This commit is contained in:
Kasper Juul Hermansen 2022-12-17 23:27:41 +01:00
parent 3300bb1ee1
commit 66105004e0
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
8 changed files with 123 additions and 2 deletions

11
Cargo.lock generated
View File

@ -105,6 +105,16 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "github"
version = "0.1.0"
dependencies = [
"clap",
"dirs",
"eyre",
"util",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@ -300,6 +310,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"eyre", "eyre",
"github",
"sourcegraph", "sourcegraph",
"tldr", "tldr",
"util", "util",

View File

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[workspace] [workspace]
members = ["crates/tldr", "crates/util", "crates/sourcegraph"] members = ["crates/tldr", "crates/util", "crates/sourcegraph", "crates/github"]
[workspace.dependencies] [workspace.dependencies]
clap = { version = "4.0.29", features = ["cargo"] } 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
github = { path = "crates/github" }
tldr = { path = "crates/tldr" } tldr = { path = "crates/tldr" }
sourcegraph = { path = "crates/sourcegraph" } sourcegraph = { path = "crates/sourcegraph" }
util = { path = "crates/util" } util = { path = "crates/util" }

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 { impl Sourcegraph {
fn run() -> eyre::Result<()> { fn run() -> eyre::Result<()> {
Ok(()) util::shell::run(&["src"])?;
Err(eyre::anyhow!("missing argument"))
} }
} }

View File

@ -9,6 +9,7 @@ fn main() -> eyre::Result<()> {
prereqs::prereqs()?, prereqs::prereqs()?,
tldr::Tldr::cmd()?, tldr::Tldr::cmd()?,
sourcegraph::Sourcegraph::cmd()?, sourcegraph::Sourcegraph::cmd()?,
github::GitHub::cmd()?,
]) ])
.get_matches(); .get_matches();
@ -16,6 +17,7 @@ fn main() -> eyre::Result<()> {
Some(("prereqs", subcmd)) => prereqs_exec(subcmd), Some(("prereqs", subcmd)) => prereqs_exec(subcmd),
Some(("tldr", subcmd)) => tldr::Tldr::exec(subcmd), Some(("tldr", subcmd)) => tldr::Tldr::exec(subcmd),
Some(("sourcegraph", subcmd)) => sourcegraph::Sourcegraph::exec(subcmd), Some(("sourcegraph", subcmd)) => sourcegraph::Sourcegraph::exec(subcmd),
Some(("github", subcmd)) => github::GitHub::exec(subcmd),
_ => Err(eyre::anyhow!("no command selected!")), _ => Err(eyre::anyhow!("no command selected!")),
} }
} }