From 03d75eb4198fa9b264921fe703587192cd91e3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Apayd=C4=B1n?= Date: Fri, 17 Jul 2020 10:51:25 +0300 Subject: [PATCH] feat(helm): Add Helm module (#1463) Signed-off-by: Furkan Co-authored-by: Furkan --- docs/config/README.md | 37 ++++++++++ src/configs/helm.rs | 22 ++++++ src/configs/mod.rs | 1 + src/configs/starship_root.rs | 3 +- src/module.rs | 1 + src/modules/helm.rs | 127 +++++++++++++++++++++++++++++++++++ src/modules/mod.rs | 3 + src/utils.rs | 4 ++ 8 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 src/configs/helm.rs create mode 100644 src/modules/helm.rs diff --git a/docs/config/README.md b/docs/config/README.md index 393bae4c..745cb5f1 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -193,6 +193,7 @@ $elixir\ $elm\ $erlang\ $golang\ +$helm\ $java\ $julia\ $nim\ @@ -1116,6 +1117,42 @@ The module will be shown if any of the following conditions are met: format = "via [🏎💨 $version](bold cyan) " ``` +## Helm + +The `helm` module shows the currently installed version of Helm. +The module will be shown if any of the following conditions are met: + +- The current directory contains a `helmfile.yaml` file +- The current directory contains a `Chart.yaml` file + +### Options + +| Option | Default | Description | +| ---------- | ---------------------------------- | ---------------------------------------------- | +| `format` | `"via [$symbol$version]($style) "` | The format for the module. | +| `symbol` | `"⎈ "` | A format string representing the symbol of Helm. | +| `style` | `"bold white"` | The style for the module. | +| `disabled` | `false` | Disables the `helm` module. | + +### Variables + +| Variable | Example | Description | +| -------- | --------- | ------------------------------------ | +| version | `v3.1.1` | The version of `helm` | +| symbol | | Mirrors the value of option `symbol` | +| style\* | | Mirrors the value of option `style` | + +\*: This variable can only be used as a part of a style string + +### Example + +```toml +# ~/.config/starship.toml + +[helm] +format = "via [⎈ $version](bold white) " +``` + ## Hostname The `hostname` module shows the system hostname. diff --git a/src/configs/helm.rs b/src/configs/helm.rs new file mode 100644 index 00000000..740bdc07 --- /dev/null +++ b/src/configs/helm.rs @@ -0,0 +1,22 @@ +use crate::config::{ModuleConfig, RootModuleConfig}; + +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct HelmConfig<'a> { + pub format: &'a str, + pub symbol: &'a str, + pub style: &'a str, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for HelmConfig<'a> { + fn new() -> Self { + HelmConfig { + format: "via [$symbol$version]($style) ", + symbol: "⎈ ", + style: "bold white", + disabled: false, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 4b871288..4ccd76f7 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -18,6 +18,7 @@ pub mod git_commit; pub mod git_state; pub mod git_status; pub mod go; +pub mod helm; pub mod hg_branch; pub mod hostname; pub mod java; diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs index 80393792..5054f7a2 100644 --- a/src/configs/starship_root.rs +++ b/src/configs/starship_root.rs @@ -11,7 +11,7 @@ pub struct StarshipRootConfig<'a> { // List of default prompt order // NOTE: If this const value is changed then Default prompt order subheading inside // prompt heading of config docs needs to be updated according to changes made here. -pub const PROMPT_ORDER: [&str; 43] = [ +pub const PROMPT_ORDER: [&str; 44] = [ "username", "hostname", "singularity", @@ -32,6 +32,7 @@ pub const PROMPT_ORDER: [&str; 43] = [ "elm", "erlang", "golang", + "helm", "java", "julia", "nim", diff --git a/src/module.rs b/src/module.rs index eda8ba99..7293c17e 100644 --- a/src/module.rs +++ b/src/module.rs @@ -27,6 +27,7 @@ pub const ALL_MODULES: &[&str] = &[ "git_state", "git_status", "golang", + "helm", "hg_branch", "hostname", "java", diff --git a/src/modules/helm.rs b/src/modules/helm.rs new file mode 100644 index 00000000..d14c8757 --- /dev/null +++ b/src/modules/helm.rs @@ -0,0 +1,127 @@ +use super::{Context, Module, RootModuleConfig}; + +use crate::configs::helm::HelmConfig; +use crate::formatter::StringFormatter; +use crate::utils; + +/// Creates a module with the current Helm version +/// +/// Will display the Helm version if any of the following criteria are met: +/// - Current directory contains a `helmfile.yaml` file +/// - Current directory contains a `Chart.yaml` file +pub fn module<'a>(context: &'a Context) -> Option> { + let is_helm_project = context + .try_begin_scan()? + .set_files(&["helmfile.yaml", "Chart.yaml"]) + .is_match(); + + if !is_helm_project { + return None; + } + + let mut module = context.new_module("helm"); + let config = HelmConfig::try_load(module.config); + let parsed = StringFormatter::new(config.format).and_then(|formatter| { + formatter + .map_meta(|var, _| match var { + "symbol" => Some(config.symbol), + _ => None, + }) + .map_style(|variable| match variable { + "style" => Some(Ok(config.style)), + _ => None, + }) + .map(|variable| match variable { + "version" => format_helm_version( + &utils::exec_cmd("helm", &["version", "--short", "--client"])? + .stdout + .as_str(), + ) + .map(Ok), + _ => None, + }) + .parse(None) + }); + + module.set_segments(match parsed { + Ok(segments) => segments, + Err(error) => { + log::warn!("Error in module `helm`:\n{}", error); + return None; + } + }); + + Some(module) +} + +fn format_helm_version(helm_stdout: &str) -> Option { + // `helm version --short --client` output looks like this: + // v3.1.1+gafe7058 + // `helm version --short --client` output looks like this for Helm 2: + // Client: v2.16.9+g8ad7037 + + Some( + helm_stdout + // split into ["v3.1.1","gafe7058"] or ["Client: v3.1.1","gafe7058"] + .splitn(2, '+') + // return "v3.1.1" or "Client: v3.1.1" + .next()? + // return "v3.1.1" or " v3.1.1" + .trim_start_matches("Client: ") + // return "v3.1.1" + .trim() + .to_owned(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::modules::utils::test::render_module; + use ansi_term::Color; + use std::fs::File; + use std::io; + + #[test] + fn folder_without_helm_files() -> io::Result<()> { + let dir = tempfile::tempdir()?; + + let actual = render_module("helm", dir.path(), None); + + let expected = None; + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_helm_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("helmfile.yaml"))?.sync_all()?; + + let actual = render_module("helm", dir.path(), None); + + let expected = Some(format!("via {} ", Color::White.bold().paint("⎈ v3.1.1"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn folder_with_chart_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("Chart.yaml"))?.sync_all()?; + + let actual = render_module("helm", dir.path(), None); + + let expected = Some(format!("via {} ", Color::White.bold().paint("⎈ v3.1.1"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn test_format_helm_version() { + let helm_2 = "Client: v2.16.9+g8ad7037"; + let helm_3 = "v3.1.1+ggit afe7058"; + assert_eq!(format_helm_version(helm_2), Some("v2.16.9".to_string())); + assert_eq!(format_helm_version(helm_3), Some("v3.1.1".to_string())); + } +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index a7d059a9..ed171339 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -18,6 +18,7 @@ mod git_commit; mod git_state; mod git_status; mod golang; +mod helm; mod hg_branch; mod hostname; mod java; @@ -73,6 +74,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "git_state" => git_state::module(context), "git_status" => git_status::module(context), "golang" => golang::module(context), + "helm" => helm::module(context), "hg_branch" => hg_branch::module(context), "hostname" => hostname::module(context), "java" => java::module(context), @@ -125,6 +127,7 @@ pub fn description(module: &str) -> &'static str { "git_state" => "The current git operation, and it's progress", "git_status" => "Symbol representing the state of the repo", "golang" => "The currently installed version of Golang", + "helm" => "The currently installed version of Helm", "hg_branch" => "The active branch of the repo in your current directory", "hostname" => "The system hostname", "java" => "The currently installed version of Java", diff --git a/src/utils.rs b/src/utils.rs index 945c6088..58595ddc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -64,6 +64,10 @@ Elixir 1.10 (compiled with Erlang/OTP 22)", stdout: String::from("go version go1.12.1 linux/amd64"), stderr: String::default(), }), + "helm version --short --client" => Some(CommandOutput { + stdout: String::from("v3.1.1+gafe7058"), + stderr: String::default(), + }), "julia --version" => Some(CommandOutput { stdout: String::from("julia version 1.4.0"), stderr: String::default(),