Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/config/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ struct ConfigWrapper<'a> {
struct InfoConfig<'a> {
#[serde(default, borrow)]
disks: Option<DisksInfoConfig<'a>>,
#[serde(default, borrow)]
networks: Option<NetworksInfoConfig<'a>>,
}

#[derive(Debug, Deserialize)]
struct DisksInfoConfig<'a> {
#[serde(default, borrow)]
exclude: Option<Vec<&'a str>>,
#[serde(default, borrow)]
include: Option<Vec<&'a str>>,
}

#[derive(Debug, Deserialize)]
struct NetworksInfoConfig<'a> {
#[serde(default, borrow)]
exclude: Option<Vec<&'a str>>,
#[serde(default, borrow)]
include: Option<Vec<&'a str>>,
#[serde(default)]
private_only: Option<bool>,
#[serde(default)]
assigned_only: Option<bool>,
#[serde(default)]
ignore_loopback: Option<bool>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -212,6 +230,17 @@ impl<'de: 'static> serde::Deserialize<'de> for super::Config {
.disks
.map(|disks| super::DisksInfoConfig {
exclude: disks.exclude.unwrap_or_default(),
include: disks.include,
})
.unwrap_or_default(),
networks: info
.networks
.map(|networks| super::NetworksInfoConfig {
exclude: networks.exclude.unwrap_or_default(),
include: networks.include,
private_only: networks.private_only.unwrap_or(true),
assigned_only: networks.assigned_only.unwrap_or(true),
ignore_loopback: networks.ignore_loopback.unwrap_or(true),
})
.unwrap_or_default(),
})
Expand Down
26 changes: 26 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,47 @@ pub struct Config {
#[derive(Debug, Default, Decode, Encode)]
pub struct InfoConfig<'a> {
pub disks: DisksInfoConfig<'a>,
pub networks: NetworksInfoConfig<'a>,
}

#[derive(Debug, Decode, Encode)]
pub struct DisksInfoConfig<'a> {
pub exclude: Vec<&'a str>,
pub include: Option<Vec<&'a str>>,
}

#[derive(Debug, Decode, Encode)]
pub struct NetworksInfoConfig<'a> {
pub exclude: Vec<&'a str>,
pub include: Option<Vec<&'a str>>,
pub private_only: bool,
pub assigned_only: bool,
pub ignore_loopback: bool,
}

impl<'a> Default for DisksInfoConfig<'a> {
fn default() -> Self {
Self {
include: None,
exclude: vec!["/boot", "/etc", "/snapd", "/docker"],
}
}
}

impl<'a> Default for NetworksInfoConfig<'a> {
fn default() -> Self {
Self {
include: None,
exclude: vec![
"br-", "docker", "veth", "tun", "tap", "wg", "virbr", "vmnet",
],
private_only: true,
assigned_only: true,
ignore_loopback: true,
}
}
}

