feat: also read from DOCKER_MACHINE_NAME (#3175)
This adds support to also read the context from `DOCKER_MACHINE_NAME` since it is a bit more user friendly.
This commit is contained in:
parent
48fca507f5
commit
39e7b78cb2
|
@ -836,9 +836,10 @@ truncation_symbol = "…/"
|
|||
## Docker Context
|
||||
|
||||
The `docker_context` module shows the currently active
|
||||
[Docker context](https://docs.docker.com/engine/context/working-with-contexts/) if it's not set to
|
||||
`default` or if the `DOCKER_HOST` or `DOCKER_CONTEXT` environment variables are set (as they are meant
|
||||
to override the context in use).
|
||||
[Docker context](https://docs.docker.com/engine/context/working-with-contexts/)
|
||||
if it's not set to `default` or if the `DOCKER_MACHINE_NAME`, `DOCKER_HOST` or
|
||||
`DOCKER_CONTEXT` environment variables are set (as they are meant to override
|
||||
the context in use).
|
||||
|
||||
### Options
|
||||
|
||||
|
|
|
@ -41,8 +41,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||
)
|
||||
.join("config.json");
|
||||
|
||||
let docker_context_env = std::array::IntoIter::new(["DOCKER_HOST", "DOCKER_CONTEXT"])
|
||||
.find_map(|env| context.get_env(env));
|
||||
let docker_context_env =
|
||||
std::array::IntoIter::new(["DOCKER_MACHINE_NAME", "DOCKER_HOST", "DOCKER_CONTEXT"])
|
||||
.find_map(|env| context.get_env(env));
|
||||
|
||||
let ctx = match docker_context_env {
|
||||
Some(data) => data,
|
||||
|
@ -365,6 +366,39 @@ mod tests {
|
|||
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
cfg_dir.close()
|
||||
}
|
||||
#[test]
|
||||
fn test_docker_machine_name_overrides_other_env_vars_and_conf() -> io::Result<()> {
|
||||
let cfg_dir = tempfile::tempdir()?;
|
||||
|
||||
let cfg_file = cfg_dir.path().join("config.json");
|
||||
|
||||
let config_content = serde_json::json!({
|
||||
"currentContext": "starship"
|
||||
});
|
||||
|
||||
let mut docker_config = File::create(&cfg_file)?;
|
||||
docker_config.write_all(config_content.to_string().as_bytes())?;
|
||||
docker_config.sync_all()?;
|
||||
|
||||
let actual = ModuleRenderer::new("docker_context")
|
||||
.env("DOCKER_MACHINE_NAME", "machine_name")
|
||||
.env("DOCKER_HOST", "udp://starship@127.0.0.1:53")
|
||||
.env("DOCKER_CONTEXT", "starship")
|
||||
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||
.config(toml::toml! {
|
||||
[docker_context]
|
||||
only_with_files = false
|
||||
})
|
||||
.collect();
|
||||
let expected = Some(format!(
|
||||
"via {} ",
|
||||
Color::Blue.bold().paint("🐳 machine_name")
|
||||
));
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
cfg_dir.close()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue