add tldr and base

This commit is contained in:
2022-12-17 21:51:41 +01:00
commit 6dc747f8b0
13 changed files with 764 additions and 0 deletions

10
crates/util/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "util"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
eyre.workspace = true
clap.workspace = true

7
crates/util/src/lib.rs Normal file
View File

@@ -0,0 +1,7 @@
pub mod shell;
pub trait Cmd {
fn cmd() -> eyre::Result<clap::Command>;
fn exec(args: &clap::ArgMatches) -> eyre::Result<()>;
}

123
crates/util/src/shell.rs Normal file
View File

@@ -0,0 +1,123 @@
use std::io::Write;
pub fn run(args: &[&str]) -> eyre::Result<()> {
let output = std::process::Command::new(
args.first()
.ok_or(eyre::anyhow!("could not find first arg"))?,
)
.args(
args.to_vec()
.into_iter()
.skip(1)
.collect::<Vec<&str>>()
.as_slice(),
)
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.stdin(std::process::Stdio::inherit())
.output();
match output {
Ok(o) => {
if o.status.success() {
Ok(())
} else {
Err(eyre::anyhow!(
"command failed with statuscode: {}",
o.status
.code()
.ok_or(eyre::anyhow!("could not get a status code from process"))?
))
}
}
Err(e) => Err(eyre::anyhow!(e)),
}
}
pub fn run_with_input(args: &[&str], input: String) -> eyre::Result<()> {
let output = std::process::Command::new(
args.first()
.ok_or(eyre::anyhow!("could not find first arg"))?,
)
.args(
args.to_vec()
.into_iter()
.skip(1)
.collect::<Vec<&str>>()
.as_slice(),
)
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.stdin(std::process::Stdio::piped())
.spawn();
match output {
Ok(mut o) => {
let stdin = o
.stdin
.as_mut()
.ok_or(eyre::anyhow!("could not acquire stdin"))?;
stdin.write_all(input.as_bytes())?;
drop(stdin);
let o = o.wait_with_output()?;
if o.status.success() {
Ok(())
} else {
Err(eyre::anyhow!(
"command failed with statuscode: {}",
o.status
.code()
.ok_or(eyre::anyhow!("could not get a status code from process"))?
))
}
}
Err(e) => Err(eyre::anyhow!(e)),
}
}
pub fn run_with_input_and_output(
args: &[&str],
input: String,
) -> eyre::Result<std::process::Output> {
let output = std::process::Command::new(
args.first()
.ok_or(eyre::anyhow!("could not find first arg"))?,
)
.args(
args.to_vec()
.into_iter()
.skip(1)
.collect::<Vec<&str>>()
.as_slice(),
)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::inherit())
.stdin(std::process::Stdio::piped())
.spawn();
match output {
Ok(mut o) => {
let stdin = o
.stdin
.as_mut()
.ok_or(eyre::anyhow!("could not acquire stdin"))?;
stdin.write_all(input.as_bytes())?;
drop(stdin);
let o = o.wait_with_output()?;
if o.status.success() {
Ok(o)
} else {
Err(eyre::anyhow!(
"command failed with statuscode: {}",
o.status
.code()
.ok_or(eyre::anyhow!("could not get a status code from process"))?
))
}
}
Err(e) => Err(eyre::anyhow!(e)),
}
}