Skip to content

Commit 243e21a

Browse files
committed
Simplifying get_container_engine() call
- 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.
1 parent 91223e4 commit 243e21a

File tree

4 files changed

+9
-22
lines changed

4 files changed

+9
-22
lines changed

Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ libc = "0.2.18"
1919
rustc_version = "0.2"
2020
semver = "0.9"
2121
toml = "0.5"
22-
which = "3.1.0"
22+
which = { version = "3.1.0", default_features = false }
2323

2424
[target.'cfg(not(windows))'.dependencies]
2525
nix = "0.15"

src/docker.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@ const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-image
1515
const DOCKER: &str = "docker";
1616
const PODMAN: &str = "podman";
1717

18-
fn get_container_engine() -> Option<&'static str> {
19-
if which::which(DOCKER).is_ok() {
20-
Some(DOCKER)
21-
} else if which::which(PODMAN).is_ok() {
22-
Some(PODMAN)
23-
} else {
24-
None
25-
}
18+
fn get_container_engine() -> Result<std::path::PathBuf> {
19+
which::which(DOCKER).or_else(|_| which::which(PODMAN)).map_err(|e| e.into())
2620
}
2721

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

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

126122
docker

src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ use error_chain::error_chain;
55
error_chain! {
66
foreign_links {
77
Io(std::io::Error);
8+
Which(which::Error);
89
}
910
}

0 commit comments

Comments
 (0)