Skip to content

Commit 980ca71

Browse files
committed
update docs
1 parent b100486 commit 980ca71

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ eprintln!("There are {} words in above sentence", n);
115115
```
116116

117117
#### Abstraction without overhead
118+
118119
Since all the macros' lexical analysis and syntactic analysis happen at compile time, it can
119120
basically generate code the same as calling `std::process` APIs manually. It also includes
120121
command type checking, so most of the errors can be found at compile time instead of at
121-
runtime.
122+
runtime. With tools like `rust-analyzer`, it can give you real-time feedback for broken
123+
commands being used.
124+
125+
You can use `cargo expand` to check the generated code.
122126

123127
#### Intuitive parameters passing
124128
When passing parameters to `run_cmd!` and `run_fun!` macros, if they are not part to rust
@@ -160,6 +164,23 @@ for opts in gopts {
160164
Right now piping and stdin, stdout, stderr redirection are supported. Most parts are the same as in
161165
[bash scripts](https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Redirections).
162166

167+
#### logging
168+
169+
This library provides convenient macros and builtin commands for logging. It also automatically logs
170+
command execution failures, along with messages which were printed to stderr originally.
171+
172+
```rust
173+
let dir: &str = "folder with spaces";
174+
assert!(run_cmd!(mkdir /tmp/$dir; ls /tmp/$dir).is_ok());
175+
assert!(run_cmd!(mkdir /tmp/"$dir"; ls /tmp/"$dir"; rmdir /tmp/"$dir").is_err());
176+
// output:
177+
// INFO - mkdir: cannot create directory ‘/tmp/folder with spaces’: File exists
178+
// ERROR - Running ["mkdir", "/tmp/folder with spaces"] failed, Error: ["mkdir", "/tmp/folder with spaces"] exited with error; status code: 1
179+
```
180+
181+
It is using rust [log crate](https://crates.io/crates/log), and you can use your actual favorite logging
182+
implementation.
183+
163184
#### Builtin commands
164185
##### cd
165186
cd: set process current directory, which is always enabled

src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,14 @@
126126
//! ```
127127
//!
128128
//! ### Abstraction without overhead
129+
//!
129130
//! Since all the macros' lexical analysis and syntactic analysis happen at compile time, it can
130131
//! basically generate code the same as calling `std::process` APIs manually. It also includes
131132
//! command type checking, so most of the errors can be found at compile time instead of at
132-
//! runtime.
133+
//! runtime. With tools like `rust-analyzer`, it can give you real-time feedback for broken
134+
//! commands being used.
135+
//!
136+
//! You can use `cargo expand` to check the generated code.
133137
//!
134138
//! ### Intuitive parameters passing
135139
//! When passing parameters to `run_cmd!` and `run_fun!` macros, if they are not part to rust
@@ -177,6 +181,24 @@
177181
//! Right now piping and stdin, stdout, stderr redirection are supported. Most parts are the same as in
178182
//! [bash scripts](https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Redirections).
179183
//!
184+
//! ### logging
185+
//!
186+
//! This library provides convenient macros and builtin commands for logging. It also automatically logs
187+
//! command execution failures, along with messages which were printed to stderr originally.
188+
//!
189+
//! ```no_run
190+
//! # use cmd_lib::*;
191+
//! let dir: &str = "folder with spaces";
192+
//! assert!(run_cmd!(mkdir /tmp/$dir; ls /tmp/$dir).is_ok());
193+
//! assert!(run_cmd!(mkdir /tmp/"$dir"; ls /tmp/"$dir"; rmdir /tmp/"$dir").is_err());
194+
//! // output:
195+
//! // INFO - mkdir: cannot create directory ‘/tmp/folder with spaces’: File exists
196+
//! // ERROR - Running ["mkdir", "/tmp/folder with spaces"] failed, Error: ["mkdir", "/tmp/folder with spaces"] exited with error; status code: 1
197+
//! ```
198+
//!
199+
//! It is using rust [log crate](https://crates.io/crates/log), and you can use your actual favorite logging
200+
//! implementation.
201+
//!
180202
//! ### Builtin commands
181203
//! #### cd
182204
//! cd: set process current directory, which is always enabled
@@ -302,7 +324,9 @@ pub use cmd_lib_macros::{
302324
cmd_debug, cmd_die, cmd_echo, cmd_error, cmd_info, cmd_trace, cmd_warn, export_cmd, run_cmd,
303325
run_fun, spawn, spawn_with_output, use_builtin_cmd, use_custom_cmd,
304326
};
327+
/// Return type for run_fun!() macro
305328
pub type FunResult = std::io::Result<String>;
329+
/// Return type for run_cmd!() macro
306330
pub type CmdResult = std::io::Result<()>;
307331
pub use builtins::{
308332
builtin_cat, builtin_debug, builtin_die, builtin_echo, builtin_error, builtin_info,

src/process.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::path::Path;
1313
use std::process::{Command, Stdio};
1414
use std::sync::Mutex;
1515

16-
/// Process environment for builtin or custom commands
16+
/// Environment for builtin or custom commands
1717
pub struct CmdEnv {
1818
stdin: CmdIn,
1919
stdout: CmdOut,
@@ -23,26 +23,32 @@ pub struct CmdEnv {
2323
current_dir: String,
2424
}
2525
impl CmdEnv {
26+
/// Returns the arguments for this command
2627
pub fn args(&self) -> &[String] {
2728
&self.args
2829
}
2930

31+
/// Fetches the environment variable key for this command
3032
pub fn var(&self, key: &str) -> Option<&String> {
3133
self.vars.get(key)
3234
}
3335

36+
/// Returns the current working directory for this command
3437
pub fn current_dir(&self) -> &str {
3538
&self.current_dir
3639
}
3740

41+
/// Returns a new handle to the standard input for this command
3842
pub fn stdin(&mut self) -> impl Read + '_ {
3943
&mut self.stdin
4044
}
4145

46+
/// Returns a new handle to the standard output for this command
4247
pub fn stdout(&mut self) -> impl Write + '_ {
4348
&mut self.stdout
4449
}
4550

51+
/// Returns a new handle to the standard error for this command
4652
pub fn stderr(&mut self) -> impl Write + '_ {
4753
&mut self.stderr
4854
}

0 commit comments

Comments
 (0)