Skip to content

Commit

Permalink
print shell commands when the verbose flag is used
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Aparicio committed Dec 27, 2016
1 parent 038ecf4 commit 1cc25ac
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ pub fn root() -> Result<Option<PathBuf>> {
}

/// Pass-through mode
pub fn run(args: &[String]) -> Result<ExitStatus> {
Command::new("cargo").args(args).run_and_get_exit_status()
pub fn run(args: &[String], verbose: bool) -> Result<ExitStatus> {
Command::new("cargo").args(args).run_and_get_exit_status(verbose)
}
12 changes: 7 additions & 5 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<ExitStatus> {
let target = target.triple();
let cargo_dir = env::home_dir()
Expand Down Expand Up @@ -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)
}
23 changes: 15 additions & 8 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExitStatus>;
fn run_and_get_stdout(&mut self) -> Result<String>;
fn run(&mut self, verbose: bool) -> Result<()>;
fn run_and_get_exit_status(&mut self, verbose: bool) -> Result<ExitStatus>;
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String>;
}

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(())
Expand All @@ -23,13 +23,21 @@ impl CommandExt for Command {
}

/// Runs the command to completion
fn run_and_get_exit_status(&mut self) -> Result<ExitStatus> {
fn run_and_get_exit_status(&mut self, verbose: bool) -> Result<ExitStatus> {
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<String> {
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String> {
if verbose {
println!("+ {:?}", self);
}

let out = self.output()
.chain_err(|| format!("couldn't execute `{:?}`", self))?;

Expand All @@ -43,6 +51,5 @@ impl CommandExt for Command {
self,
out.status.code()))?
}

}
}
2 changes: 1 addition & 1 deletion src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn read<P>(path: P) -> Result<String>
fn read_(path: &Path) -> Result<String> {
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)
}
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ fn run() -> Result<ExitStatus> {
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 {
Expand All @@ -179,20 +182,20 @@ fn run() -> Result<ExitStatus> {
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)
}
4 changes: 2 additions & 2 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub fn host() -> Host {
Host::from(&*rustc_version::version_meta().host)
}

pub fn sysroot() -> Result<PathBuf> {
pub fn sysroot(verbose: bool) -> Result<PathBuf> {
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();
Expand Down
8 changes: 4 additions & 4 deletions src/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Target>> {
pub fn installed_targets(verbose: bool) -> Result<Vec<Target>> {
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") ||
Expand Down

0 comments on commit 1cc25ac

Please sign in to comment.