Skip to content

Commit 51004e1

Browse files
committed
address copilot feedback
1 parent 74f9638 commit 51004e1

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

dsc/src/main.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use rust_i18n::{i18n, t};
1010
use std::io;
1111
use sysinfo::{Process, RefreshKind, System, get_current_pid, ProcessRefreshKind};
1212
use tracing::{error, info, warn, debug};
13-
use util::exit;
1413

15-
use crate::util::{EXIT_INVALID_INPUT, get_input};
14+
use crate::util::{enable_tracing, exit, EXIT_INVALID_INPUT, EXIT_INVALID_ARGS, get_input, get_schema, merge_parameters, write_object};
1615

1716
#[cfg(debug_assertions)]
1817
use crossterm::event;
@@ -42,7 +41,7 @@ fn main() {
4241

4342
let args = Args::parse();
4443

45-
util::enable_tracing(args.trace_level.as_ref(), args.trace_format.as_ref());
44+
enable_tracing(args.trace_level.as_ref(), args.trace_format.as_ref());
4645

4746
debug!("{}: {}", t!("main.usingDscVersion"), env!("CARGO_PKG_VERSION"));
4847

@@ -65,7 +64,7 @@ fn main() {
6564
let merged_parameters = match (file_params, parameters) {
6665
(Some(file_content), Some(inline_content)) => {
6766
info!("{}", t!("main.mergingParameters"));
68-
match util::merge_parameters(&file_content, &inline_content) {
67+
match merge_parameters(&file_content, &inline_content) {
6968
Ok(merged) => Some(merged),
7069
Err(err) => {
7170
error!("{}: {err}", t!("main.failedMergingParameters"));
@@ -89,27 +88,27 @@ fn main() {
8988
SubCommand::Mcp => {
9089
if let Err(err) = start_mcp_server() {
9190
error!("{}", t!("main.failedToStartMcpServer", error = err));
92-
exit(util::EXIT_MCP_FAILED);
91+
exit(EXIT_MCP_FAILED);
9392
}
94-
exit(util::EXIT_SUCCESS);
93+
exit(EXIT_SUCCESS);
9594
}
9695
SubCommand::Resource { subcommand } => {
9796
subcommand::resource(&subcommand, progress_format);
9897
},
9998
SubCommand::Schema { dsc_type , output_format } => {
100-
let schema = util::get_schema(dsc_type);
99+
let schema = get_schema(dsc_type);
101100
let json = match serde_json::to_string(&schema) {
102101
Ok(json) => json,
103102
Err(err) => {
104103
error!("JSON: {err}");
105-
exit(util::EXIT_JSON_ERROR);
104+
exit(EXIT_JSON_ERROR);
106105
}
107106
};
108-
util::write_object(&json, output_format.as_ref(), false);
107+
write_object(&json, output_format.as_ref(), false);
109108
},
110109
}
111110

112-
exit(util::EXIT_SUCCESS);
111+
exit(EXIT_SUCCESS);
113112
}
114113

115114
fn ctrlc_handler() {
@@ -120,16 +119,16 @@ fn ctrlc_handler() {
120119
info!("{}: {}", t!("main.foundProcesses"), sys.processes().len());
121120
let Ok(current_pid) = get_current_pid() else {
122121
error!("{}", t!("main.failedToGetPid"));
123-
exit(util::EXIT_CTRL_C);
122+
exit(EXIT_CTRL_C);
124123
};
125124
info!("{}: {}", t!("main.currentPid"), current_pid);
126125
let Some(current_process) = sys.process(current_pid) else {
127126
error!("{}", t!("main.failedToGetProcess"));
128-
exit(util::EXIT_CTRL_C);
127+
exit(EXIT_CTRL_C);
129128
};
130129

131130
terminate_subprocesses(&sys, current_process);
132-
exit(util::EXIT_CTRL_C);
131+
exit(EXIT_CTRL_C);
133132
}
134133

135134
fn terminate_subprocesses(sys: &System, process: &Process) {
@@ -193,6 +192,6 @@ fn check_store() {
193192
eprintln!("{}", t!("main.storeMessage"));
194193
// wait for keypress
195194
let _ = io::stdin().read(&mut [0u8]).unwrap();
196-
exit(util::EXIT_INVALID_ARGS);
195+
exit(EXIT_INVALID_ARGS);
197196
}
198197
}

dsc/src/util.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,17 +690,28 @@ pub fn merge_parameters(file_params: &str, inline_params: &str) -> Result<String
690690
Ok(serde_json::to_string(&merged)?)
691691
}
692692

693-
/// Exit the process with the given code after flushing and shutting down tracing.
693+
/// Exit the process with the given code after flushing outputs
694+
///
695+
/// # Arguments
696+
///
697+
/// * `code` - The exit code to use when exiting the process
694698
pub fn exit(code: i32) -> ! {
699+
// Small delay to ensure async writes complete
700+
std::thread::sleep(std::time::Duration::from_millis(50));
701+
695702
// Force any pending writes to complete
696703
if let Err(e) = std::io::stderr().flush() {
697-
eprintln!("Failed to flush stderr: {}", e);
704+
// Ignore BrokenPipe on stderr to avoid noisy exits in piped scenarios
705+
if e.kind() != std::io::ErrorKind::BrokenPipe {
706+
eprintln!("Failed to flush stderr: {}", e);
707+
}
698708
}
699709
if let Err(e) = std::io::stdout().flush() {
700-
eprintln!("Failed to flush stdout: {}", e);
710+
// Ignore BrokenPipe on stdout to avoid noisy exits in piped scenarios
711+
if e.kind() != std::io::ErrorKind::BrokenPipe {
712+
eprintln!("Failed to flush stdout: {}", e);
713+
}
701714
}
702715

703-
// Small delay to ensure async writes complete
704-
std::thread::sleep(std::time::Duration::from_millis(50));
705716
std::process::exit(code);
706717
}

0 commit comments

Comments
 (0)