Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/core/src/commands/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ pub async fn push(
]);

let child = run_command_with_env(
&CommandArguments::new(command_string, context.modifiers)
.mode(crate::commands::ChildOutputMode::Nix),
&CommandArguments::new(command_string, context.modifiers).mode(
crate::commands::ChildOutputMode::Nix(Some(context.name.clone())),
),
HashMap::from([(
"NIX_SSHOPTS".into(),
target.create_ssh_opts(context.modifiers)?,
Expand Down Expand Up @@ -152,7 +153,7 @@ pub async fn evaluate_hive_attribute(

let child = run_command(
&CommandArguments::new(command_string, modifiers)
.mode(crate::commands::ChildOutputMode::Nix),
.mode(crate::commands::ChildOutputMode::Nix(None)),
)
.await?;

Expand Down
59 changes: 49 additions & 10 deletions crates/core/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use crate::{
commands::pty::{InteractiveChildChip, interactive_command_with_env},
hive::node::SharedTarget,
hive::node::{Name, SharedTarget},
status::UI_SENDER,
};
use std::{
collections::HashMap,
Expand All @@ -26,9 +27,9 @@ pub mod common;
pub(crate) mod noninteractive;
pub(crate) mod pty;

#[derive(Copy, Clone, Debug)]
#[derive(Clone, Debug)]
pub(crate) enum ChildOutputMode {
Nix,
Nix(Option<Name>),
Generic,
Interactive,
}
Expand Down Expand Up @@ -76,7 +77,7 @@ impl<S: AsRef<str>> CommandArguments<S> {
self
}

pub(crate) const fn mode(mut self, mode: ChildOutputMode) -> Self {
pub(crate) fn mode(mut self, mode: ChildOutputMode) -> Self {
self.output_mode = mode;
self
}
Expand Down Expand Up @@ -156,19 +157,19 @@ impl WireCommandChip for Either<InteractiveChildChip, NonInteractiveChildChip> {
impl ChildOutputMode {
/// this function is by far the biggest hotspot in the whole tree
/// Returns a string if this log is notable to be stored as an error message
fn trace_slice(self, line: &mut [u8]) -> Option<String> {
let slice = match self {
fn trace_slice(&self, line: &mut [u8]) -> Option<String> {
let (slice, task_name) = match self {
Self::Generic | Self::Interactive => {
let string = String::from_utf8_lossy(line);
let stripped = strip_ansi_escapes::strip_str(&string);
warn!("{stripped}");
return Some(string.to_string());
}
Self::Nix => {
Self::Nix(task_name) => {
let position = AHO_CORASICK.find(&line).map(|x| &mut line[x.end()..]);

if let Some(json_buf) = position {
json_buf
(json_buf, task_name)
} else {
// usually happens when ssh is outputting something
warn!("{}", String::from_utf8_lossy(line));
Expand All @@ -184,9 +185,47 @@ impl ChildOutputMode {
};

let (msg, level) = match log_message {
LogMessage::Start { text, level, .. } => (text, level),
LogMessage::Start {
text,
level,
id,
r#type,
..
} => {
if let Some(tx) = UI_SENDER.get() {
let _ = tx.send(crate::status::UiMessage::ActivityBegin(
task_name.clone(),
id,
r#type,
));
}

(text, level)
}
LogMessage::Stop { id } => {
if let Some(tx) = UI_SENDER.get() {
let _ = tx.send(crate::status::UiMessage::ActivityEnd(
task_name.clone(),
id,
None,
));
}

return None;
}
LogMessage::Result { id, r#type, .. } => {
if let Some(tx) = UI_SENDER.get() {
let _ = tx.send(crate::status::UiMessage::ActivityEnd(
task_name.clone(),
id,
Some(r#type),
));
}

return None;
}
LogMessage::Msg { msg, level, .. } => (msg, level),
_ => return None,
LogMessage::SetPhase { .. } => return None,
};

if msg.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/commands/noninteractive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) async fn non_interactive_command_with_env<S: AsRef<str>>(
command_string = arguments.command_string.as_ref(),
extra = match arguments.output_mode {
ChildOutputMode::Generic | ChildOutputMode::Interactive => "",
ChildOutputMode::Nix => " --log-format internal-json",
ChildOutputMode::Nix(..) => " --log-format internal-json",
}
);

Expand Down Expand Up @@ -86,7 +86,7 @@ pub(crate) async fn non_interactive_command_with_env<S: AsRef<str>>(
.ok_or(HiveLibError::CommandError(CommandError::NoHandle))?;

let mut joinset = JoinSet::new();
let output_mode = Arc::new(arguments.output_mode);
let output_mode = Arc::new(arguments.output_mode.clone());

joinset.spawn(
handle_io(
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/commands/pty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub(crate) async fn interactive_command_with_env<S: AsRef<str>>(
"{starting}{command} {flags} {IO_SUBS} && {ending}",
command = arguments.command_string.as_ref(),
flags = match arguments.output_mode {
ChildOutputMode::Nix => "--log-format internal-json",
ChildOutputMode::Nix(..) => "--log-format internal-json",
ChildOutputMode::Generic | ChildOutputMode::Interactive => "",
},
starting = create_starting_segment(arguments, &needles.start),
Expand Down Expand Up @@ -184,7 +184,7 @@ pub(crate) async fn interactive_command_with_env<S: AsRef<str>>(
began_tx,
reader,
needles,
output_mode: arguments.output_mode,
output_mode: arguments.output_mode.clone(),
stderr_collection: stderr_collection.clone(),
stdout_collection: stdout_collection.clone(),
span: Span::current(),
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/commands/pty/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub(super) fn handle_pty_stdout(arguments: WatchStdoutArguments) -> Result<(), C
&stdout_collection,
&mut line,
log_stdout,
output_mode,
&output_mode,
);
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ fn handle_normal_data(
stdout_collection: &Arc<Mutex<VecDeque<String>>>,
line: &mut [u8],
log_stdout: bool,
output_mode: ChildOutputMode,
output_mode: &ChildOutputMode,
) {
if line.starts_with(b"#") {
let stripped = &mut line[1..];
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/hive/steps/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ impl SwitchToConfiguration {

let child = run_command(
&CommandArguments::new(command_string, ctx.modifiers)
.mode(crate::commands::ChildOutputMode::Nix)
.mode(crate::commands::ChildOutputMode::Nix(Some(
ctx.name.clone(),
)))
.execute_on_remote(self.target.clone())
.privileged(&self.privilege_escalation_command),
)
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/hive/steps/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ impl ExecuteStep for Build {
&CommandArguments::new(command_string, ctx.modifiers)
// build remotely if asked for AND we arent applying locally
.execute_on_remote(self.target.clone())
.mode(crate::commands::ChildOutputMode::Nix)
.mode(crate::commands::ChildOutputMode::Nix(Some(
ctx.name.clone(),
)))
.log_stdout(),
std::collections::HashMap::new(),
)
Expand Down
Loading
Loading