Crates.io | envvars |
lib.rs | envvars |
version | 0.1.9 |
source | src |
created_at | 2023-03-03 11:12:29.277713 |
updated_at | 2024-02-01 08:55:41.708579 |
description | Getting environment variables considering shell configuration |
homepage | https://github.com/esrlabs/envvars |
repository | https://github.com/esrlabs/envvars.git |
max_upload_size | |
id | 799670 |
size | 54,948 |
envvars
helps to detect a list of available shells and related environment variables. If works in two steps:
Profile
for each found shellGetting all available (detected) shells and related to each shell list of environment variables
use envvars::{get_profiles, Profile};
// ...
let mut profiles: Vec<Profile> = get_profiles().unwrap();
// By default profile doesn't have loaded list of environment variables.
// It should be loaded calling method load().
profiles.iter_mut().for_each(|profile| {
// Attempt to load envvars
if let Err(err) = profile.load() {
eprintln!("Cannot load envvars for {}: {err}", profile.name);
}
if let Some(envvars) = &profile.envvars {
println!("Environment variables for {}", profile.name);
envvars.iter().for_each(|(key, value)| {
println!("{key}: {value}");
});
}
});
Extract environment variables without shell context.
use std::collections::HashMap;
use envvars::get_context_envvars;
// ...
let vars: HashMap<String, String> = get_context_envvars().unwrap();
assert!(vars.contains_key("PATH") || vars.contains_key("Path") || vars.contains_key("path"));
std::env::vars
envvars
actually executes each found shell
it means: all settings of the target shell will be inited before a list of environment variables will be requested. That's very sensitive if the configuration of some shell includes some initialization script, which affects environment variables. That means in some cases std::env::vars
and envvars
could give different results.
Under the hood, envvars
takes each shell, and executes it with a command, which posts a list of environment variables to stdout
. As soon as executing
is done, envvars
reads stdout
and parse environment variables into HashMap<String, String>
.
As soon as extracting process could take a sensitive time (~1sec on windows and ~10ms on Unix-based OS), envvars
doesn't extract environment variables
during detecting the shell's profiles. That's the developer's decision when it should be done for the selected or each profile.
envvars
creates a small executable application in the system's temporary folder. This application is used to "drop" list of environment variables into stdout
of the parent process and does nothing else. As soon as envvars
instance is dropped, the application would be removed from the disk.
For security reasons envvars
checks the checksum of the extractor each time before using it. If a checksum is invalid (the file was damaged/changed etc),
envars
will remove a corrupted file and create a new one.
envvars
reads /etc/shells
and analyze each shell from a list
envvars
checks for availability next shells:
Because envvars
tries to initialize each shell and "drop" a list of environment variables to stdout
, the shell should support the possibility to put a command as an argument, for example: /bin/bash -c path_to_command
. Obviously not many, but still some shells don't support it (like windows command prompt). In this case, you still can use get_context_envvars()
to get a list of environment variables without the shell's context.