feat: add network namespace module (#6449)

This commit is contained in:
Max Niederman
2025-03-09 08:45:58 -07:00
committed by GitHub
parent d6ed4c6192
commit eb42f5ac70
8 changed files with 218 additions and 0 deletions
+35
View File
@@ -1082,6 +1082,19 @@
} }
] ]
}, },
"netns": {
"default": {
"disabled": false,
"format": "[$symbol \\[$name\\]]($style) ",
"style": "blue bold dimmed",
"symbol": "🛜"
},
"allOf": [
{
"$ref": "#/definitions/NetnsConfig"
}
]
},
"nim": { "nim": {
"default": { "default": {
"detect_extensions": [ "detect_extensions": [
@@ -4601,6 +4614,28 @@
}, },
"additionalProperties": false "additionalProperties": false
}, },
"NetnsConfig": {
"type": "object",
"properties": {
"format": {
"default": "[$symbol \\[$name\\]]($style) ",
"type": "string"
},
"symbol": {
"default": "🛜",
"type": "string"
},
"style": {
"default": "blue bold dimmed",
"type": "string"
},
"disabled": {
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
},
"NimConfig": { "NimConfig": {
"type": "object", "type": "object",
"properties": { "properties": {
+33
View File
@@ -353,6 +353,7 @@ $time\
$status\ $status\
$os\ $os\
$container\ $container\
$netns\
$shell\ $shell\
$character""" $character"""
``` ```
@@ -3011,6 +3012,38 @@ format = '[$symbol]($style)'
style = 'bold purple' style = 'bold purple'
``` ```
## Network Namespace
The `netns` module shows the current network namespace.
This uses `ip netns identify` to get the network namespace, so only network namespaces mounted at `/var/run/netns` will be detected.
### Options
| Option | Default | Description |
| ---------- | ------------------------------- | ----------------------------------------------------------------- |
| `format` | `'[$symbol \[$name\]]($style)'` | The format for the module. |
| `symbol` | `'🛜 '` | The symbol used before the network namespace (defaults to empty). |
| `style` | `'blue bold dimmed'` | The style for the module. |
| `disabled` | `false` | Disables the `netns` module. |
### Variables
| Variable | Example | Description |
| -------- | ---------- | ----------------------------------------- |
| name | `my-netns` | The name of the current network namespace |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |
### Example
```toml
# ~/.config/starship.toml
[netns]
style = 'bold yellow'
symbol = '🌐 '
```
## Nim ## Nim
The `nim` module shows the currently installed version of [Nim](https://nim-lang.org/). The `nim` module shows the currently installed version of [Nim](https://nim-lang.org/).
+3
View File
@@ -57,6 +57,7 @@ pub mod memory_usage;
pub mod meson; pub mod meson;
pub mod mojo; pub mod mojo;
pub mod nats; pub mod nats;
pub mod netns;
pub mod nim; pub mod nim;
pub mod nix_shell; pub mod nix_shell;
pub mod nodejs; pub mod nodejs;
@@ -224,6 +225,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)] #[serde(borrow)]
nats: nats::NatsConfig<'a>, nats: nats::NatsConfig<'a>,
#[serde(borrow)] #[serde(borrow)]
netns: netns::NetnsConfig<'a>,
#[serde(borrow)]
nim: nim::NimConfig<'a>, nim: nim::NimConfig<'a>,
#[serde(borrow)] #[serde(borrow)]
nix_shell: nix_shell::NixShellConfig<'a>, nix_shell: nix_shell::NixShellConfig<'a>,
+26
View File
@@ -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 NetnsConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
}
impl Default for NetnsConfig<'_> {
fn default() -> Self {
NetnsConfig {
format: "[$symbol \\[$name\\]]($style) ",
symbol: "🛜",
style: "blue bold dimmed",
disabled: false,
}
}
}
+1
View File
@@ -125,6 +125,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"time", "time",
"status", "status",
"container", "container",
"netns",
"os", "os",
"shell", "shell",
"character", "character",
+1
View File
@@ -62,6 +62,7 @@ pub const ALL_MODULES: &[&str] = &[
"meson", "meson",
"mojo", "mojo",
"nats", "nats",
"netns",
"nim", "nim",
"nix_shell", "nix_shell",
"nodejs", "nodejs",
+3
View File
@@ -54,6 +54,7 @@ mod memory_usage;
mod meson; mod meson;
mod mojo; mod mojo;
mod nats; mod nats;
mod netns;
mod nim; mod nim;
mod nix_shell; mod nix_shell;
mod nodejs; mod nodejs;
@@ -166,6 +167,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"meson" => meson::module(context), "meson" => meson::module(context),
"mojo" => mojo::module(context), "mojo" => mojo::module(context),
"nats" => nats::module(context), "nats" => nats::module(context),
"netns" => netns::module(context),
"nim" => nim::module(context), "nim" => nim::module(context),
"nix_shell" => nix_shell::module(context), "nix_shell" => nix_shell::module(context),
"nodejs" => nodejs::module(context), "nodejs" => nodejs::module(context),
@@ -291,6 +293,7 @@ pub fn description(module: &str) -> &'static str {
} }
"mojo" => "The currently installed version of Mojo", "mojo" => "The currently installed version of Mojo",
"nats" => "The current NATS context", "nats" => "The current NATS context",
"netns" => "The current network namespace",
"nim" => "The currently installed version of Nim", "nim" => "The currently installed version of Nim",
"nix_shell" => "The nix-shell environment", "nix_shell" => "The nix-shell environment",
"nodejs" => "The currently installed version of NodeJS", "nodejs" => "The currently installed version of NodeJS",
+116
View File
@@ -0,0 +1,116 @@
use super::{Context, Module};
#[cfg(not(target_os = "linux"))]
pub fn module<'a>(_context: &'a Context) -> Option<Module<'a>> {
None
}
#[cfg(target_os = "linux")]
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
use crate::{config::ModuleConfig, configs::netns::NetnsConfig, formatter::StringFormatter};
fn netns_name(context: &Context) -> Option<String> {
context
.exec_cmd("ip", &["netns", "identify"])
.map(|output| output.stdout.trim().to_string())
.filter(|name| !name.is_empty())
}
let mut module = context.new_module("netns");
let config = NetnsConfig::try_load(module.config);
if config.disabled {
return None;
}
let netns_name = netns_name(context)?;
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"name" => Some(Ok(&netns_name)),
_ => None,
})
.parse(None, Some(context))
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `netns`: \n{}", error);
return None;
}
});
Some(module)
}
#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use crate::utils::CommandOutput;
use nu_ansi_term::Color;
fn mock_ip_netns_identify(netns_name: &str) -> Option<CommandOutput> {
Some(CommandOutput {
stdout: format!("{}\n", netns_name),
stderr: String::new(),
})
}
#[test]
fn test_none_if_disabled() {
let expected = None;
let actual = ModuleRenderer::new("netns")
.config(toml::toml! {
[netns]
disabled = true
})
.collect();
assert_eq!(expected, actual);
}
#[test]
#[cfg(target_os = "linux")]
fn test_netns_identify() {
let actual = ModuleRenderer::new("netns")
.config(toml::toml! {
[netns]
disabled = false
})
.cmd("ip netns identify", mock_ip_netns_identify("test_netns"))
.collect();
let expected = Some(format!(
"{} ",
Color::Blue.bold().dimmed().paint("🛜 [test_netns]")
));
assert_eq!(actual, expected);
}
#[test]
#[cfg(target_os = "linux")]
fn test_netns_identify_empty() {
let actual = ModuleRenderer::new("netns")
.config(toml::toml! {
[netns]
disabled = false
})
.cmd("ip netns identify", mock_ip_netns_identify(""))
.collect();
let expected = None;
assert_eq!(actual, expected);
}
}