Skip to content

Commit 1e506dd

Browse files
committed
Show envs and redirects when running in debug mode
1 parent 42e0d0b commit 1e506dd

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

src/process.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{CmdResult, FunResult};
22
use lazy_static::lazy_static;
33
use std::collections::HashMap;
4+
use std::fmt;
45
use std::fs::OpenOptions;
56
use std::io::{Error, ErrorKind, Write};
67
use std::process::{Child, Command, ExitStatus, Stdio};
@@ -196,6 +197,19 @@ impl Cmds {
196197
ret += " | ";
197198
}
198199
ret += &format!("{:?}", cmd_arg.get_args());
200+
let mut extra = String::new();
201+
if !cmd_arg.get_envs().is_empty() {
202+
extra += &format!("envs: {:?}", cmd_arg.get_envs());
203+
}
204+
if !cmd_arg.get_redirects().is_empty() {
205+
if !extra.is_empty() {
206+
extra += ", ";
207+
}
208+
extra += &format!("redirects: {:?}", cmd_arg.get_redirects());
209+
}
210+
if !extra.is_empty() {
211+
ret += &format!(" ({})", extra);
212+
}
199213
}
200214
ret
201215
}
@@ -304,13 +318,13 @@ impl Cmds {
304318
Ok(())
305319
}
306320

307-
pub fn run_cmd(&mut self, current_dir: &mut String) -> CmdResult {
321+
fn run_cmd(&mut self, current_dir: &mut String) -> CmdResult {
308322
let mut handle = self.spawn(current_dir)?;
309323
self.pipes.clear(); // to avoid wait deadlock
310324
handle.wait_result()
311325
}
312326

313-
pub fn run_fun(&mut self, current_dir: &mut String) -> FunResult {
327+
fn run_fun(&mut self, current_dir: &mut String) -> FunResult {
314328
let mut handle = self.spawn_with_output(current_dir)?;
315329
self.pipes.clear(); // to avoid wait deadlock
316330
handle.wait_result()
@@ -339,6 +353,29 @@ pub enum Redirect {
339353
StdoutToFile(String, bool),
340354
StderrToFile(String, bool),
341355
}
356+
impl fmt::Debug for Redirect {
357+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
358+
match self {
359+
Redirect::FileToStdin(path) => f.write_str(&format!("< {}", path)),
360+
Redirect::StdoutToStderr => f.write_str(">&2"),
361+
Redirect::StderrToStdout => f.write_str("2>&1"),
362+
Redirect::StdoutToFile(path, append) => {
363+
if *append {
364+
f.write_str(&format!("1>> {}", path))
365+
} else {
366+
f.write_str(&format!("1> {}", path))
367+
}
368+
}
369+
Redirect::StderrToFile(path, append) => {
370+
if *append {
371+
f.write_str(&format!("2>> {}", path))
372+
} else {
373+
f.write_str(&format!("2> {}", path))
374+
}
375+
}
376+
}
377+
}
378+
}
342379

343380
#[doc(hidden)]
344381
#[derive(Default)]
@@ -368,11 +405,11 @@ impl Cmd {
368405
self
369406
}
370407

371-
pub fn get_args(&self) -> &Vec<String> {
408+
fn get_args(&self) -> &Vec<String> {
372409
&self.args
373410
}
374411

375-
pub fn get_envs(&self) -> &HashMap<String, String> {
412+
fn get_envs(&self) -> &HashMap<String, String> {
376413
&self.envs
377414
}
378415

@@ -381,7 +418,11 @@ impl Cmd {
381418
self
382419
}
383420

384-
pub fn gen_command(&mut self) -> Command {
421+
fn get_redirects(&self) -> &Vec<Redirect> {
422+
&self.redirects
423+
}
424+
425+
fn gen_command(&mut self) -> Command {
385426
let cmd_args: Vec<String> = self.get_args().to_vec();
386427
let mut cmd = Command::new(&cmd_args[0]);
387428
cmd.args(&cmd_args[1..]);

0 commit comments

Comments
 (0)