use rustsec::{Error, ErrorKind};
use std::{
path::{Path, PathBuf},
process::Command,
};
const CARGO_LOCK_FILE: &str = "Cargo.lock";
pub fn locate_or_generate(maybe_lockfile_path: Option<&Path>) -> rustsec::Result<PathBuf> {
match maybe_lockfile_path {
Some(p) => Ok(p.into()),
None => {
let path = Path::new(CARGO_LOCK_FILE);
if !path.exists() && Path::new("Cargo.toml").exists() {
generate()?;
}
Ok(path.into())
}
}
}
pub fn generate() -> rustsec::Result<()> {
let status = Command::new("cargo")
.arg("update")
.arg("--workspace")
.status();
if let Err(e) = status {
return Err(Error::with_source(
ErrorKind::Io,
"couldn't run `cargo update --workspace` to generate a lockfile".to_string(),
e,
));
}
let status = status.unwrap();
if !status.success() {
let msg = match status.code() {
Some(code) => format!(
"non-zero exit status running `cargo update --workspace`: {}",
code
),
_ => "no exit status running `cargo update --workspace`!".to_string(),
};
return Err(Error::new(ErrorKind::Io, &msg));
}
Ok(())
}