cmd_utils/
lib.rs

1//! # cmd-utils crate
2//! Safe Rust `Command` utility traits
3//! - run command (`spawn` & `wait` wrapper)
4//! - pipe commands
5//! - output to file (`spawn` & `wait` & outuput to file wrapper)
6//!
7//! ## CmdRun trait
8//! ```rust
9//! use std::process::Command;
10//! use cmd_utils::CmdRun;
11//!
12//! // `spawn` the command and `wait` for child process to end
13//! // note that `run` does not return the command output, just Ok(())
14//! // but you can redirect stdout & stderr where you want
15//! Command::new("test")
16//!     // .stdout(cfg)
17//!     // .stderr(cfg)
18//!     .args(["-n", "a"])
19//!     .run()
20//!     .unwrap();
21//! ```
22//!
23//! ## CmdPipe trait
24//! ```rust
25//! use std::process::Command;
26//! use std::str;
27//! use cmd_utils::CmdPipe;
28//!
29//! // pipe echo & wc commands
30//! // equivalent to bash: echo test | wc -c
31//! let mut echo = Command::new("echo");
32//! let mut wc = Command::new("wc");
33//! let output = echo.arg("test")
34//!     .pipe(&mut wc.arg("-c"))
35//!     .unwrap();
36//! let res = str::from_utf8(&output.stdout).unwrap();
37//! println!("pipe result: {}", &res);
38//! ```
39//!
40//! ## CmdToFile trait
41//! ```rust
42//! use std::fs::File;
43//! use std::process::Command;
44//! use cmd_utils::CmdToFile;
45//!
46//! let stdout = File::create("tmp/my_file.stdout").unwrap();
47//! let stderr = File::create("tmp/my_file.stderr").unwrap();
48//! let mut cmd = Command::new("echo");
49//! // writes stdout to file
50//! cmd.arg("test").to_file(stdout, Some(stderr));
51//! ```
52//!
53
54#![forbid(unsafe_code)]
55#![allow(clippy::needless_return)]
56
57mod cmd_pipe;
58mod cmd_spawn;
59mod cmd_to_file;
60
61pub use cmd_pipe::{CmdPipe, command_pipe, command_pipe_to_file, command_pipe_base};
62pub use cmd_spawn::{ChildError, CmdSpawnError, command_spawn, CmdRun};
63pub use cmd_to_file::{CmdToFile, command_to_file};