Skip to content

Commit bba147f

Browse files
read environment variable (#604)
* read environment variable * set default config path
1 parent 019c0a0 commit bba147f

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/os.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
| [Run piped external commands][ex-run-piped-external-commands] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
88
| [Redirect both stdout and stderr of child process to the same file][ex-redirect-stdout-stderr-same-file] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
99
| [Continuously process child process' outputs][ex-continuous-process-output] | [![std-badge]][std] | [![cat-os-badge]][cat-os][![cat-text-processing-badge]][cat-text-processing] |
10+
| [Read environment variable][ex-read-env-variable] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
11+
1012

1113
[ex-parse-subprocess-output]: os/external.html#run-an-external-command-and-process-stdout
1214
[ex-parse-subprocess-input]: os/external.html#run-an-external-command-passing-it-stdin-and-check-for-an-error-code
1315
[ex-run-piped-external-commands]: os/external.html#run-piped-external-commands
1416
[ex-redirect-stdout-stderr-same-file]: os/external.html#redirect-both-stdout-and-stderr-of-child-process-to-the-same-file
1517
[ex-continuous-process-output]: os/external.html#continuously-process-child-process-outputs
18+
[ex-read-env-variable]: os/external.html#read-environment-variable
19+
1620

1721
{{#include links.md}}

src/os/external.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010

1111
{{#include external/continuous.md}}
1212

13+
{{#include external/read-env-variable.md}}
14+
1315
{{#include ../links.md}}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Read Environment Variable
2+
3+
[![std-badge]][std] [![cat-os-badge]][cat-os]
4+
5+
Reads an environment variable via [std::env::var].
6+
7+
```rust,edition2018,no_run
8+
use std::env;
9+
use std::fs;
10+
use std::io::Error;
11+
12+
fn main() -> Result<(), Error> {
13+
// read `config_path` from the environment variable `CONFIG`.
14+
// If `CONFIG` isn't set, fall back to a default config path.
15+
let config_path = env::var("CONFIG")
16+
.unwrap_or("/etc/myapp/config".to_string());
17+
18+
let config: String = fs::read_to_string(config_path)?;
19+
println!("Config: {}", config);
20+
21+
Ok(())
22+
}
23+
```
24+
25+
[std::env::var]: https://doc.rust-lang.org/std/env/fn.var.html

0 commit comments

Comments
 (0)