Skip to content

Commit 862f601

Browse files
authored
Merge pull request #122 from Michael-F-Bryan/parsing-subprocess-output
Parsing subprocess output
2 parents fd0e0b4 + a1f0418 commit 862f601

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/basics.md

+78
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
| [Generate random numbers within a range][ex-rand-range] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
99
| [Generate random numbers with normal distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
1010
| [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
11+
| [Run an External Command and Process Stdout][ex-parse-subprocess-output] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] |
12+
1113

1214

1315
[ex-std-read-lines]: #ex-std-read-lines
@@ -200,6 +202,73 @@ fn main() {
200202
}
201203
```
202204

205+
[ex-parse-subprocess-output]: #ex-parse-subprocess-output
206+
<a name="ex-parse-subprocess-output"></a>
207+
## Run an External Command and Process Stdout
208+
209+
[![regex-badge]][regex] [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing]
210+
211+
`git log --oneline` is run as an external [`Command`] and its [`Output`] is
212+
inspected using [`Regex`] to get the hash and message of the last 5 commits.
213+
214+
```rust
215+
#[macro_use]
216+
extern crate error_chain;
217+
extern crate regex;
218+
219+
use std::process::Command;
220+
use regex::Regex;
221+
222+
error_chain!{
223+
foreign_links {
224+
Io(std::io::Error);
225+
Regex(regex::Error);
226+
Utf8(std::string::FromUtf8Error);
227+
}
228+
}
229+
230+
231+
#[derive(PartialEq, Default, Clone, Debug)]
232+
struct Commit {
233+
hash: String,
234+
message: String,
235+
}
236+
237+
238+
fn run() -> Result<()> {
239+
let output = Command::new("git").arg("log").arg("--oneline").output()?;
240+
241+
if !output.status.success() {
242+
bail!("Command executed with failing error code");
243+
}
244+
245+
let pattern = Regex::new(r"(?x)
246+
([0-9a-fA-F]+) # commit hash
247+
(.*) # The commit message")?;
248+
249+
let stdout = String::from_utf8(output.stdout)?;
250+
let commits = stdout
251+
.lines()
252+
.filter_map(|line| pattern.captures(line))
253+
.map(|cap| {
254+
Commit {
255+
hash: cap[1].to_string(),
256+
message: cap[2].trim().to_string(),
257+
}
258+
})
259+
.take(5);
260+
261+
for commit in commits {
262+
println!("{:?}", commit);
263+
}
264+
265+
Ok(())
266+
}
267+
268+
quick_main!(run);
269+
```
270+
271+
203272
<!-- Categories -->
204273

205274
[cat-encoding-badge]: https://img.shields.io/badge/-encoding-red.svg
@@ -208,6 +277,10 @@ fn main() {
208277
[cat-filesystem]: https://crates.io/categories/filesystem
209278
[cat-science-badge]: https://img.shields.io/badge/-rand-red.svg
210279
[cat-science]: https://crates.io/categories/science
280+
[cat-os-badge]: https://img.shields.io/badge/-os-red.svg
281+
[cat-os]: https://crates.io/categories/os
282+
[cat-text-processing-badge]: https://img.shields.io/badge/-text_processing-red.svg
283+
[cat-text-processing]: https://crates.io/categories/text-processing
211284

212285
<!-- Crates -->
213286

@@ -217,6 +290,8 @@ fn main() {
217290
[rand]: https://docs.rs/rand/
218291
[std-badge]: https://img.shields.io/badge/std-1.17.0-blue.svg
219292
[std]: https://doc.rust-lang.org/std
293+
[regex]: https://docs.rs/regex/
294+
[regex-badge]: https://img.shields.io/crates/v/regex.svg?label=regex
220295

221296
<!-- API links -->
222297

@@ -232,3 +307,6 @@ fn main() {
232307
[`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample
233308
[`Rng::gen_range`]: https://doc.rust-lang.org/rand/rand/trait.Rng.html#method.gen_range
234309
[`rand::Rand`]: https://doc.rust-lang.org/rand/rand/trait.Rand.html
310+
[`Regex`]: https://doc.rust-lang.org/regex/regex/struct.Regex.html
311+
[`Output`]: https://doc.rust-lang.org/std/process/struct.Output.html
312+
[`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html

src/intro.md

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ community. It needs and welcomes help. For details see
2626
| [Generate random numbers within a range][ex-rand-range] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
2727
| [Generate random numbers with normal distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
2828
| [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
29+
| [Run an External Command and Process Stdout][ex-parse-subprocess-output] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] |
2930

3031
## [Encoding](encoding.html)
3132

@@ -96,6 +97,10 @@ Keep lines sorted.
9697
[cat-net]: https://crates.io/categories/network-programming
9798
[cat-science-badge]: https://img.shields.io/badge/-science-red.svg
9899
[cat-science]: https://crates.io/categories/science
100+
[cat-os-badge]: https://img.shields.io/badge/-os-red.svg
101+
[cat-os]: https://crates.io/categories/os
102+
[cat-text-processing-badge]: https://img.shields.io/badge/-text_processing-red.svg
103+
[cat-text-processing]: https://crates.io/categories/text-processing
99104

100105
<!-- Crates -->
101106

@@ -127,6 +132,8 @@ Keep lines sorted.
127132
[toml]: https://docs.rs/toml/
128133
[url-badge]: https://img.shields.io/crates/v/url.svg?label=url
129134
[url]: https://docs.rs/url/
135+
[regex]: https://docs.rs/regex/
136+
[regex-badge]: https://img.shields.io/crates/v/regex.svg?label=regex
130137

131138
<!-- Examples -->
132139

@@ -157,3 +164,4 @@ Keep lines sorted.
157164
[ex-url-origin]: net.html#ex-url-origin
158165
[ex-url-parse]: net.html#ex-url-parse
159166
[ex-url-rm-frag]: net.html#ex-url-rm-frag
167+
[ex-parse-subprocess-output]: basics.html#ex-parse-subprocess-output

0 commit comments

Comments
 (0)