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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! # Fun Run
//!
//! What does the "Zombie Zoom 5K", the "Wibbly wobbly log jog", and the "Turkey Trot" have in common?
//! They're runs with a fun name! The `fun_run` library adds display and safety features to make
//! running a Rust [`Command`] better for you and your users.
//!
//! Stream the command and raise on non-zero exit:
//!
//! ```rust
//! use fun_run::CommandWithName;
//! use std::process::Command;
//!
//! let mut cmd = Command::new("bash");
//! cmd.args(["-c", "echo -n oops all berries; exit 1"]);
//!
//! // Advertise the command being run before execution
//! println!("Running `{name}`", name = cmd.name());
//!
//! // Stream output to the end user
//! // Turn non-zero status results into an error
//! let error = cmd
//! .stream_output(std::io::stdout(), std::io::stderr())
//! .unwrap_err();
//!
//! assert_eq!(
//! indoc::indoc!{r#"
//! Command failed `bash -c "echo -n oops all berries; exit 1"`
//! exit status: 1
//! stdout: <see above>
//! stderr: <see above>
//! "#}.trim().to_string(),
//! error.to_string()
//! );
//! ```
//!
//! Run the command quietly, capture stdout/stderr and raise on non-zero exit:
//!
//! ```
//! # use fun_run::CommandWithName;
//! # use std::process::Command;
//! # let mut cmd = Command::new("bash");
//! # cmd.args(["-c", "echo -n oops all berries; exit 1"]);
//! let error = cmd.named_output().unwrap_err();
//! assert_eq!(
//! indoc::indoc!{r#"
//! Command failed `bash -c "echo -n oops all berries; exit 1"`
//! exit status: 1
//! stdout: oops all berries
//! stderr: <empty>
//! "#}.trim().to_string(),
//! error.to_string()
//! );
//! ```
//!
//! Output of the command is preserved in success and error cases:
//!
//! ```
//! # use fun_run::CommandWithName;
//! # use std::process::Command;
//! # let mut cmd = Command::new("bash");
//! # cmd.args(["-c", "echo -n oops all berries; exit 1"]);
//! # let error = cmd.named_output().unwrap_err();
//! // Both Ok and Err from result store the output for inspection
//! assert!(
//! error.output().unwrap().stdout_lossy()
//! .contains("oops all berries")
//! );
//! ```
//!
//! ## Install
//!
//! ```shell
//! $ cargo add fun_run
//! ```
//!
//! ## Renaming a command
//!
//! If you need to provide an alternate display for your command you can rename it, this is useful
//! for omitting implementation details.
//!
//! ```rust
//! use fun_run::CommandWithName;
//! use std::process::Command;
//!
//! let mut cmd = Command::new("bash");
//! cmd
//! .args(["-eo", "pipefail", "-c"])
//! .arg("echo -n 'hello world' && exit 1");
//!
//! let mut renamed_cmd = cmd.named("echo 'hello world'");
//!
//! assert_eq!("echo 'hello world'", &renamed_cmd.name());
//! ```
//!
//! This is also useful for adding additional information, such as environment variables:
//!
//! ```rust
//! use fun_run::CommandWithName;
//! use std::process::Command;
//!
//! let mut cmd = Command::new("bundle");
//! cmd.arg("install");
//!
//! let env_vars = std::env::vars();
//! # let mut env_vars = std::collections::HashMap::<String, String>::new();
//! # env_vars.insert("RAILS_ENV".to_string(), "production".to_string());
//!
//! let mut renamed_cmd = cmd.named_fn(|cmd| fun_run::display_with_env_keys(
//! cmd,
//! env_vars,
//! ["RAILS_ENV"]
//! ));
//!
//! assert_eq!(r#"RAILS_ENV="production" bundle install"#, renamed_cmd.name())
//! ```
//!
//! ## What won't it do?
//!
//! The `fun_run` library doesn't support executing a [`Command`] in ways that do not produce an
//! [`Output`], for example calling [`Command::spawn`](https://doc.rust-lang.org/std/process/struct.Command.html#method.spawn) returns a [`std::process::Child`]
//! (Which doesn't contain an [`Output`]). If you want to run-for-fun in the background, spawn a thread
//! and join it manually:
//!
//! ```no_run
//! use fun_run::CommandWithName;
//! use std::process::Command;
//! use std::thread;
//!
//! let mut cmd = Command::new("bundle");
//! cmd.args(["install"]);
//!
//! // Advertise the command being run before execution
//! println!("Quietly Running `{name}` in the background", name = cmd.name());
//!
//! let result = thread::spawn(move || {
//! cmd.named_output()
//! }).join().unwrap();
//!
//! // Command name is persisted on success or failure
//! match result {
//! Ok(output) => {
//! assert_eq!("bundle install", &output.name())
//! },
//! Err(cmd_error) => {
//! assert_eq!("bundle install", &cmd_error.name())
//! }
//! }
//! ```
//!
//! ## Async
//!
//! This library uses synchronous command execution. If you’re using this library in an async context,
//! you’ll want to use an async wrapper like [tokio::task::spawn_blocking](https://docs.rs/tokio/latest/tokio/task/fn.spawn_blocking.html).
//!
//! ## Clippy
//!
//! To ensure all commands have their exit status checked you can add this to your `clippy.toml` to
//! prevent accidentally spawning an un-checked plain [`Command`]:
//!
//! ```toml
//! [[disallowed-methods]]
//! path = "std::process::Command::output"
//! reason = "Use fun_run::CommandWithName::named_output"
//!
//! [[disallowed-methods]]
//! path = "std::process::Command::status"
//! reason = "Use fun_run::CommandWithName::named_output and read the status from the result"
//!
//! [[disallowed-methods]]
//! path = "std::process::Command::spawn"
//! reason = "Use fun_run::CommandWithName::stream_output(std::io::stdout(), std::io::stderr())"
//! ```
//!
//! ## Debugging system failures with `which_problem`
//!
//! When a command execution returns an Err due to a system error (and not because the program it
//! executed launched but returned non-zero status), it's usually because the executable couldn't be
//! found, or if it was found, it couldn't be launched, for example due to a permissions error. The
//! [which_problem](https://github.com/schneems/which_problem) crate is designed to add debugging errors
//! to help you identify why the command couldn't be launched.
//!
//! The crate `which_problem` works like `which` but helps you identify common mistakes such as typos:
//!
//! ```shell
//! $ cargo whichp zuby
//! Program "zuby" not found
//!
//! Info: No other executables with the same name are found on the PATH
//!
//! Info: These executables have the closest spelling to "zuby" but did not match:
//! "hub", "ruby", "subl"
//! ```
//!
//! Fun run supports `which_problem` integration through the `which_problem` feature. In your `Cargo.toml`:
//!
//! ```toml
//! # Cargo.toml
//! fun_run = { version = <version.here>, features = ["which_problem"] }
//! ```
//!
//! And annotate errors:
//!
//! ```rust,no_run
//! #[cfg(not(feature = "which_problem"))] { return; }
//! use fun_run::CommandWithName;
//! use std::process::Command;
//!
//! let mut cmd = Command::new("becho");
//! cmd.args(["hello", "world"]);
//!
//! #[cfg(feature = "which_problem")]
//! cmd.stream_output(std::io::stdout(), std::io::stderr())
//! .map_err(|error| fun_run::map_which_problem(error, cmd.mut_cmd(), std::env::var_os("PATH"))).unwrap();
//! ```
//!
//! Now if the system cannot find a `becho` program on your system the output will give you all the
//! info you need to diagnose the underlying issue.
//!
//! Note that `which_problem` integration is not enabled by default because it outputs information
//! about the contents of your disk such as layout and file permissions.
//!
//! ## Nightly-only items
//!
//! A few items (`display_env_vars` and `CommandWithName::named_env_vars`) require a
//! nightly toolchain. They depend on the unstable
//! [`command_resolved_envs`](https://github.com/rust-lang/rust/issues/149070)
//! feature, auto-detected at build time, and are absent on stable. Because
//! <https://docs.rs> builds on nightly, these appear in the published docs even
//! though stable users cannot use them.
use ;
pub use CmdError;
pub use ExitStatusFromCode;
pub use display_env_vars;
pub use ;
pub use ;
pub use NamedOutput;
pub use OutputWithName;
pub use map_which_problem;