More convenient run_with_output

This commit is contained in:
Aleksey Kladov 2020-02-10 15:16:07 +01:00
parent 33df947d62
commit 1b6acc391a
3 changed files with 16 additions and 19 deletions

View file

@ -18,7 +18,7 @@ impl Cmd<'_> {
run(self.unix, self.work_dir)
}
}
pub fn run_with_output(self) -> Result<Output> {
pub fn run_with_output(self) -> Result<String> {
if cfg!(windows) {
run_with_output(self.windows, self.work_dir)
} else {
@ -34,8 +34,10 @@ pub fn run(cmdline: &str, dir: &str) -> Result<()> {
.map(|_| ())
}
pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
do_run(cmdline, dir, &mut |_| {})
pub fn run_with_output(cmdline: &str, dir: &str) -> Result<String> {
let output = do_run(cmdline, dir, &mut |_| {})?;
let stdout = String::from_utf8(output.stdout)?;
Ok(stdout)
}
fn do_run(cmdline: &str, dir: &str, f: &mut dyn FnMut(&mut Command)) -> Result<Output> {

View file

@ -127,15 +127,12 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
}
.run()?;
let installed_extensions = {
let output = Cmd {
let installed_extensions = Cmd {
unix: &format!(r"{} --list-extensions", code_binary),
windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary),
work_dir: ".",
}
.run_with_output()?;
String::from_utf8(output.stdout)?
};
if !installed_extensions.contains("rust-analyzer") {
anyhow::bail!(
@ -161,14 +158,12 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
fn install_server(opts: ServerOpt) -> Result<()> {
let mut old_rust = false;
if let Ok(output) = run_with_output("cargo --version", ".") {
if let Ok(stdout) = String::from_utf8(output.stdout) {
if let Ok(stdout) = run_with_output("cargo --version", ".") {
println!("{}", stdout);
if !check_version(&stdout, REQUIRED_RUST_VERSION) {
old_rust = true;
}
}
}
if old_rust {
eprintln!(

View file

@ -14,7 +14,7 @@ pub fn run_hook() -> Result<()> {
let diff = run_with_output("git diff --diff-filter=MAR --name-only --cached", ".")?;
let root = project_root();
for line in String::from_utf8(diff.stdout)?.lines() {
for line in diff.lines() {
run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
}