Skip to content

Commit

Permalink
Simplifying get_container_engine() call
Browse files Browse the repository at this point in the history
- Changing the get_container_engine() call to use Result<> instead
  of Option<> and using combinators rather than ifs.
- Added which::Error to the list of errors in the error_chain. This
  also required disabling the default features of which which uses
  failure by default rather than std::error::Error.
  • Loading branch information
ostrosco committed Nov 15, 2019
1 parent 91223e4 commit 243e21a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 22 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ libc = "0.2.18"
rustc_version = "0.2"
semver = "0.9"
toml = "0.5"
which = "3.1.0"
which = { version = "3.1.0", default_features = false }

[target.'cfg(not(windows))'.dependencies]
nix = "0.15"
Expand Down
18 changes: 7 additions & 11 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-image
const DOCKER: &str = "docker";
const PODMAN: &str = "podman";

fn get_container_engine() -> Option<&'static str> {
if which::which(DOCKER).is_ok() {
Some(DOCKER)
} else if which::which(PODMAN).is_ok() {
Some(PODMAN)
} else {
None
}
fn get_container_engine() -> Result<std::path::PathBuf> {
which::which(DOCKER).or_else(|_| which::which(PODMAN)).map_err(|e| e.into())
}

pub fn docker_command(subcommand: &str) -> Result<Command> {
if let Some(ce) = get_container_engine() {
if let Ok(ce) = get_container_engine() {
let mut command = Command::new(ce);
command.arg(subcommand);
command.args(&["--userns", "host"]);
Expand Down Expand Up @@ -119,8 +113,10 @@ pub fn run(target: &Target,
docker.arg("--rm");

// We need to specify the user for Docker, but not for Podman.
if let Some(DOCKER) = get_container_engine() {
docker.args(&["--user", &format!("{}:{}", id::user(), id::group())]);
if let Ok(ce) = get_container_engine() {
if ce.ends_with(DOCKER) {
docker.args(&["--user", &format!("{}:{}", id::user(), id::group())]);
}
}

docker
Expand Down
1 change: 1 addition & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ use error_chain::error_chain;
error_chain! {
foreign_links {
Io(std::io::Error);
Which(which::Error);
}
}

0 comments on commit 243e21a

Please sign in to comment.