|
| 1 | +use ansi_term::Color; |
| 2 | +use dirs; |
| 3 | +use yaml_rust::YamlLoader; |
| 4 | + |
| 5 | +use std::env; |
| 6 | +use std::path; |
| 7 | + |
| 8 | +use super::{Context, Module}; |
| 9 | +use crate::utils; |
| 10 | + |
| 11 | +const KUBE_CHAR: &str = "☸ "; |
| 12 | + |
| 13 | +fn get_kube_context(contents: &str) -> Option<(String, String)> { |
| 14 | + let yaml_docs = YamlLoader::load_from_str(&contents).ok()?; |
| 15 | + if yaml_docs.is_empty() { |
| 16 | + return None; |
| 17 | + } |
| 18 | + let conf = &yaml_docs[0]; |
| 19 | + |
| 20 | + let current_ctx = conf["current-context"].as_str()?; |
| 21 | + |
| 22 | + if current_ctx.is_empty() { |
| 23 | + return None; |
| 24 | + } |
| 25 | + |
| 26 | + let ns = conf["contexts"] |
| 27 | + .as_vec() |
| 28 | + .and_then(|contexts| { |
| 29 | + contexts |
| 30 | + .iter() |
| 31 | + .filter_map(|ctx| Some((ctx, ctx["name"].as_str()?))) |
| 32 | + .find(|(_, name)| *name == current_ctx) |
| 33 | + .and_then(|(ctx, _)| ctx["context"]["namespace"].as_str()) |
| 34 | + }) |
| 35 | + .unwrap_or(""); |
| 36 | + |
| 37 | + Some((current_ctx.to_string(), ns.to_string())) |
| 38 | +} |
| 39 | + |
| 40 | +pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { |
| 41 | + let filename = match env::var("KUBECONFIG") { |
| 42 | + Ok(path) => path::PathBuf::from(path), |
| 43 | + Err(_) => dirs::home_dir()?.join(".kube").join("config"), |
| 44 | + }; |
| 45 | + |
| 46 | + let contents = utils::read_file(filename).ok()?; |
| 47 | + |
| 48 | + match get_kube_context(&contents) { |
| 49 | + Some(kube_cfg) => { |
| 50 | + let (kube_ctx, kube_ns) = kube_cfg; |
| 51 | + |
| 52 | + let mut module = context.new_module("kubernetes"); |
| 53 | + |
| 54 | + let module_style = module |
| 55 | + .config_value_style("style") |
| 56 | + .unwrap_or_else(|| Color::Cyan.bold()); |
| 57 | + module.set_style(module_style); |
| 58 | + module.get_prefix().set_value("on "); |
| 59 | + |
| 60 | + module.new_segment("symbol", KUBE_CHAR); |
| 61 | + module.new_segment("context", &kube_ctx); |
| 62 | + if kube_ns != "" { |
| 63 | + module.new_segment("namespace", &format!(" ({})", kube_ns)); |
| 64 | + } |
| 65 | + Some(module) |
| 66 | + } |
| 67 | + None => None, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests { |
| 73 | + use super::*; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn parse_empty_config() { |
| 77 | + let input = ""; |
| 78 | + let result = get_kube_context(&input); |
| 79 | + let expected = None; |
| 80 | + |
| 81 | + assert_eq!(result, expected); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn parse_no_config() { |
| 86 | + let input = r#" |
| 87 | +apiVersion: v1 |
| 88 | +clusters: [] |
| 89 | +contexts: [] |
| 90 | +current-context: "" |
| 91 | +kind: Config |
| 92 | +preferences: {} |
| 93 | +users: [] |
| 94 | +"#; |
| 95 | + let result = get_kube_context(&input); |
| 96 | + let expected = None; |
| 97 | + |
| 98 | + assert_eq!(result, expected); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn parse_only_context() { |
| 103 | + let input = r#" |
| 104 | +apiVersion: v1 |
| 105 | +clusters: [] |
| 106 | +contexts: |
| 107 | +- context: |
| 108 | + cluster: test_cluster |
| 109 | + user: test_user |
| 110 | + name: test_context |
| 111 | +current-context: test_context |
| 112 | +kind: Config |
| 113 | +preferences: {} |
| 114 | +users: [] |
| 115 | +"#; |
| 116 | + let result = get_kube_context(&input); |
| 117 | + let expected = Some(("test_context".to_string(), "".to_string())); |
| 118 | + |
| 119 | + assert_eq!(result, expected); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn parse_context_and_ns() { |
| 124 | + let input = r#" |
| 125 | +apiVersion: v1 |
| 126 | +clusters: [] |
| 127 | +contexts: |
| 128 | +- context: |
| 129 | + cluster: test_cluster |
| 130 | + user: test_user |
| 131 | + namespace: test_namespace |
| 132 | + name: test_context |
| 133 | +current-context: test_context |
| 134 | +kind: Config |
| 135 | +preferences: {} |
| 136 | +users: [] |
| 137 | +"#; |
| 138 | + let result = get_kube_context(&input); |
| 139 | + let expected = Some(("test_context".to_string(), "test_namespace".to_string())); |
| 140 | + |
| 141 | + assert_eq!(result, expected); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn parse_multiple_contexts() { |
| 146 | + let input = r#" |
| 147 | +apiVersion: v1 |
| 148 | +clusters: [] |
| 149 | +contexts: |
| 150 | +- context: |
| 151 | + cluster: another_cluster |
| 152 | + user: another_user |
| 153 | + namespace: another_namespace |
| 154 | + name: another_context |
| 155 | +- context: |
| 156 | + cluster: test_cluster |
| 157 | + user: test_user |
| 158 | + namespace: test_namespace |
| 159 | + name: test_context |
| 160 | +current-context: test_context |
| 161 | +kind: Config |
| 162 | +preferences: {} |
| 163 | +users: [] |
| 164 | +"#; |
| 165 | + let result = get_kube_context(&input); |
| 166 | + let expected = Some(("test_context".to_string(), "test_namespace".to_string())); |
| 167 | + |
| 168 | + assert_eq!(result, expected); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn parse_broken_config() { |
| 173 | + let input = r#" |
| 174 | +--- |
| 175 | +dummy_string |
| 176 | +"#; |
| 177 | + let result = get_kube_context(&input); |
| 178 | + let expected = None; |
| 179 | + |
| 180 | + assert_eq!(result, expected); |
| 181 | + } |
| 182 | +} |
0 commit comments