|
5 | 5 |
|
6 | 6 | //! Module for interacting with Cargo. |
7 | 7 |
|
| 8 | +mod add_owner; |
| 9 | +mod get_owners; |
| 10 | +mod publish; |
| 11 | +mod yank; |
| 12 | + |
| 13 | +pub use add_owner::AddOwner; |
| 14 | +pub use get_owners::GetOwners; |
| 15 | +pub use publish::Publish; |
| 16 | +pub use yank::Yank; |
| 17 | + |
8 | 18 | use anyhow::{Context, Result}; |
9 | | -use std::path::{Path, PathBuf}; |
| 19 | +use async_trait::async_trait; |
| 20 | +use std::borrow::Cow; |
10 | 21 | use std::process::{Command, Output}; |
11 | 22 |
|
12 | | -macro_rules! cmd { |
13 | | - [ $( $x:expr ),* ] => { |
14 | | - { |
15 | | - let mut cmd = Cmd::new(); |
16 | | - $(cmd.push($x);)* |
17 | | - cmd |
18 | | - } |
19 | | - }; |
20 | | -} |
| 23 | +#[async_trait] |
| 24 | +pub trait CargoOperation { |
| 25 | + type Output; |
21 | 26 |
|
22 | | -/// Confirms that cargo exists on the path. |
23 | | -pub async fn confirm_installed_on_path() -> Result<()> { |
24 | | - cmd!["cargo", "--version"] |
25 | | - .spawn() |
26 | | - .await |
27 | | - .context("cargo is not installed on the PATH")?; |
28 | | - Ok(()) |
29 | | -} |
| 27 | + /// Runs the command asynchronously. |
| 28 | + async fn spawn(&self) -> Result<Self::Output>; |
30 | 29 |
|
31 | | -/// Returns a `Cmd` that, when spawned, will asynchronously run `cargo publish` in the given crate path. |
32 | | -pub fn publish_task(crate_path: &Path) -> Cmd { |
33 | | - cmd!["cargo", "publish"].working_dir(crate_path) |
| 30 | + /// Returns a plan string that can be output to the user to describe the command. |
| 31 | + fn plan(&self) -> Option<Cow<'static, str>>; |
34 | 32 | } |
35 | 33 |
|
36 | | -#[derive(Default)] |
37 | | -pub struct Cmd { |
38 | | - parts: Vec<String>, |
39 | | - working_dir: Option<PathBuf>, |
| 34 | +/// Confirms that cargo exists on the path. |
| 35 | +pub fn confirm_installed_on_path() -> Result<()> { |
| 36 | + handle_failure( |
| 37 | + "discover cargo version", |
| 38 | + &Command::new("cargo") |
| 39 | + .arg("version") |
| 40 | + .output() |
| 41 | + .context("cargo is not installed on the PATH")?, |
| 42 | + ) |
| 43 | + .context("cargo is not installed on the PATH") |
40 | 44 | } |
41 | 45 |
|
42 | | -impl Cmd { |
43 | | - fn new() -> Cmd { |
44 | | - Default::default() |
45 | | - } |
46 | | - |
47 | | - fn push(&mut self, part: impl Into<String>) { |
48 | | - self.parts.push(part.into()); |
49 | | - } |
50 | | - |
51 | | - fn working_dir(mut self, working_dir: impl AsRef<Path>) -> Self { |
52 | | - self.working_dir = Some(working_dir.as_ref().into()); |
53 | | - self |
54 | | - } |
55 | | - |
56 | | - /// Returns a plan string that can be output to the user to describe the command. |
57 | | - pub fn plan(&self) -> String { |
58 | | - let mut plan = String::new(); |
59 | | - if let Some(working_dir) = &self.working_dir { |
60 | | - plan.push_str(&format!("[in {:?}]: ", working_dir)); |
61 | | - } |
62 | | - plan.push_str(&self.parts.join(" ")); |
63 | | - plan |
64 | | - } |
| 46 | +/// Returns (stdout, stderr) |
| 47 | +fn output_text(output: &Output) -> (String, String) { |
| 48 | + ( |
| 49 | + String::from_utf8_lossy(&output.stdout).to_string(), |
| 50 | + String::from_utf8_lossy(&output.stderr).to_string(), |
| 51 | + ) |
| 52 | +} |
65 | 53 |
|
66 | | - /// Runs the command asynchronously. |
67 | | - pub async fn spawn(mut self) -> Result<Output> { |
68 | | - let working_dir = self |
69 | | - .working_dir |
70 | | - .take() |
71 | | - .unwrap_or_else(|| std::env::current_dir().unwrap()); |
72 | | - let mut command: Command = self.into(); |
73 | | - tokio::task::spawn_blocking(move || Ok(command.current_dir(working_dir).output()?)).await? |
| 54 | +fn handle_failure(operation_name: &str, output: &Output) -> Result<(), anyhow::Error> { |
| 55 | + if !output.status.success() { |
| 56 | + return Err(capture_error(operation_name, output)); |
74 | 57 | } |
| 58 | + Ok(()) |
75 | 59 | } |
76 | 60 |
|
77 | | -impl From<Cmd> for Command { |
78 | | - fn from(cmd: Cmd) -> Self { |
79 | | - assert!(!cmd.parts.is_empty()); |
80 | | - let mut command = Command::new(&cmd.parts[0]); |
81 | | - for i in 1..cmd.parts.len() { |
82 | | - command.arg(&cmd.parts[i]); |
83 | | - } |
84 | | - command |
85 | | - } |
| 61 | +fn capture_error(operation_name: &str, output: &Output) -> anyhow::Error { |
| 62 | + let message = format!( |
| 63 | + "Failed to {name}:\nStatus: {status}\nStdout: {stdout}\nStderr: {stderr}\n", |
| 64 | + name = operation_name, |
| 65 | + status = if let Some(code) = output.status.code() { |
| 66 | + format!("{}", code) |
| 67 | + } else { |
| 68 | + "Killed by signal".to_string() |
| 69 | + }, |
| 70 | + stdout = String::from_utf8_lossy(&output.stdout), |
| 71 | + stderr = String::from_utf8_lossy(&output.stderr) |
| 72 | + ); |
| 73 | + anyhow::Error::msg(message) |
86 | 74 | } |
0 commit comments