-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nats): Add NATS Context (#5900)
--------- Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
- Loading branch information
Showing
10 changed files
with
195 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,9 @@ symbol = "memory " | |
[meson] | ||
symbol = "meson " | ||
|
||
[nats] | ||
symbol = "nats " | ||
|
||
[nim] | ||
symbol = "nim " | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Deserialize, Serialize)] | ||
#[cfg_attr( | ||
feature = "config-schema", | ||
derive(schemars::JsonSchema), | ||
schemars(deny_unknown_fields) | ||
)] | ||
#[serde(default)] | ||
pub struct NatsConfig<'a> { | ||
pub format: &'a str, | ||
pub symbol: &'a str, | ||
pub style: &'a str, | ||
pub disabled: bool, | ||
} | ||
|
||
impl<'a> Default for NatsConfig<'a> { | ||
fn default() -> Self { | ||
NatsConfig { | ||
format: "[$symbol($name )]($style)", | ||
symbol: "✉️ ", | ||
style: "bold purple", | ||
disabled: true, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use super::{Context, Module, ModuleConfig}; | ||
use serde_json as json; | ||
|
||
use crate::configs::nats::NatsConfig; | ||
use crate::formatter::StringFormatter; | ||
|
||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { | ||
let mut module = context.new_module("nats"); | ||
let config = NatsConfig::try_load(module.config); | ||
|
||
if config.disabled { | ||
return None; | ||
}; | ||
|
||
let ctx_str = context | ||
.exec_cmd("nats", &["context", "info", "--json"])? | ||
.stdout; | ||
let nats_context: json::Value = json::from_str(&ctx_str) | ||
.map_err(|e| { | ||
log::warn!("Error parsing nats context JSON: {}\n", e); | ||
drop(e); | ||
}) | ||
.ok()?; | ||
|
||
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 { | ||
"name" => Some(Ok(nats_context.get("name")?.as_str()?)), | ||
_ => None, | ||
}) | ||
.parse(None, Some(context)) | ||
}); | ||
|
||
module.set_segments(match parsed { | ||
Ok(segments) => segments, | ||
Err(error) => { | ||
log::warn!("Error in module `nats`:\n{}", error); | ||
return None; | ||
} | ||
}); | ||
|
||
Some(module) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use nu_ansi_term::Color; | ||
use std::io; | ||
|
||
use crate::test::ModuleRenderer; | ||
|
||
#[test] | ||
fn show_context() -> io::Result<()> { | ||
let actual = ModuleRenderer::new("nats") | ||
.config(toml::toml! { | ||
[nats] | ||
format = "[$symbol$name](bold purple)" | ||
symbol = "" | ||
disabled = false | ||
}) | ||
.collect(); | ||
let expected = Some(format!("{}", Color::Purple.bold().paint("localhost"))); | ||
assert_eq!(expected, actual); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_with_symbol() -> io::Result<()> { | ||
let actual = ModuleRenderer::new("nats") | ||
.config(toml::toml! { | ||
[nats] | ||
format = "[$symbol$name](bold red)" | ||
symbol = "✉️ " | ||
disabled = false | ||
}) | ||
.collect(); | ||
let expected = Some(format!("{}", Color::Red.bold().paint("✉️ localhost"))); | ||
assert_eq!(expected, actual); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters