From 7225929f3404c988bca99f233305ce4d1a86254e Mon Sep 17 00:00:00 2001 From: Adrian Benavides Date: Thu, 2 Jan 2025 17:13:50 +0100 Subject: [PATCH] fix: newlines between commands run in a configuration --- .../ockam_command/src/node/create/config.rs | 33 +++++++++++++++---- .../src/run/parser/resource/traits.rs | 10 +++--- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/implementations/rust/ockam/ockam_command/src/node/create/config.rs b/implementations/rust/ockam/ockam_command/src/node/create/config.rs index 8fa550f91d1..fcbc428e4d4 100644 --- a/implementations/rust/ockam/ockam_command/src/node/create/config.rs +++ b/implementations/rust/ockam/ockam_command/src/node/create/config.rs @@ -281,9 +281,9 @@ impl NodeConfig { self.kafka_outlet.into_parsed_commands(node_name)?.into(), self.kafka_inlet.into_parsed_commands(node_name)?.into(), ]; - for section in other_sections { - section.run(ctx, opts).await?; - } + opts.terminal.write_line("")?; + Self::run_commands_sections(ctx, opts, other_sections).await?; + opts.terminal.write_line("")?; // Block on the node until it exits let _ = node_handle.await.into_diagnostic()?; @@ -297,9 +297,8 @@ impl NodeConfig { node_name: &String, identity_name: &String, ) -> miette::Result<()> { - for section in self.parse_commands(node_name, identity_name)? { - section.run(ctx, opts).await? - } + let sections = self.parse_commands(node_name, identity_name)?; + Self::run_commands_sections(ctx, opts, sections).await?; Ok(()) } @@ -328,6 +327,28 @@ impl NodeConfig { self.kafka_inlet.into_parsed_commands(node_name)?.into(), ]) } + + async fn run_commands_sections( + ctx: &Context, + opts: &CommandGlobalOpts, + sections: Vec, + ) -> miette::Result<()> { + let sections: Vec = sections + .into_iter() + .filter(|s| !s.commands.is_empty()) + .collect(); + let len = sections.len(); + for (idx, section) in sections.into_iter().enumerate() { + if section.commands.is_empty() { + continue; + } + section.run(ctx, opts).await?; + if idx < len - 1 { + opts.terminal.write_line("")?; + } + } + Ok(()) + } } #[cfg(test)] diff --git a/implementations/rust/ockam/ockam_command/src/run/parser/resource/traits.rs b/implementations/rust/ockam/ockam_command/src/run/parser/resource/traits.rs index 564d6e12acc..56572024c5b 100644 --- a/implementations/rust/ockam/ockam_command/src/run/parser/resource/traits.rs +++ b/implementations/rust/ockam/ockam_command/src/run/parser/resource/traits.rs @@ -68,14 +68,12 @@ impl ParsedCommands { /// Validate and run each command pub async fn run(self, ctx: &Context, opts: &CommandGlobalOpts) -> Result<()> { let len = self.commands.len(); - if len > 0 { - opts.terminal.write_line("")?; - } - for cmd in self.commands.into_iter() { + for (idx, cmd) in self.commands.into_iter().enumerate() { if cmd.is_valid(ctx, opts).await? { cmd.run(ctx, opts).await?; - // Newline after each command - opts.terminal.write_line("")?; + if idx < len - 1 { + opts.terminal.write_line("")?; + } } } Ok(())