actually display hosts table
/ build (push) Successful in 49s Details
/ build_x64 (push) Successful in 2m12s Details
/ build_arm64 (push) Successful in 2m40s Details
/ build_win64 (push) Successful in 2m36s Details

This commit is contained in:
core 2023-11-25 15:24:58 -05:00
parent 74f045ed62
commit 2045d7b636
Signed by: core
GPG Key ID: FDBF740DADDCEECF
4 changed files with 70 additions and 43 deletions

2
Cargo.lock generated
View File

@ -2721,7 +2721,7 @@ dependencies = [
[[package]] [[package]]
name = "tfcli" name = "tfcli"
version = "0.2.1" version = "0.3.0"
dependencies = [ dependencies = [
"clap", "clap",
"comfy-table", "comfy-table",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "tfcli" name = "tfcli"
version = "0.3.0" version = "0.3.1"
edition = "2021" edition = "2021"
description = "Command-line client for managing trifid-api" description = "Command-line client for managing trifid-api"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"

View File

@ -1,14 +1,17 @@
use crate::api::APIErrorResponse; use crate::api::APIErrorResponse;
use crate::{HostCommands, HostOverrideCommands}; use crate::{HostCommands, HostOverrideCommands, TableStyle};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::error::Error; use std::error::Error;
use std::fs; use std::fs;
use std::net::{Ipv4Addr, SocketAddrV4}; use std::net::{Ipv4Addr, SocketAddrV4};
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
use comfy_table::presets::UTF8_FULL;
use comfy_table::Table;
use url::Url; use url::Url;
pub async fn host_main(command: HostCommands, server: Url) -> Result<(), Box<dyn Error>> { pub async fn host_main(command: HostCommands, server: Url) -> Result<(), Box<dyn Error>> {
match command { match command {
HostCommands::List {} => list_hosts(server).await, HostCommands::List { table_style } => list_hosts(server, table_style).await,
HostCommands::Create { HostCommands::Create {
name, name,
network_id, network_id,
@ -100,7 +103,7 @@ pub struct Host {
pub metadata: HostMetadata, pub metadata: HostMetadata,
} }
pub async fn list_hosts(server: Url) -> Result<(), Box<dyn Error>> { pub async fn list_hosts(server: Url, table_style: TableStyle) -> Result<(), Box<dyn Error>> {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
// load session token // load session token
@ -120,46 +123,67 @@ pub async fn list_hosts(server: Url) -> Result<(), Box<dyn Error>> {
if res.status().is_success() { if res.status().is_success() {
let resp: HostListResp = res.json().await?; let resp: HostListResp = res.json().await?;
for host in &resp.data {
println!(" Host: {} ({})", host.name, host.id);
println!(" Organization: {}", host.organization_id);
println!(" Network: {}", host.network_id);
println!(" Role: {}", host.role_id);
println!(" IP Address: {}", host.ip_address);
println!(
" Static Addresses: {}",
host.static_addresses
.iter()
.map(|u| u.to_string())
.collect::<Vec<_>>()
.join(", ")
);
println!(" Listen Port: {}", host.listen_port);
println!(
" Type: {}",
if host.is_lighthouse {
"Lighthouse"
} else if host.is_relay {
"Relay"
} else {
"Host"
}
);
println!(" Blocked: {}", host.is_blocked);
println!(" Last Seen: {}", host.metadata.last_seen_at);
println!(" Client Version: {}", host.metadata.version);
println!(" Platform: {}", host.metadata.platform);
println!(
"Client Update Available: {}",
host.metadata.update_available
);
println!(" Created: {}", host.created_at);
println!();
}
if resp.data.is_empty() { if resp.data.is_empty() {
println!("No hosts found"); println!("No hosts found");
return Ok(());
} }
if matches!(table_style, TableStyle::List) {
for host in &resp.data {
println!(" Host: {} ({})", host.name, host.id);
println!(" Organization: {}", host.organization_id);
println!(" Network: {}", host.network_id);
println!(" Role: {}", host.role_id);
println!(" IP Address: {}", host.ip_address);
println!(
" Static Addresses: {}",
host.static_addresses
.iter()
.map(|u| u.to_string())
.collect::<Vec<_>>()
.join(", ")
);
println!(" Listen Port: {}", host.listen_port);
println!(
" Type: {}",
if host.is_lighthouse {
"Lighthouse"
} else if host.is_relay {
"Relay"
} else {
"Host"
}
);
println!(" Blocked: {}", host.is_blocked);
println!(" Last Seen: {}", host.metadata.last_seen_at);
println!(" Client Version: {}", host.metadata.version);
println!(" Platform: {}", host.metadata.platform);
println!(
"Client Update Available: {}",
host.metadata.update_available
);
println!(" Created: {}", host.created_at);
println!();
}
return Ok(());
}
let mut table = Table::new();
match table_style {
TableStyle::List => unreachable!(),
TableStyle::Basic => (),
TableStyle::Pretty => { table.load_preset(UTF8_FULL).apply_modifier(UTF8_ROUND_CORNERS) ; },
};
table.set_header(vec!["ID", "Name", "Organization ID", "Network ID", "Role ID", "IP Address", "Static Addresses", "Listen Port", "Type", "Blocked", "Last Seen"]);
for host in &resp.data {
table.add_row(vec![host.id.as_str(), &host.name, &host.organization_id, &host.network_id, &host.role_id, &host.ip_address, &host.static_addresses.iter().map(|u| u.to_string()).collect::<Vec<_>>().join(" "), &host.listen_port.to_string(), if host.is_lighthouse { "Lighthouse" } else if host.is_relay { "Relay" } else { "Host" }, if host.is_blocked { "true" } else { "false" }, &host.metadata.last_seen_at]);
}
println!("{table}");
} else { } else {
let resp: APIErrorResponse = res.json().await?; let resp: APIErrorResponse = res.json().await?;

View File

@ -175,7 +175,10 @@ pub enum HostCommands {
static_address: Option<SocketAddrV4>, static_address: Option<SocketAddrV4>,
}, },
/// List all hosts on your network /// List all hosts on your network
List {}, List {
#[clap(short = 'T', long, default_value_t = TableStyle::Basic)]
table_style: TableStyle
},
/// Lookup a specific host by it's ID /// Lookup a specific host by it's ID
Lookup { Lookup {
#[clap(short, long)] #[clap(short, long)]