From 1cc25ace7effce3075636cc50042ff07efb45a02 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 26 Dec 2016 22:14:45 -0500 Subject: [PATCH] print shell commands when the verbose flag is used closes #11 --- CHANGELOG.md | 1 + src/cargo.rs | 4 ++-- src/docker.rs | 12 +++++++----- src/extensions.rs | 23 +++++++++++++++-------- src/file.rs | 2 +- src/main.rs | 13 ++++++++----- src/rustc.rs | 4 ++-- src/rustup.rs | 8 ++++---- 8 files changed, 40 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3640e2864..3a95c605a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - Support for `x86_64-unknown-linux-musl` +- Print shell commands when the verbose flag is used. ## v0.1.0 - 2016-12-26 diff --git a/src/cargo.rs b/src/cargo.rs index abeffb60b..a1c461c6b 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -64,6 +64,6 @@ pub fn root() -> Result> { } /// Pass-through mode -pub fn run(args: &[String]) -> Result { - Command::new("cargo").args(args).run_and_get_exit_status() +pub fn run(args: &[String], verbose: bool) -> Result { + Command::new("cargo").args(args).run_and_get_exit_status(verbose) } diff --git a/src/docker.rs b/src/docker.rs index 7a090b1b9..004c1b68d 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -10,7 +10,7 @@ use id; use rustc; /// Register QEMU interpreters -pub fn register() -> Result<()> { +pub fn register(verbose: bool) -> Result<()> { Command::new("docker") .arg("run") .arg("--privileged") @@ -21,12 +21,13 @@ pub fn register() -> Result<()> { "-c", "apt-get update && apt-get install --no-install-recommends \ -y binfmt-support qemu-user-static"]) - .run() + .run(verbose) } pub fn run(target: Target, args: &[String], - cargo_root: &Path) + cargo_root: &Path, + verbose: bool) -> Result { let target = target.triple(); let cargo_dir = env::home_dir() @@ -56,10 +57,11 @@ pub fn run(target: Target, .args(&["-e", "CARGO_TARGET_DIR=/target"]) .args(&["-v", &format!("{}:/cargo", cargo_dir.display())]) .args(&["-v", &format!("{}:/project:ro", cargo_root.display())]) - .args(&["-v", &format!("{}:/rust:ro", rustc::sysroot()?.display())]) + .args(&["-v", + &format!("{}:/rust:ro", rustc::sysroot(verbose)?.display())]) .args(&["-v", &format!("{}:/target", target_dir.display())]) .args(&["-w", "/project"]) .args(&["-it", &format!("japaric/{}:{}", target, tag)]) .args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)]) - .run_and_get_exit_status() + .run_and_get_exit_status(verbose) } diff --git a/src/extensions.rs b/src/extensions.rs index d1f3a848e..deb5659ac 100644 --- a/src/extensions.rs +++ b/src/extensions.rs @@ -3,15 +3,15 @@ use std::process::{Command, ExitStatus}; use errors::*; pub trait CommandExt { - fn run(&mut self) -> Result<()>; - fn run_and_get_exit_status(&mut self) -> Result; - fn run_and_get_stdout(&mut self) -> Result; + fn run(&mut self, verbose: bool) -> Result<()>; + fn run_and_get_exit_status(&mut self, verbose: bool) -> Result; + fn run_and_get_stdout(&mut self, verbose: bool) -> Result; } impl CommandExt for Command { /// Runs the command to completion - fn run(&mut self) -> Result<()> { - let status = self.run_and_get_exit_status()?; + fn run(&mut self, verbose: bool) -> Result<()> { + let status = self.run_and_get_exit_status(verbose)?; if status.success() { Ok(()) @@ -23,13 +23,21 @@ impl CommandExt for Command { } /// Runs the command to completion - fn run_and_get_exit_status(&mut self) -> Result { + fn run_and_get_exit_status(&mut self, verbose: bool) -> Result { + if verbose { + println!("+ {:?}", self); + } + self.status() .chain_err(|| format!("couldn't execute `{:?}`", self)) } /// Runs the command to completion and returns its stdout - fn run_and_get_stdout(&mut self) -> Result { + fn run_and_get_stdout(&mut self, verbose: bool) -> Result { + if verbose { + println!("+ {:?}", self); + } + let out = self.output() .chain_err(|| format!("couldn't execute `{:?}`", self))?; @@ -43,6 +51,5 @@ impl CommandExt for Command { self, out.status.code()))? } - } } diff --git a/src/file.rs b/src/file.rs index e379572d1..40427768c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -13,7 +13,7 @@ pub fn read

(path: P) -> Result fn read_(path: &Path) -> Result { let mut s = String::new(); File::open(path).chain_err(|| format!("couldn't open {}", path.display()))? - .read_to_string(&mut s) + .read_to_string(&mut s) .chain_err(|| format!("couldn't read {}", path.display()))?; Ok(s) } diff --git a/src/main.rs b/src/main.rs index 8362ae156..bb40d4379 100644 --- a/src/main.rs +++ b/src/main.rs @@ -171,6 +171,9 @@ fn run() -> Result { include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))); } + let verbose = + args.all.iter().any(|a| a == "--verbose" || a == "-v" || a == "-vv"); + let host = rustc::host(); if host == Host::X86_64UnknownLinuxGnu { @@ -179,20 +182,20 @@ fn run() -> Result { if target.needs_docker() && args.subcommand.map(|sc| sc.needs_docker()).unwrap_or(false) { if let Some(root) = cargo::root()? { - if !rustup::installed_targets()?.contains(&target) { - rustup::install(target)?; + if !rustup::installed_targets(verbose)?.contains(&target) { + rustup::install(target, verbose)?; } if args.subcommand.map(|sc| sc.needs_qemu()).unwrap_or(false) && target.needs_qemu() && !qemu::is_registered()? { - docker::register()? + docker::register(verbose)? } - return docker::run(target, &args.all, &root); + return docker::run(target, &args.all, &root, verbose); } } } - cargo::run(&args.all) + cargo::run(&args.all, verbose) } diff --git a/src/rustc.rs b/src/rustc.rs index bc9e63a16..4cc1062c0 100644 --- a/src/rustc.rs +++ b/src/rustc.rs @@ -11,9 +11,9 @@ pub fn host() -> Host { Host::from(&*rustc_version::version_meta().host) } -pub fn sysroot() -> Result { +pub fn sysroot(verbose: bool) -> Result { let mut stdout = Command::new("rustc").args(&["--print", "sysroot"]) - .run_and_get_stdout()?; + .run_and_get_stdout(verbose)?; if stdout.ends_with('\n') { stdout.pop(); diff --git a/src/rustup.rs b/src/rustup.rs index 96f16e78d..2460843c1 100644 --- a/src/rustup.rs +++ b/src/rustup.rs @@ -4,18 +4,18 @@ use Target; use errors::*; use extensions::CommandExt; -pub fn install(target: Target) -> Result<()> { +pub fn install(target: Target, verbose: bool) -> Result<()> { let target = target.triple(); Command::new("rustup") .args(&["target", "install", target]) - .run() + .run(verbose) .chain_err(|| format!("couldn't install `std` for {}", target)) } -pub fn installed_targets() -> Result> { +pub fn installed_targets(verbose: bool) -> Result> { let out = Command::new("rustup").args(&["target", "list"]) - .run_and_get_stdout()?; + .run_and_get_stdout(verbose)?; Ok(out.lines() .filter_map(|line| if line.contains("installed") ||