1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! # ensembler
//!
//! A library for executing external commands with advanced output handling
//! and progress reporting capabilities.
//!
//! ## Features
//!
//! - **Async execution** - Built on Tokio for non-blocking command execution
//! - **Output capture** - Capture stdout, stderr, and combined output
//! - **Progress integration** - Real-time progress bar updates via the `clx` crate
//! - **Secret redaction** - Automatically redact sensitive data from output
//! - **Cancellation** - Support for cancelling running commands via `CancellationToken`
//! - **Cross-platform** - Works on Unix and Windows
//!
//! ## Basic Usage
//!
//! > **Note:** Examples use Unix commands for clarity. On Windows, substitute
//! > equivalent commands (e.g., `dir` for `ls`, `type` for `cat`).
//!
//! ```no_run
//! use ensembler::CmdLineRunner;
//!
//! #[tokio::main]
//! async fn main() -> ensembler::Result<()> {
//! let result = CmdLineRunner::new("echo")
//! .arg("hello")
//! .execute()
//! .await?;
//!
//! println!("stdout: {}", result.stdout);
//! Ok(())
//! }
//! ```
//!
//! ## Redacting Secrets
//!
//! Sensitive data can be automatically redacted from command output:
//!
//! ```no_run
//! use ensembler::CmdLineRunner;
//!
//! #[tokio::main]
//! async fn main() -> ensembler::Result<()> {
//! let api_key = "super-secret-key";
//! let result = CmdLineRunner::new("echo")
//! .arg(api_key)
//! .redact(vec![api_key.to_string()])
//! .execute()
//! .await?;
//!
//! // stdout will contain "[redacted]" instead of the actual key
//! assert!(!result.stdout.contains(api_key));
//! Ok(())
//! }
//! ```
//!
//! ## Stdin Input
//!
//! Pipe input to a command's stdin:
//!
//! ```no_run
//! use ensembler::CmdLineRunner;
//!
//! #[tokio::main]
//! async fn main() -> ensembler::Result<()> {
//! let result = CmdLineRunner::new("cat")
//! .stdin_string("hello from stdin")
//! .execute()
//! .await?;
//!
//! assert_eq!(result.stdout.trim(), "hello from stdin");
//! Ok(())
//! }
//! ```
//!
//! ## Cancellation
//!
//! Commands can be cancelled using a `CancellationToken`:
//!
//! ```no_run
//! use ensembler::CmdLineRunner;
//! use tokio_util::sync::CancellationToken;
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> ensembler::Result<()> {
//! let cancel = CancellationToken::new();
//! let cancel_clone = cancel.clone();
//!
//! // Cancel after 1 second
//! tokio::spawn(async move {
//! tokio::time::sleep(Duration::from_secs(1)).await;
//! cancel_clone.cancel();
//! });
//!
//! let result = CmdLineRunner::new("sleep")
//! .arg("60")
//! .with_cancel_token(cancel)
//! .execute()
//! .await;
//!
//! // Command was cancelled
//! assert!(result.is_err());
//! Ok(())
//! }
//! ```
extern crate log;
pub use ;
pub use ;