1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Running `cargo install` for `shep install`, on a terminal or inside a
//! session.
//!
//! On a terminal cargo narrates on the operator's stderr. With a sink set
//! its stderr is read to the end instead, so it cannot draw over lookout, and
//! a failure carries the tail of it.
use std::path::Path;
use std::process::{ExitStatus, Stdio};
use super::session::Progress;
/// The most bytes of cargo's stderr a failure message carries: the same
/// four kibibytes lookout's output modal shows.
const TAIL_CAP: usize = 4096;
/// How a `cargo install` ended.
pub(super) struct Ran {
/// The exit status.
pub(super) status: ExitStatus,
/// The tail of cargo's stderr, empty when it went to the terminal.
pub(super) tail: String,
}
/// Runs `cargo` with `args`, stdin closed and stdout dropped: stdout is the
/// verb's answer, and cargo narrates on stderr.
///
/// # Errors
/// `cargo` could not be started or waited on.
pub(super) async fn run(
cargo: &Path,
args: &[String],
progress: Option<&Progress>,
) -> std::io::Result<Ran> {
let mut command = tokio::process::Command::new(cargo);
command
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.kill_on_drop(true);
if progress.is_none() {
let status = command.status().await?;
return Ok(Ran {
status,
tail: String::new(),
});
}
let output = command.stderr(Stdio::piped()).output().await?;
Ok(Ran {
status: output.status,
tail: tail_of(&output.stderr),
})
}
/// `message` followed by what cargo said, when it said anything.
pub(super) fn with_tail(message: String, tail: &str) -> String {
if tail.is_empty() {
message
} else {
format!("{message}\ncargo said:\n{tail}")
}
}
/// The last [`TAIL_CAP`] bytes of `stderr` on a character boundary, with a
/// leading `...` when something was cut.
fn tail_of(stderr: &[u8]) -> String {
let text = String::from_utf8_lossy(stderr);
let text = text.trim_end();
if text.len() <= TAIL_CAP {
return text.to_owned();
}
let mut start = text.len() - TAIL_CAP;
while !text.is_char_boundary(start) {
start += 1;
}
format!("...{}", &text[start..])
}