Skip to content

Commit

Permalink
Refactor(cargo/finish_search_exact): Bail as soon as search process d…
Browse files Browse the repository at this point in the history
…eclares failure exit status

More explicit and clearer logging. Would otherwise fail on the following
check, which has a bit of an opaque and generic failure message.

Signed-off-by: Paul Mabileau <paulmabileau@hotmail.fr>
  • Loading branch information
PaulDance committed Feb 19, 2024
1 parent e397447 commit 4516dc6
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,23 @@ fn spawn_search_exact(pkg: &str) -> Result<Child> {
/// Waits for the given child process as spawned by [`spawn_search_exact`] to
/// finish and extract the received package version from the output.
fn finish_search_exact(pkg: &str, proc: Child) -> Result<Version> {
let out = String::from_utf8(proc.wait_with_output()?.stdout)?;
trace!("Search for {:#?} got: {:#?}", pkg, out);
let out = proc.wait_with_output()?;

if !out.status.success() {
anyhow::bail!(
"Search for {:?} failed on {:?} with stderr: {:?}",
pkg,
out.status.code(),
String::from_utf8(out.stderr),
);
}

let stdout = String::from_utf8(out.stdout)?;
trace!("Search for {:?} got: {:?}", pkg, stdout);

// See https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions.
let ver = Regex::new(&format!(r#"{pkg}\s=\s"([0-9a-zA-Z.+-]+)"\s+#.*"#))?
.captures(out.lines().next().ok_or_else(|| {
.captures(stdout.lines().next().ok_or_else(|| {
anyhow!("Not at least one line in search output for {pkg:#?}: does the package exist?")
})?)
.ok_or_else(|| {
Expand Down

0 comments on commit 4516dc6

Please sign in to comment.