fix: Use prettytable for detailed database output

pull/24376/head
Carol (Nichols || Goulding) 2021-09-17 15:55:37 -04:00
parent 6520985b5d
commit ac9c25d33c
2 changed files with 23 additions and 8 deletions

View File

@ -11,6 +11,7 @@ use influxdb_iox_client::{
}, },
write::{self, WriteError}, write::{self, WriteError},
}; };
use prettytable::{format, Cell, Row, Table};
use std::{ use std::{
convert::TryInto, fs::File, io::Read, num::NonZeroU64, path::PathBuf, str::FromStr, convert::TryInto, fs::File, io::Read, num::NonZeroU64, path::PathBuf, str::FromStr,
time::Duration, time::Duration,
@ -269,8 +270,15 @@ pub async fn command(connection: Connection, config: Config) -> Result<()> {
} else { } else {
client.list_detailed_databases().await? client.list_detailed_databases().await?
}; };
println!("Deleted at | Generation ID | Name");
println!("---------------------------------+---------------+--------"); let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
table.set_titles(Row::new(vec![
Cell::new("Deleted at"),
Cell::new("Generation ID"),
Cell::new("Name"),
]));
for database in databases { for database in databases {
let deleted_at = database let deleted_at = database
.deleted_at .deleted_at
@ -279,11 +287,14 @@ pub async fn command(connection: Connection, config: Config) -> Result<()> {
dt.ok().map(|d| d.to_string()) dt.ok().map(|d| d.to_string())
}) })
.unwrap_or_else(String::new); .unwrap_or_else(String::new);
println!( table.add_row(Row::new(vec![
"{:<34}{:<16}{}", Cell::new(&deleted_at),
deleted_at, database.generation_id, database.db_name, Cell::new(&database.generation_id.to_string()),
); Cell::new(&database.db_name),
]));
} }
print!("{}", table);
} else { } else {
let names = client.list_database_names().await?; let names = client.list_database_names().await?;
println!("{}", names.join("\n")) println!("{}", names.join("\n"))

View File

@ -187,14 +187,18 @@ const DELETED_DB_DATETIME: &str = r#"[\d-]+\s[\d:\.]+\s[A-Z]+"#;
fn deleted_db_match(db: &str, generation_id: usize) -> predicates::str::RegexPredicate { fn deleted_db_match(db: &str, generation_id: usize) -> predicates::str::RegexPredicate {
predicate::str::is_match(format!( predicate::str::is_match(format!(
r#"(?m)^{}\s+{}\s+{}$"#, r#"(?m)^\|\s+{}\s+\|\s+{}\s+\|\s+{}\s+\|$"#,
DELETED_DB_DATETIME, generation_id, db DELETED_DB_DATETIME, generation_id, db
)) ))
.unwrap() .unwrap()
} }
fn active_db_match(db: &str, generation_id: usize) -> predicates::str::RegexPredicate { fn active_db_match(db: &str, generation_id: usize) -> predicates::str::RegexPredicate {
predicate::str::is_match(format!(r#"(?m)^\s+{}\s+{}$"#, generation_id, db)).unwrap() predicate::str::is_match(format!(
r#"(?m)^\|\s+\|\s+{}\s+\|\s+{}\s+\|$"#,
generation_id, db
))
.unwrap()
} }
#[tokio::test] #[tokio::test]