feat(helm): Add Helm module (#1463)

Signed-off-by: Furkan <furkan.turkal@hotmail.com>

Co-authored-by: Furkan <furkan.turkal@hotmail.com>
This commit is contained in:
Batuhan Apaydın 2020-07-17 10:51:25 +03:00 committed by GitHub
parent 7b7f2707dd
commit 03d75eb419
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 197 additions and 1 deletions

View File

@ -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.

22
src/configs/helm.rs Normal file
View File

@ -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,
}
}
}

View File

@ -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;

View File

@ -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",

View File

@ -27,6 +27,7 @@ pub const ALL_MODULES: &[&str] = &[
"git_state",
"git_status",
"golang",
"helm",
"hg_branch",
"hostname",
"java",

127
src/modules/helm.rs Normal file
View File

@ -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<Module<'a>> {
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<String> {
// `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()));
}
}

View File

@ -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<Module<'a>> {
"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",

View File

@ -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(),