read environment variable (#604)

* read environment variable

* set default config path
This commit is contained in:
Stefan Mesken 2020-06-14 01:01:41 +02:00 committed by GitHub
parent 019c0a0d2c
commit bba147f185
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View file

@ -7,11 +7,15 @@
| [Run piped external commands][ex-run-piped-external-commands] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
| [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] |
| [Continuously process child process' outputs][ex-continuous-process-output] | [![std-badge]][std] | [![cat-os-badge]][cat-os][![cat-text-processing-badge]][cat-text-processing] |
| [Read environment variable][ex-read-env-variable] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
[ex-parse-subprocess-output]: os/external.html#run-an-external-command-and-process-stdout
[ex-parse-subprocess-input]: os/external.html#run-an-external-command-passing-it-stdin-and-check-for-an-error-code
[ex-run-piped-external-commands]: os/external.html#run-piped-external-commands
[ex-redirect-stdout-stderr-same-file]: os/external.html#redirect-both-stdout-and-stderr-of-child-process-to-the-same-file
[ex-continuous-process-output]: os/external.html#continuously-process-child-process-outputs
[ex-read-env-variable]: os/external.html#read-environment-variable
{{#include links.md}}

View file

@ -10,4 +10,6 @@
{{#include external/continuous.md}}
{{#include external/read-env-variable.md}}
{{#include ../links.md}}

25
src/os/external/read-env-variable.md vendored Normal file
View file

@ -0,0 +1,25 @@
## Read Environment Variable
[![std-badge]][std] [![cat-os-badge]][cat-os]
Reads an environment variable via [std::env::var].
```rust,edition2018,no_run
use std::env;
use std::fs;
use std::io::Error;
fn main() -> Result<(), Error> {
// read `config_path` from the environment variable `CONFIG`.
// If `CONFIG` isn't set, fall back to a default config path.
let config_path = env::var("CONFIG")
.unwrap_or("/etc/myapp/config".to_string());
let config: String = fs::read_to_string(config_path)?;
println!("Config: {}", config);
Ok(())
}
```
[std::env::var]: https://doc.rust-lang.org/std/env/fn.var.html