feat: ContainerPorts use ipaddr

This commit is contained in:
Jack Wills
2024-12-03 20:39:51 +00:00
parent 508c697cf4
commit 1b26997d25
4 changed files with 29 additions and 25 deletions
+9 -6
View File
@@ -2,6 +2,7 @@ use std::{
cmp::Ordering, cmp::Ordering,
collections::{HashSet, VecDeque}, collections::{HashSet, VecDeque},
fmt, fmt,
net::IpAddr,
}; };
use bollard::service::Port; use bollard::service::Port;
@@ -103,9 +104,9 @@ macro_rules! unit_struct {
unit_struct!(ContainerName); unit_struct!(ContainerName);
unit_struct!(ContainerImage); unit_struct!(ContainerImage);
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContainerPorts { pub struct ContainerPorts {
pub ip: Option<String>, pub ip: Option<IpAddr>,
pub private: u16, pub private: u16,
pub public: Option<u16>, pub public: Option<u16>,
} }
@@ -113,7 +114,7 @@ pub struct ContainerPorts {
impl From<Port> for ContainerPorts { impl From<Port> for ContainerPorts {
fn from(value: Port) -> Self { fn from(value: Port) -> Self {
Self { Self {
ip: value.ip, ip: value.ip.and_then(|i| i.parse::<IpAddr>().ok()),
private: value.private_port, private: value.private_port,
public: value.public_port, public: value.public_port,
} }
@@ -122,7 +123,9 @@ impl From<Port> for ContainerPorts {
impl ContainerPorts { impl ContainerPorts {
pub fn len_ip(&self) -> usize { pub fn len_ip(&self) -> usize {
self.ip.as_ref().unwrap_or(&String::new()).chars().count() self.ip
.as_ref()
.map_or(0, |i|i.to_string().chars().count())
} }
pub fn len_private(&self) -> usize { pub fn len_private(&self) -> usize {
format!("{}", self.private).chars().count() format!("{}", self.private).chars().count()
@@ -134,11 +137,11 @@ impl ContainerPorts {
} }
/// Return as tuple of Strings, ip address, private port, and public port /// Return as tuple of Strings, ip address, private port, and public port
pub fn print(&self) -> (String, String, String) { pub fn get_all(&self) -> (String, String, String) {
( (
self.ip self.ip
.as_ref() .as_ref()
.map_or(String::new(), std::borrow::ToOwned::to_owned), .map_or(String::new(), std::string::ToString::to_string),
format!("{}", self.private), format!("{}", self.private),
self.public.map_or(String::new(), |s| s.to_string()), self.public.map_or(String::new(), |s| s.to_string()),
) )
+9 -12
View File
@@ -450,39 +450,36 @@ impl AppData {
} }
/// Find the longest port when it's transformed into a string, defaults are header lens (ip, private, public) /// Find the longest port when it's transformed into a string, defaults are header lens (ip, private, public)
/// TODO refactor this, and write comments as to whete the initial sizes come from ///display like this: "│ ip, private, public│", so (5,10,9) are the minimum lengths required
pub fn get_longest_port(&self) -> (usize, usize, usize) { pub fn get_longest_port(&self) -> (usize, usize, usize) {
let mut longest_ip = 5; let mut output = (5, 10, 9);
let mut longest_private = 10;
let mut longest_public = 9;
for item in [&self.containers.items, &self.hidden_containers] { for item in [&self.containers.items, &self.hidden_containers] {
for item in item { for item in item {
longest_ip = longest_ip.max( output.0 = output.0.max(
item.ports item.ports
.iter() .iter()
.map(ContainerPorts::len_ip) .map(ContainerPorts::len_ip)
.max() .max()
.unwrap_or(3), .unwrap_or(output.0),
); );
longest_private = longest_private.max( output.1 = output.1.max(
item.ports item.ports
.iter() .iter()
.map(ContainerPorts::len_private) .map(ContainerPorts::len_private)
.max() .max()
.unwrap_or(8), .unwrap_or(output.1),
); );
longest_public = longest_public.max( output.2 = output.2.max(
item.ports item.ports
.iter() .iter()
.map(ContainerPorts::len_public) .map(ContainerPorts::len_public)
.max() .max()
.unwrap_or(6), .unwrap_or(output.2),
); );
} }
} }
output
(longest_ip, longest_private, longest_public)
} }
/// Get Option of the current selected container's ports, sorted by private port /// Get Option of the current selected container's ports, sorted by private port
+1 -1
View File
@@ -292,7 +292,7 @@ impl DockerData {
GuiState::start_loading_animation(&self.gui_state, loading_uuid); GuiState::start_loading_animation(&self.gui_state, loading_uuid);
self.update_all_containers().await; self.update_all_containers().await;
let all_ids = self.app_data.lock().get_all_id_state(); let all_ids = self.app_data.lock().get_all_id_state();
let all_ids_len = all_ids.len(); let all_ids_len = all_ids.len();
let init = self.init_all_logs(all_ids); let init = self.init_all_logs(all_ids);
self.update_all_container_stats(); self.update_all_container_stats();
+10 -6
View File
@@ -330,7 +330,7 @@ pub fn ports(
)]; )];
for item in ports.0 { for item in ports.0 {
let fg = Color::White; let fg = Color::White;
let strings = item.print(); let strings = item.get_all();
let line = vec![ let line = vec![
Span::from(format!("{:>ip$}", strings.0)).fg(fg), Span::from(format!("{:>ip$}", strings.0)).fg(fg),
@@ -1066,7 +1066,11 @@ fn popup(text_lines: usize, text_width: usize, r: Rect, box_location: BoxLocatio
#[allow(clippy::unwrap_used)] #[allow(clippy::unwrap_used)]
mod tests { mod tests {
use std::{ops::RangeInclusive, sync::Arc}; use std::{
net::{IpAddr, Ipv4Addr},
ops::RangeInclusive,
sync::Arc,
};
use parking_lot::Mutex; use parking_lot::Mutex;
use ratatui::{ use ratatui::{
@@ -3171,7 +3175,7 @@ mod tests {
setup.app_data.lock().containers.items[0] setup.app_data.lock().containers.items[0]
.ports .ports
.push(ContainerPorts { .push(ContainerPorts {
ip: Some("127.0.0.1".to_owned()), ip: Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))),
private: 8003, private: 8003,
public: Some(8003), public: Some(8003),
}); });
@@ -3317,7 +3321,7 @@ mod tests {
setup.app_data.lock().containers.items[0] setup.app_data.lock().containers.items[0]
.ports .ports
.push(ContainerPorts { .push(ContainerPorts {
ip: Some("127.0.0.1".to_owned()), ip: Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))),
private: 8003, private: 8003,
public: Some(8003), public: Some(8003),
}); });
@@ -3381,7 +3385,7 @@ mod tests {
setup.app_data.lock().containers.items[1] setup.app_data.lock().containers.items[1]
.ports .ports
.push(ContainerPorts { .push(ContainerPorts {
ip: Some("127.0.0.1".to_owned()), ip: Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))),
private: 8003, private: 8003,
public: Some(8003), public: Some(8003),
}); });
@@ -3498,7 +3502,7 @@ mod tests {
setup.app_data.lock().containers.items[0] setup.app_data.lock().containers.items[0]
.ports .ports
.push(ContainerPorts { .push(ContainerPorts {
ip: Some("127.0.0.1".to_owned()), ip: Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))),
private: 8003, private: 8003,
public: Some(8003), public: Some(8003),
}); });