#[derive(Debug, Decode, Encode)]
pub struct ColorOption {
pub header: Option<ColorWrapper>,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use afetch::system::kernel::get_kernel;
use afetch::system::loadavg::get_loadavg;
use afetch::system::memory::get_memory;
use afetch::system::motherboard::get_motherboard;
use afetch::system::networks::get_networks;
use afetch::system::product::get_product;
use afetch::system::uptime::get_uptime;
use afetch::system::{InfoGroup, InfoKind, InfoResult};
Expand Down Expand Up @@ -44,6 +45,7 @@ fn main() -> Result<(), FetchInfoError> {
InfoKind::Loadavg => get_loadavg(language_func, fields, &config),
InfoKind::Memory => get_memory(language_func, fields, &config),
InfoKind::Motherboard => get_motherboard(language_func, fields, &config),
InfoKind::Networks => get_networks(language_func, fields, &config),
InfoKind::Product => get_product(language_func, fields, &config),
InfoKind::Uptime => get_uptime(language_func, fields, &config),
},
Expand Down
9 changes: 2 additions & 7 deletions src/system/disk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::Config;
use crate::error::FetchInfoError;
use crate::filtered_values;
use crate::system::disks::ignore_disk;
use crate::system::{InfoField, InfoGroup, InfoResult, InfoValue};
use crate::util::ToOptionString;
use crate::util::convert_to_readable_unity;
Expand All @@ -16,13 +17,7 @@ pub fn get_disk(
for disk in Disks::new_with_refreshed_list().list() {
let mount_point = disk.mount_point().to_string_lossy().to_string();

if config
.parameters
.disks
.exclude
.iter()
.any(|ignore| mount_point.starts_with(ignore))
{
if ignore_disk(config, &mount_point) {
continue;
}

Expand Down
32 changes: 24 additions & 8 deletions src/system/disks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,29 @@ pub fn get_disks(
let mut available_space = 0;
let mut total_space = 0;
let mut count = 0;
let mut count_filtered = 0;

for disk in
Disks::new_with_refreshed_list_specifics(DiskRefreshKind::nothing().with_storage()).list()
{
let mount_point = disk.mount_point().to_string_lossy().to_string();
count += 1;

if config
.parameters
.disks
.exclude
.iter()
.any(|ignore| mount_point.starts_with(ignore))
{
if ignore_disk(config, &mount_point) {
continue;
}

available_space += disk.available_space();
total_space += disk.total_space();
count += 1;
count_filtered += 1;
}

Ok(InfoResult::Single(InfoGroup {
values: filtered_values!(
fields,
[
(InfoField::DisksCount, count.to_string()),
(InfoField::DisksCountFiltered, count_filtered.to_string()),
(
InfoField::DisksAvailableSpace,
convert_to_readable_unity(available_space as f64)
Expand All @@ -56,3 +53,22 @@ pub fn get_disks(
),
}))
}

pub(crate) fn ignore_disk(config: &Config, mount_point: &str) -> bool {
config
.parameters
.disks
.exclude
.iter()
.any(|ignore| mount_point.starts_with(ignore))
|| config
.parameters
.disks
.include
.as_ref()
.is_some_and(|include| {
!include
.iter()
.any(|&include| mount_point.starts_with(include))
})
}
48 changes: 48 additions & 0 deletions src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod kernel;
pub mod loadavg;
pub mod memory;
pub mod motherboard;
pub mod networks;
pub mod product;
pub mod uptime;

Expand All @@ -30,6 +31,7 @@ pub enum InfoKind {
Loadavg,
Memory,
Motherboard,
Networks,
Product,
Uptime,
}
Expand Down Expand Up @@ -77,6 +79,7 @@ impl InfoKind {
],
Self::Disks => &[
InfoField::DisksCount,
InfoField::DisksCountFiltered,
InfoField::DisksAvailableSpace,
InfoField::DisksUsedSpace,
InfoField::DisksTotalSpace,
Expand Down Expand Up @@ -104,6 +107,21 @@ impl InfoKind {
InfoField::MotherboardVendorName,
InfoField::MotherboardVersion,
],
Self::Networks => &[
InfoField::NetworkName,
InfoField::NetworkFirstIp,
InfoField::NetworkPreferFirstIpv4,
InfoField::NetworkPreferFirstIpv6,
InfoField::NetworkAllIp,
InfoField::NetworkMacAddress,
InfoField::NetworkMaximumTransferUnit,
InfoField::NetworkErrorsOnReceived,
InfoField::NetworkErrorsOnTransmitted,
InfoField::NetworkPacketsReceived,
InfoField::NetworkPacketsTransmitted,
InfoField::NetworkReceived,
InfoField::NetworkTransmitted,
],
Self::Product => &[
InfoField::ProductFamily,
InfoField::ProductName,
Expand All @@ -128,6 +146,7 @@ impl InfoKind {
Self::Loadavg => "{loadavg_one}, {loadavg_five}, {loadavg_fifteen}",
Self::Memory => "{memory_used} / {memory_total}",
Self::Motherboard => "{motherboard_name} {motherboard_version}",
Self::Networks => "{network_prefer_first_ipv4}",
Self::Product => "{product_name} {product_version}",
Self::Uptime => "{uptime}",
}
Expand All @@ -144,6 +163,7 @@ impl InfoKind {
Self::Loadavg => "loadavg",
Self::Memory => "memory",
Self::Motherboard => "motherboard",
Self::Networks => "networks",
Self::Product => "host",
Self::Uptime => "uptime",
}
Expand Down Expand Up @@ -185,6 +205,7 @@ pub enum InfoField {
DiskWrittenSinceBoot,
DiskReadSinceBoot,
DisksCount,
DisksCountFiltered,
DisksAvailableSpace,
DisksUsedSpace,
DisksTotalSpace,
Expand All @@ -206,6 +227,19 @@ pub enum InfoField {
MotherboardSerialNumber,
MotherboardVendorName,
MotherboardVersion,
NetworkName,
NetworkFirstIp,
NetworkPreferFirstIpv4,
NetworkPreferFirstIpv6,
NetworkAllIp,
NetworkMacAddress,
NetworkMaximumTransferUnit,
NetworkErrorsOnReceived,
NetworkErrorsOnTransmitted,
NetworkPacketsReceived,
NetworkPacketsTransmitted,
NetworkReceived,
NetworkTransmitted,
ProductFamily,
ProductName,
ProductSerialNumber,
Expand Down Expand Up @@ -253,6 +287,7 @@ impl InfoField {
Self::DiskWrittenSinceBoot => "disk_written_since_boot",
Self::DiskReadSinceBoot => "disk_read_since_boot",
Self::DisksCount => "disks_count",
Self::DisksCountFiltered => "disks_count_filtered",
Self::DisksAvailableSpace => "disks_available_space",
Self::DisksUsedSpace => "disks_used_space",
Self::DisksTotalSpace => "disks_total_space",
Expand All @@ -274,6 +309,19 @@ impl InfoField {
Self::MotherboardSerialNumber => "motherboard_serial_number",
Self::MotherboardVendorName => "motherboard_vendor_name",
Self::MotherboardVersion => "motherboard_version",
Self::NetworkName => "network_name",
Self::NetworkPreferFirstIpv4 => "network_prefer_first_ipv4",
Self::NetworkPreferFirstIpv6 => "network_prefer_first_ipv6",
Self::NetworkFirstIp => "network_first_ip",
Self::NetworkAllIp => "network_all_ip",
Self::NetworkMacAddress => "network_mac_address",
Self::NetworkMaximumTransferUnit => "network_maximum_transfer_unit",
Self::NetworkErrorsOnReceived => "network_errors_on_received",
Self::NetworkErrorsOnTransmitted => "network_errors_on_transmitted",
Self::NetworkPacketsReceived => "network_packets_received",
Self::NetworkPacketsTransmitted => "network_packets_transmitted",
Self::NetworkReceived => "network_received",
Self::NetworkTransmitted => "network_transmitted",
Self::ProductFamily => "product_family",
Self::ProductName => "product_name",
Self::ProductSerialNumber => "product_serial_number",
Expand Down
Loading
Loading