2020-07-07 22:45:32 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-04-05 19:42:55 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
|
|
|
|
|
|
|
use crate::configs::docker_context::DockerContextConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2020-04-05 19:42:55 +00:00
|
|
|
use crate::utils;
|
|
|
|
|
|
|
|
/// Creates a module with the currently active Docker context
|
|
|
|
///
|
|
|
|
/// Will display the Docker context if the following criteria are met:
|
2021-11-23 16:56:16 +00:00
|
|
|
/// - There is a non-empty environment variable named DOCKER_HOST
|
|
|
|
/// - Or there is a non-empty environment variable named DOCKER_CONTEXT
|
2021-07-03 21:38:39 +00:00
|
|
|
/// - Or there is a file named `$HOME/.docker/config.json`
|
2020-07-07 22:45:32 +00:00
|
|
|
/// - Or a file named `$DOCKER_CONFIG/config.json`
|
2020-04-05 19:42:55 +00:00
|
|
|
/// - The file is JSON and contains a field named `currentContext`
|
|
|
|
/// - The value of `currentContext` is not `default`
|
2021-07-03 21:38:39 +00:00
|
|
|
/// - If multiple criterias are met, we use the following order to define the docker context:
|
|
|
|
/// - DOCKER_HOST, DOCKER_CONTEXT, $HOME/.docker/config.json, $DOCKER_CONFIG/config.json
|
|
|
|
/// - (This is the same order docker follows, as DOCKER_HOST and DOCKER_CONTEXT override the
|
|
|
|
/// config)
|
2020-04-05 19:42:55 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|
|
|
let mut module = context.new_module("docker_context");
|
|
|
|
let config: DockerContextConfig = DockerContextConfig::try_load(module.config);
|
|
|
|
|
|
|
|
if config.only_with_files
|
|
|
|
&& !context
|
|
|
|
.try_begin_scan()?
|
2021-02-16 19:05:43 +00:00
|
|
|
.set_files(&config.detect_files)
|
|
|
|
.set_extensions(&config.detect_extensions)
|
|
|
|
.set_folders(&config.detect_folders)
|
2020-04-05 19:42:55 +00:00
|
|
|
.is_match()
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
2021-02-16 19:05:43 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let docker_config = PathBuf::from(
|
2020-08-07 19:13:12 +00:00
|
|
|
&context
|
|
|
|
.get_env_os("DOCKER_CONFIG")
|
2021-01-19 22:23:27 +00:00
|
|
|
.unwrap_or(context.get_home()?.join(".docker").into_os_string()),
|
2020-07-07 22:45:32 +00:00
|
|
|
)
|
|
|
|
.join("config.json");
|
2020-04-05 19:42:55 +00:00
|
|
|
|
2021-10-25 05:54:39 +00:00
|
|
|
let docker_context_env =
|
|
|
|
std::array::IntoIter::new(["DOCKER_MACHINE_NAME", "DOCKER_HOST", "DOCKER_CONTEXT"])
|
|
|
|
.find_map(|env| context.get_env(env));
|
2020-07-07 22:45:32 +00:00
|
|
|
|
2021-07-03 21:38:39 +00:00
|
|
|
let ctx = match docker_context_env {
|
|
|
|
Some(data) => data,
|
|
|
|
_ => {
|
|
|
|
if !docker_config.exists() {
|
|
|
|
return None;
|
2020-04-05 19:42:55 +00:00
|
|
|
}
|
2021-07-03 21:38:39 +00:00
|
|
|
let json = utils::read_file(docker_config).ok()?;
|
|
|
|
let parsed_json: serde_json::Value = serde_json::from_str(&json).ok()?;
|
|
|
|
parsed_json.get("currentContext")?.as_str()?.to_owned()
|
2020-04-05 19:42:55 +00:00
|
|
|
}
|
2021-07-03 21:38:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"context" => Some(Ok(ctx.as_str())),
|
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2021-07-03 21:38:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `docker_context`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Some(module)
|
2020-04-05 19:42:55 +00:00
|
|
|
}
|
2021-01-26 21:46:17 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
use ansi_term::Color;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, Write};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn only_trigger_when_docker_config_exists() -> io::Result<()> {
|
|
|
|
let cfg_dir = tempfile::tempdir()?;
|
|
|
|
let actual = ModuleRenderer::new("docker_context")
|
|
|
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
|
|
|
.collect();
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
cfg_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_docker_compose_yml() -> io::Result<()> {
|
|
|
|
let cfg_dir = tempfile::tempdir()?;
|
|
|
|
let cfg_file = cfg_dir.path().join("config.json");
|
|
|
|
|
|
|
|
let pwd = tempfile::tempdir()?;
|
|
|
|
File::create(pwd.path().join("docker-compose.yml"))?.sync_all()?;
|
|
|
|
|
|
|
|
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_CONFIG", cfg_dir.path().to_string_lossy())
|
|
|
|
.path(pwd.path())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()?;
|
|
|
|
pwd.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_docker_compose_yaml() -> io::Result<()> {
|
|
|
|
let cfg_dir = tempfile::tempdir()?;
|
|
|
|
let cfg_file = cfg_dir.path().join("config.json");
|
|
|
|
|
|
|
|
let pwd = tempfile::tempdir()?;
|
|
|
|
File::create(pwd.path().join("docker-compose.yaml"))?.sync_all()?;
|
|
|
|
|
|
|
|
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_CONFIG", cfg_dir.path().to_string_lossy())
|
|
|
|
.path(pwd.path())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()?;
|
|
|
|
pwd.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_dockerfile() -> io::Result<()> {
|
|
|
|
let cfg_dir = tempfile::tempdir()?;
|
|
|
|
let cfg_file = cfg_dir.path().join("config.json");
|
|
|
|
|
|
|
|
let pwd = tempfile::tempdir()?;
|
|
|
|
File::create(pwd.path().join("Dockerfile"))?.sync_all()?;
|
|
|
|
|
|
|
|
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_CONFIG", cfg_dir.path().to_string_lossy())
|
|
|
|
.path(pwd.path())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()?;
|
|
|
|
pwd.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_docker_files() -> 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_CONFIG", cfg_dir.path().to_string_lossy())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_scan_for_docker_files() -> 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_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("🐳 starship")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_json() -> io::Result<()> {
|
|
|
|
let cfg_dir = tempfile::tempdir()?;
|
|
|
|
let cfg_file = cfg_dir.path().join("config.json");
|
|
|
|
|
|
|
|
let config_content = "not valid json";
|
|
|
|
|
|
|
|
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_CONFIG", cfg_dir.path().to_string_lossy())
|
|
|
|
.config(toml::toml! {
|
|
|
|
[docker_context]
|
|
|
|
only_with_files = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()
|
|
|
|
}
|
2021-07-03 21:38:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_docker_host_env() -> io::Result<()> {
|
|
|
|
let cfg_dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("docker_context")
|
|
|
|
.env("DOCKER_HOST", "udp://starship@127.0.0.1:53")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[docker_context]
|
|
|
|
only_with_files = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!(
|
|
|
|
"via {} ",
|
|
|
|
Color::Blue.bold().paint("🐳 udp://starship@127.0.0.1:53")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_docker_context_env() -> io::Result<()> {
|
|
|
|
let cfg_dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("docker_context")
|
|
|
|
.env("DOCKER_CONTEXT", "starship")
|
|
|
|
.config(toml::toml! {
|
|
|
|
[docker_context]
|
|
|
|
only_with_files = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_docker_context_overrides_config() -> 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_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("🐳 starship")));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
cfg_dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_docker_host_overrides_docker_context_env_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_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("🐳 udp://starship@127.0.0.1:53")
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
2021-10-25 05:54:39 +00:00
|
|
|
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);
|
|
|
|
|
2021-07-03 21:38:39 +00:00
|
|
|
cfg_dir.close()
|
|
|
|
}
|
2021-01-26 21:46:17 +00:00
|
|
|
}
|