Skip to content

Commit 58878f3

Browse files
fix(rust): propagate global options to commands run in the node configuration
1 parent 3691234 commit 58878f3

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

implementations/rust/ockam/ockam_command/src/global_args.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::Args;
22
use clap::{ArgAction, ValueEnum};
33
use ockam_api::output::OutputFormat;
4+
use std::fmt::Display;
45

56
use ockam_core::env::get_env_with_default;
67

@@ -123,3 +124,12 @@ pub enum OutputFormatArg {
123124
Plain,
124125
Json,
125126
}
127+
128+
impl Display for OutputFormatArg {
129+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130+
match self {
131+
OutputFormatArg::Plain => write!(f, "plain"),
132+
OutputFormatArg::Json => write!(f, "json"),
133+
}
134+
}
135+
}

implementations/rust/ockam/ockam_command/src/node/create/config.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use miette::{miette, IntoDiagnostic};
1111
use ockam_api::cli_state::journeys::APPLICATION_EVENT_COMMAND_CONFIGURATION_FILE;
1212
use ockam_api::cli_state::random_name;
1313
use ockam_api::nodes::BackgroundNodeClient;
14-
use ockam_core::AsyncTryClone;
14+
use ockam_core::{AsyncTryClone, OpenTelemetryContext};
1515
use ockam_node::Context;
1616
use serde::{Deserialize, Serialize};
1717
use tracing::{debug, instrument, Span};
@@ -166,6 +166,12 @@ impl NodeConfig {
166166
.clone()
167167
.map(ArgValue::String);
168168
}
169+
if self.node.opentelemetry_context.is_none() {
170+
self.node.opentelemetry_context = Some(ArgValue::String(
171+
serde_json::to_string(&OpenTelemetryContext::current()).into_diagnostic()?,
172+
));
173+
}
174+
169175
Ok(())
170176
}
171177

@@ -189,7 +195,7 @@ impl NodeConfig {
189195
if self.project_enroll.ticket.is_some()
190196
&& !self
191197
.project_enroll
192-
.run_in_subprocess(opts.global_args.quiet)?
198+
.run_in_subprocess(&opts.global_args)?
193199
.wait()
194200
.await
195201
.into_diagnostic()?
@@ -199,7 +205,7 @@ impl NodeConfig {
199205
}
200206

201207
// Next, run the 'node create' command
202-
let child = self.node.run_in_subprocess(opts.global_args.quiet)?;
208+
let child = self.node.run_in_subprocess(&opts.global_args)?;
203209

204210
// Wait for the node to be up
205211
let is_up = {

implementations/rust/ockam/ockam_command/src/run/parser/resource/node.rs

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct Node {
2929
pub http_server_port: Option<ArgValue>,
3030
pub identity: Option<ArgValue>,
3131
pub project: Option<ArgValue>,
32+
#[serde(alias = "opentelemetry-context")]
33+
pub opentelemetry_context: Option<ArgValue>,
3234
}
3335

3436
impl Resource<CreateCommand> for Node {
@@ -66,6 +68,9 @@ impl Resource<CreateCommand> for Node {
6668
if let Some(project) = self.project {
6769
args.insert("project".to_string(), project);
6870
}
71+
if let Some(opentelemetry_context) = self.opentelemetry_context {
72+
args.insert("opentelemetry-context".to_string(), opentelemetry_context);
73+
}
6974
if args.is_empty() {
7075
return vec![];
7176
}

implementations/rust/ockam/ockam_command/src/run/parser/resource/traits.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use std::process::Stdio;
22

33
use async_trait::async_trait;
44
use miette::{IntoDiagnostic, Result};
5-
use tokio::process::Child;
6-
use tracing::debug;
7-
85
use ockam_core::AsyncTryClone;
96
use ockam_node::Context;
7+
use tokio::process::{Child, Command as ProcessCommand};
8+
use tracing::debug;
109

1110
use crate::run::parser::resource::utils::{binary_path, subprocess_stdio};
12-
use crate::{Command, CommandGlobalOpts};
11+
use crate::{Command, CommandGlobalOpts, GlobalArgs};
1312

1413
/// This trait defines the methods that a resource must implement before it's parsed into a Command.
1514
///
@@ -21,15 +20,31 @@ pub trait Resource<C: ParsedCommand>: Sized + Send + Sync + 'static {
2120
vec![]
2221
}
2322

24-
fn run_in_subprocess(self, quiet: bool) -> Result<Child> {
25-
let args = self.args();
23+
fn run_in_subprocess(self, global_args: &GlobalArgs) -> Result<Child> {
24+
let mut args = self.args();
25+
if global_args.quiet {
26+
args.push("--quiet".to_string());
27+
}
28+
if global_args.no_color {
29+
args.push("--no-color".to_string());
30+
}
31+
if global_args.no_input {
32+
args.push("--no-input".to_string());
33+
}
34+
if global_args.verbose > 0 {
35+
args.push(format!("-{}", "v".repeat(global_args.verbose as usize)));
36+
}
37+
if let Some(o) = &global_args.output_format {
38+
args.push("--output".to_string());
39+
args.push(o.to_string());
40+
}
2641
let args = Self::COMMAND_NAME
2742
.split(' ')
2843
.chain(args.iter().map(|s| s.as_str()));
29-
let handle = tokio::process::Command::new(binary_path())
44+
let handle = ProcessCommand::new(binary_path())
3045
.args(args)
31-
.stdout(subprocess_stdio(quiet))
32-
.stderr(subprocess_stdio(quiet))
46+
.stdout(subprocess_stdio(global_args.quiet))
47+
.stderr(subprocess_stdio(global_args.quiet))
3348
.stdin(Stdio::null())
3449
.spawn()
3550
.into_diagnostic()?;

0 commit comments

Comments
 (0)