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:
Thomas O'Donnell 2021-10-25 07:54:39 +02:00 committed by GitHub
parent 48fca507f5
commit 39e7b78cb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View File

@ -836,9 +836,10 @@ truncation_symbol = "…/"
## Docker Context ## Docker Context
The `docker_context` module shows the currently active 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 [Docker context](https://docs.docker.com/engine/context/working-with-contexts/)
`default` or if the `DOCKER_HOST` or `DOCKER_CONTEXT` environment variables are set (as they are meant if it's not set to `default` or if the `DOCKER_MACHINE_NAME`, `DOCKER_HOST` or
to override the context in use). `DOCKER_CONTEXT` environment variables are set (as they are meant to override
the context in use).
### Options ### Options

View File

@ -41,8 +41,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
) )
.join("config.json"); .join("config.json");
let docker_context_env = std::array::IntoIter::new(["DOCKER_HOST", "DOCKER_CONTEXT"]) let docker_context_env =
.find_map(|env| context.get_env(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 { let ctx = match docker_context_env {
Some(data) => data, Some(data) => data,
@ -365,6 +366,39 @@ mod tests {
assert_eq!(expected, actual); 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() cfg_dir.close()
} }
} }