2019-10-01 18:58:24 +00:00
|
|
|
use yaml_rust::YamlLoader;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::path;
|
|
|
|
|
2019-10-05 21:13:03 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
2019-10-05 09:31:23 +00:00
|
|
|
|
|
|
|
use crate::configs::kubernetes::KubernetesConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-10-01 18:58:24 +00:00
|
|
|
use crate::utils;
|
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
fn get_kube_context(filename: path::PathBuf) -> Option<String> {
|
|
|
|
let contents = utils::read_file(filename).ok()?;
|
|
|
|
|
2019-10-01 18:58:24 +00:00
|
|
|
let yaml_docs = YamlLoader::load_from_str(&contents).ok()?;
|
|
|
|
if yaml_docs.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let conf = &yaml_docs[0];
|
|
|
|
|
|
|
|
let current_ctx = conf["current-context"].as_str()?;
|
|
|
|
|
|
|
|
if current_ctx.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2020-10-23 11:39:50 +00:00
|
|
|
Some(current_ctx.to_string())
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
fn get_kube_ns(filename: path::PathBuf, current_ctx: String) -> Option<String> {
|
2019-11-12 05:34:01 +00:00
|
|
|
let contents = utils::read_file(filename).ok()?;
|
2020-10-23 11:39:50 +00:00
|
|
|
|
|
|
|
let yaml_docs = YamlLoader::load_from_str(&contents).ok()?;
|
|
|
|
if yaml_docs.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let conf = &yaml_docs[0];
|
|
|
|
|
|
|
|
let ns = conf["contexts"].as_vec().and_then(|contexts| {
|
|
|
|
contexts
|
|
|
|
.iter()
|
|
|
|
.filter_map(|ctx| Some((ctx, ctx["name"].as_str()?)))
|
|
|
|
.find(|(_, name)| *name == current_ctx)
|
|
|
|
.and_then(|(ctx, _)| ctx["context"]["namespace"].as_str())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if ns.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ns.to_owned())
|
2019-11-12 05:34:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 18:58:24 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2020-10-23 11:39:50 +00:00
|
|
|
let mut module = context.new_module("kubernetes");
|
|
|
|
let config: KubernetesConfig = KubernetesConfig::try_load(module.config);
|
|
|
|
|
|
|
|
// As we default to disabled=true, we have to check here after loading our config module,
|
|
|
|
// before it was only checking against whatever is in the config starship.toml
|
|
|
|
if config.disabled {
|
|
|
|
return None;
|
2019-10-01 18:58:24 +00:00
|
|
|
};
|
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
let default_config_file = dirs_next::home_dir()?.join(".kube").join("config");
|
|
|
|
|
|
|
|
let kube_cfg = context
|
|
|
|
.get_env("KUBECONFIG")
|
|
|
|
.unwrap_or(default_config_file.to_str()?.to_string());
|
|
|
|
|
|
|
|
let kube_ctx = env::split_paths(&kube_cfg).find_map(get_kube_context)?;
|
|
|
|
|
|
|
|
let kube_ns =
|
|
|
|
env::split_paths(&kube_cfg).find_map(|filename| get_kube_ns(filename, kube_ctx.clone()));
|
|
|
|
|
|
|
|
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" => match config.context_aliases.get(&kube_ctx) {
|
|
|
|
None => Some(Ok(kube_ctx.as_str())),
|
|
|
|
Some(&alias) => Some(Ok(alias)),
|
|
|
|
},
|
|
|
|
"namespace" => match &kube_ns {
|
|
|
|
Some(kube_ns) => Some(Ok(kube_ns)),
|
|
|
|
None => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `kubernetes`: \n{}", error);
|
|
|
|
return None;
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
2020-10-23 11:39:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Some(module)
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-23 11:39:50 +00:00
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
use ansi_term::Color;
|
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, Write};
|
2019-10-01 18:58:24 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-10-23 11:39:50 +00:00
|
|
|
fn test_none_when_disabled() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let filename = dir.path().join("config");
|
|
|
|
|
|
|
|
let mut file = File::create(&filename)?;
|
|
|
|
file.write_all(
|
|
|
|
b"
|
|
|
|
apiVersion: v1
|
|
|
|
clusters: []
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: test_cluster
|
|
|
|
user: test_user
|
|
|
|
name: test_context
|
|
|
|
current-context: test_context
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users: []
|
|
|
|
",
|
|
|
|
)?;
|
|
|
|
file.sync_all()?;
|
2019-10-01 18:58:24 +00:00
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
let actual = ModuleRenderer::new("kubernetes")
|
|
|
|
.path(dir.path())
|
|
|
|
.env("KUBECONFIG", filename.to_string_lossy().as_ref())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(None, actual);
|
|
|
|
|
|
|
|
dir.close()
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-23 11:39:50 +00:00
|
|
|
fn test_ctx_alias() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let filename = dir.path().join("config");
|
|
|
|
|
|
|
|
let mut file = File::create(&filename)?;
|
|
|
|
file.write_all(
|
|
|
|
b"
|
2019-10-01 18:58:24 +00:00
|
|
|
apiVersion: v1
|
|
|
|
clusters: []
|
|
|
|
contexts: []
|
2020-10-23 11:39:50 +00:00
|
|
|
current-context: test_context
|
2019-10-01 18:58:24 +00:00
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users: []
|
2020-10-23 11:39:50 +00:00
|
|
|
",
|
|
|
|
)?;
|
|
|
|
file.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("kubernetes")
|
|
|
|
.path(dir.path())
|
|
|
|
.env("KUBECONFIG", filename.to_string_lossy().as_ref())
|
|
|
|
.config(toml::toml! {
|
|
|
|
[kubernetes]
|
|
|
|
disabled = false
|
|
|
|
[kubernetes.context_aliases]
|
|
|
|
"test_context" = "test_alias"
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!("{} in ", Color::Cyan.bold().paint("☸ test_alias")));
|
|
|
|
assert_eq!(expected, actual);
|
2019-10-01 18:58:24 +00:00
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
dir.close()
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-23 11:39:50 +00:00
|
|
|
fn test_single_config_file_no_ns() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let filename = dir.path().join("config");
|
|
|
|
|
|
|
|
let mut file = File::create(&filename)?;
|
|
|
|
file.write_all(
|
|
|
|
b"
|
2019-10-01 18:58:24 +00:00
|
|
|
apiVersion: v1
|
|
|
|
clusters: []
|
|
|
|
contexts:
|
2020-10-23 11:39:50 +00:00
|
|
|
- context:
|
|
|
|
cluster: test_cluster
|
|
|
|
user: test_user
|
|
|
|
name: test_context
|
2019-10-01 18:58:24 +00:00
|
|
|
current-context: test_context
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users: []
|
2020-10-23 11:39:50 +00:00
|
|
|
",
|
|
|
|
)?;
|
|
|
|
file.sync_all()?;
|
2019-10-01 18:58:24 +00:00
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
let actual = ModuleRenderer::new("kubernetes")
|
|
|
|
.path(dir.path())
|
|
|
|
.env("KUBECONFIG", filename.to_string_lossy().as_ref())
|
|
|
|
.config(toml::toml! {
|
|
|
|
[kubernetes]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} in ",
|
|
|
|
Color::Cyan.bold().paint("☸ test_context")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
dir.close()
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-23 11:39:50 +00:00
|
|
|
fn test_single_config_file_with_ns() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let filename = dir.path().join("config");
|
|
|
|
|
|
|
|
let mut file = File::create(&filename)?;
|
|
|
|
file.write_all(
|
|
|
|
b"
|
2019-10-01 18:58:24 +00:00
|
|
|
apiVersion: v1
|
|
|
|
clusters: []
|
|
|
|
contexts:
|
2020-10-23 11:39:50 +00:00
|
|
|
- context:
|
|
|
|
cluster: test_cluster
|
|
|
|
user: test_user
|
|
|
|
namespace: test_namespace
|
|
|
|
name: test_context
|
2019-10-01 18:58:24 +00:00
|
|
|
current-context: test_context
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users: []
|
2020-10-23 11:39:50 +00:00
|
|
|
",
|
|
|
|
)?;
|
|
|
|
file.sync_all()?;
|
2019-10-01 18:58:24 +00:00
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
let actual = ModuleRenderer::new("kubernetes")
|
|
|
|
.path(dir.path())
|
|
|
|
.env("KUBECONFIG", filename.to_string_lossy().as_ref())
|
|
|
|
.config(toml::toml! {
|
|
|
|
[kubernetes]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} in ",
|
|
|
|
Color::Cyan.bold().paint("☸ test_context (test_namespace)")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
dir.close()
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-23 11:39:50 +00:00
|
|
|
fn test_single_config_file_with_multiple_ctxs() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let filename = dir.path().join("config");
|
|
|
|
|
|
|
|
let mut file = File::create(&filename)?;
|
|
|
|
file.write_all(
|
|
|
|
b"
|
2019-10-01 18:58:24 +00:00
|
|
|
apiVersion: v1
|
|
|
|
clusters: []
|
|
|
|
contexts:
|
2020-10-23 11:39:50 +00:00
|
|
|
- context:
|
|
|
|
cluster: another_cluster
|
|
|
|
user: another_user
|
|
|
|
namespace: another_namespace
|
|
|
|
name: another_context
|
|
|
|
- context:
|
|
|
|
cluster: test_cluster
|
|
|
|
user: test_user
|
|
|
|
namespace: test_namespace
|
|
|
|
name: test_context
|
2019-10-01 18:58:24 +00:00
|
|
|
current-context: test_context
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users: []
|
2020-10-23 11:39:50 +00:00
|
|
|
",
|
|
|
|
)?;
|
|
|
|
file.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("kubernetes")
|
|
|
|
.path(dir.path())
|
|
|
|
.env("KUBECONFIG", filename.to_string_lossy().as_ref())
|
|
|
|
.config(toml::toml! {
|
|
|
|
[kubernetes]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
2019-10-01 18:58:24 +00:00
|
|
|
|
2020-10-23 11:39:50 +00:00
|
|
|
let expected = Some(format!(
|
|
|
|
"{} in ",
|
|
|
|
Color::Cyan.bold().paint("☸ test_context (test_namespace)")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
dir.close()
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-23 11:39:50 +00:00
|
|
|
fn test_multiple_config_files_with_ns() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let filename_cc = dir.path().join("config_cc");
|
|
|
|
|
|
|
|
let mut file_cc = File::create(&filename_cc)?;
|
|
|
|
file_cc.write_all(
|
|
|
|
b"
|
|
|
|
apiVersion: v1
|
|
|
|
clusters: []
|
|
|
|
contexts: []
|
|
|
|
current-context: test_context
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users: []
|
|
|
|
",
|
|
|
|
)?;
|
|
|
|
file_cc.sync_all()?;
|
|
|
|
|
|
|
|
let filename_ctx = dir.path().join("config_ctx");
|
|
|
|
let mut file_ctx = File::create(&filename_ctx)?;
|
|
|
|
file_ctx.write_all(
|
|
|
|
b"
|
|
|
|
apiVersion: v1
|
|
|
|
clusters: []
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: test_cluster
|
|
|
|
user: test_user
|
|
|
|
namespace: test_namespace
|
|
|
|
name: test_context
|
|
|
|
kind: Config
|
|
|
|
preferences: {}
|
|
|
|
users: []
|
|
|
|
",
|
|
|
|
)?;
|
|
|
|
file_ctx.sync_all()?;
|
|
|
|
|
|
|
|
// Test current_context first
|
|
|
|
let actual_cc_first = ModuleRenderer::new("kubernetes")
|
|
|
|
.path(dir.path())
|
|
|
|
.env(
|
|
|
|
"KUBECONFIG",
|
|
|
|
env::join_paths([&filename_cc, &filename_ctx].iter())
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy(),
|
|
|
|
)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[kubernetes]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// And tes with context and namespace first
|
|
|
|
let actual_ctx_first = ModuleRenderer::new("kubernetes")
|
|
|
|
.path(dir.path())
|
|
|
|
.env(
|
|
|
|
"KUBECONFIG",
|
|
|
|
env::join_paths([&filename_ctx, &filename_cc].iter())
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy(),
|
|
|
|
)
|
|
|
|
.config(toml::toml! {
|
|
|
|
[kubernetes]
|
|
|
|
disabled = false
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
|
|
|
"{} in ",
|
|
|
|
Color::Cyan.bold().paint("☸ test_context (test_namespace)")
|
|
|
|
));
|
|
|
|
assert_eq!(expected, actual_cc_first);
|
|
|
|
assert_eq!(expected, actual_ctx_first);
|
|
|
|
|
|
|
|
dir.close()
|
2019-10-01 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|