feat(k8s): Add detect env vars option (#4488)
* feat(k8s): Add detect env vars option Have added the option to trigger the k8s module based on what env vars are set, this has been done in a backwards compatible way so if nothing is changed from the defaults the module will still behave the same way as before. This is similar to what I did in #4486 for the python module and if goes well I'd like to rollout to other modules. * Update src/modules/kubernetes.rs Co-authored-by: David Knaack <davidkna@users.noreply.github.com> * Update src/modules/kubernetes.rs --------- Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
parent
3e3f18ef27
commit
e3b5dff352
|
@ -952,6 +952,7 @@
|
||||||
"default": {
|
"default": {
|
||||||
"context_aliases": {},
|
"context_aliases": {},
|
||||||
"contexts": [],
|
"contexts": [],
|
||||||
|
"detect_env_vars": [],
|
||||||
"detect_extensions": [],
|
"detect_extensions": [],
|
||||||
"detect_files": [],
|
"detect_files": [],
|
||||||
"detect_folders": [],
|
"detect_folders": [],
|
||||||
|
@ -4256,6 +4257,13 @@
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"detect_env_vars": {
|
||||||
|
"default": [],
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
"contexts": {
|
"contexts": {
|
||||||
"default": [],
|
"default": [],
|
||||||
"type": "array",
|
"type": "array",
|
||||||
|
|
|
@ -2601,8 +2601,9 @@ This module is disabled by default.
|
||||||
To enable it, set `disabled` to `false` in your configuration file.
|
To enable it, set `disabled` to `false` in your configuration file.
|
||||||
|
|
||||||
When the module is enabled it will always be active, unless any of
|
When the module is enabled it will always be active, unless any of
|
||||||
`detect_extensions`, `detect_files` or `detect_folders` have been set in which
|
`detect_env_vars`, `detect_extensions`, `detect_files` or `detect_folders` have
|
||||||
case the module will only be active in directories that match those conditions.
|
been set in which case the module will only be active in directories that match
|
||||||
|
those conditions or one of the environmatal variable has been set.
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
@ -2625,6 +2626,7 @@ and `user_alias` options instead.
|
||||||
| `detect_extensions` | `[]` | Which extensions should trigger this module. |
|
| `detect_extensions` | `[]` | Which extensions should trigger this module. |
|
||||||
| `detect_files` | `[]` | Which filenames should trigger this module. |
|
| `detect_files` | `[]` | Which filenames should trigger this module. |
|
||||||
| `detect_folders` | `[]` | Which folders should trigger this modules. |
|
| `detect_folders` | `[]` | Which folders should trigger this modules. |
|
||||||
|
| `detect_env_vars` | `[]` | Which environmental variables should trigger this module |
|
||||||
| `contexts` | `[]` | Customized styles and symbols for specific contexts. |
|
| `contexts` | `[]` | Customized styles and symbols for specific contexts. |
|
||||||
| `disabled` | `true` | Disables the `kubernetes` module. |
|
| `disabled` | `true` | Disables the `kubernetes` module. |
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ pub struct KubernetesConfig<'a> {
|
||||||
pub detect_extensions: Vec<&'a str>,
|
pub detect_extensions: Vec<&'a str>,
|
||||||
pub detect_files: Vec<&'a str>,
|
pub detect_files: Vec<&'a str>,
|
||||||
pub detect_folders: Vec<&'a str>,
|
pub detect_folders: Vec<&'a str>,
|
||||||
|
pub detect_env_vars: Vec<&'a str>,
|
||||||
pub contexts: Vec<KubernetesContextConfig<'a>>,
|
pub contexts: Vec<KubernetesContextConfig<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ impl<'a> Default for KubernetesConfig<'a> {
|
||||||
detect_extensions: vec![],
|
detect_extensions: vec![],
|
||||||
detect_files: vec![],
|
detect_files: vec![],
|
||||||
detect_folders: vec![],
|
detect_folders: vec![],
|
||||||
|
detect_env_vars: vec![],
|
||||||
contexts: vec![],
|
contexts: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,6 +107,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let have_env_vars = context.detect_env_vars(&config.detect_env_vars);
|
||||||
|
|
||||||
// If we have some config for doing the directory scan then we use it but if we don't then we
|
// If we have some config for doing the directory scan then we use it but if we don't then we
|
||||||
// assume we should treat it like the module is enabled to preserve backward compatibility.
|
// assume we should treat it like the module is enabled to preserve backward compatibility.
|
||||||
let have_scan_config = [
|
let have_scan_config = [
|
||||||
|
@ -127,7 +129,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
if !is_kube_project.unwrap_or(true) {
|
if !is_kube_project.unwrap_or(true) && !have_env_vars {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,7 +322,7 @@ users: []
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_none_when_no_detected_files_or_folders() -> io::Result<()> {
|
fn test_none_when_no_detected_files_folders_or_env_vars() -> io::Result<()> {
|
||||||
let dir = tempfile::tempdir()?;
|
let dir = tempfile::tempdir()?;
|
||||||
|
|
||||||
let filename = dir.path().join("config");
|
let filename = dir.path().join("config");
|
||||||
|
@ -352,6 +354,7 @@ users: []
|
||||||
detect_files = ["k8s.ext"]
|
detect_files = ["k8s.ext"]
|
||||||
detect_extensions = ["k8s"]
|
detect_extensions = ["k8s"]
|
||||||
detect_folders = ["k8s_folder"]
|
detect_folders = ["k8s_folder"]
|
||||||
|
detect_env_vars = ["k8s_env_var"]
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -361,7 +364,7 @@ users: []
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_with_detected_files_and_folder() -> io::Result<()> {
|
fn test_with_detected_files_folder_and_env_vars() -> io::Result<()> {
|
||||||
let dir = tempfile::tempdir()?;
|
let dir = tempfile::tempdir()?;
|
||||||
|
|
||||||
let filename = dir.path().join("config");
|
let filename = dir.path().join("config");
|
||||||
|
@ -429,6 +432,19 @@ users: []
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let empty_dir = tempfile::tempdir()?;
|
||||||
|
|
||||||
|
let actual_env_var = ModuleRenderer::new("kubernetes")
|
||||||
|
.path(empty_dir.path())
|
||||||
|
.env("KUBECONFIG", filename.to_string_lossy().as_ref())
|
||||||
|
.env("TEST_K8S_ENV", "foo")
|
||||||
|
.config(toml::toml! {
|
||||||
|
[kubernetes]
|
||||||
|
disabled = false
|
||||||
|
detect_env_vars = ["TEST_K8S_ENV"]
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let expected = Some(format!(
|
let expected = Some(format!(
|
||||||
"{} in ",
|
"{} in ",
|
||||||
Color::Cyan.bold().paint("☸ test_context")
|
Color::Cyan.bold().paint("☸ test_context")
|
||||||
|
@ -437,6 +453,7 @@ users: []
|
||||||
assert_eq!(expected, actual_file);
|
assert_eq!(expected, actual_file);
|
||||||
assert_eq!(expected, actual_ext);
|
assert_eq!(expected, actual_ext);
|
||||||
assert_eq!(expected, actual_dir);
|
assert_eq!(expected, actual_dir);
|
||||||
|
assert_eq!(expected, actual_env_var);
|
||||||
|
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue