From 367285031dd47d6ae685915863010257be3e9ab3 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 2 Jul 2024 15:23:55 +1000 Subject: [PATCH] feat: Use comfy-table to show the list of mock servers --- Cargo.lock | 94 +++++++++++++++++++++++++------- pact_mock_server_cli/Cargo.toml | 1 + pact_mock_server_cli/src/list.rs | 23 +++----- 3 files changed, 84 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70308681..d5b51037 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -344,9 +344,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.101" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac367972e516d45567c7eafc73d24e1c193dcf200a8d94e9db7b3d38b349572d" +checksum = "74b6a57f98764a267ff415d50a25e6e166f3831a5071af4995296ea97d210490" dependencies = [ "jobserver", "libc", @@ -419,18 +419,18 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.7" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" +checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" -version = "4.5.7" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" +checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" dependencies = [ "anstream", "anstyle", @@ -450,6 +450,18 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "comfy-table" +version = "7.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b34115915337defe99b2aff5c2ce6771e5fbc4079f4b506301f5cf394c8452f7" +dependencies = [ + "crossterm", + "strum", + "strum_macros", + "unicode-width", +] + [[package]] name = "console" version = "0.15.8" @@ -546,6 +558,28 @@ version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" +[[package]] +name = "crossterm" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" +dependencies = [ + "bitflags 2.6.0", + "crossterm_winapi", + "libc", + "parking_lot", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -1077,9 +1111,9 @@ dependencies = [ [[package]] name = "hyper" -version = "1.3.1" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d" +checksum = "c4fe55fb7a772d59a5ff1dfbff4fe0258d19b89fec4b233e75d35d5d2316badc" dependencies = [ "bytes", "futures-channel", @@ -1118,7 +1152,7 @@ checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" dependencies = [ "futures-util", "http 1.1.0", - "hyper 1.3.1", + "hyper 1.4.0", "hyper-util", "log", "rustls 0.23.10", @@ -1144,16 +1178,16 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56" +checksum = "3ab92f4f49ee4fb4f997c784b7a2e0fa70050211e0b6a287f898c3c9785ca956" dependencies = [ "bytes", "futures-channel", "futures-util", "http 1.1.0", "http-body 1.0.0", - "hyper 1.3.1", + "hyper 1.4.0", "pin-project-lite", "socket2", "tokio", @@ -1586,9 +1620,9 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" [[package]] name = "object" -version = "0.36.0" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" +checksum = "081b846d1d56ddfc18fdf1a922e4f6e07a11768ea1b92dec44e42b72712ccfce" dependencies = [ "memchr", ] @@ -1745,7 +1779,7 @@ dependencies = [ "expectest", "futures", "http-body-util", - "hyper 1.3.1", + "hyper 1.4.0", "hyper-rustls 0.27.2", "hyper-util", "itertools 0.13.0", @@ -1782,10 +1816,11 @@ dependencies = [ "anyhow", "bytes", "clap", + "comfy-table", "env_logger 0.11.3", "expectest", "http 1.1.0", - "hyper 1.3.1", + "hyper 1.4.0", "hyper-util", "itertools 0.13.0", "lazy_static", @@ -2373,7 +2408,7 @@ dependencies = [ "http 1.1.0", "http-body 1.0.0", "http-body-util", - "hyper 1.3.1", + "hyper 1.4.0", "hyper-rustls 0.27.2", "hyper-util", "ipnet", @@ -2639,9 +2674,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.118" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d947f6b3163d8857ea16c4fa0dd4840d52f3041039a85decd46867eb1abef2e4" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -2795,6 +2830,25 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "strum" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" + +[[package]] +name = "strum_macros" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.68", +] + [[package]] name = "subtle" version = "2.6.1" @@ -3537,7 +3591,7 @@ dependencies = [ "hex", "http 1.1.0", "http-body-util", - "hyper 1.3.1", + "hyper 1.4.0", "hyper-util", "itertools 0.13.0", "lazy_static", diff --git a/pact_mock_server_cli/Cargo.toml b/pact_mock_server_cli/Cargo.toml index 027fb6f3..fd33a390 100644 --- a/pact_mock_server_cli/Cargo.toml +++ b/pact_mock_server_cli/Cargo.toml @@ -25,6 +25,7 @@ tls = ["pact_mock_server/tls", "dep:rustls"] anyhow = "1.0.86" bytes = "1.6.0" clap = { version = "4.5.7", features = ["cargo"] } +comfy-table = "7.1.1" http = "1.1.0" hyper = { version = "1.3.1", features = ["full"] } hyper-util = "0.1.5" diff --git a/pact_mock_server_cli/src/list.rs b/pact_mock_server_cli/src/list.rs index b187e6db..d20cbc7d 100644 --- a/pact_mock_server_cli/src/list.rs +++ b/pact_mock_server_cli/src/list.rs @@ -1,4 +1,6 @@ -use serde_json::{self, json, Value}; +use comfy_table::presets::UTF8_FULL; +use comfy_table::Table; +use serde_json::{self, Value}; use tracing::error; use crate::{display_error, handle_error}; @@ -29,26 +31,19 @@ pub async fn list_mock_servers(host: &str, port: u16, usage: &str) -> Result<(), .ok_or_else(|| handle_error("Invalid JSON received from master server - no mockServers attribute"))?; let mock_servers = mock_servers_json.as_array() .ok_or_else(|| handle_error("Invalid JSON received from master server - mockServers is not an array"))?; - let provider_len = mock_servers.iter().fold(0, |acc, ms| { - let unknown = &json!(""); - let provider = ms.get("provider").unwrap_or(unknown) - .as_str().unwrap_or(""); - if provider.len() > acc { - provider.len() - } else { - acc - } - }); - println!("{0:36} {1:5} {2:3$} {4}", "Mock Server Id", "Port", - "Provider", provider_len, "Verification State"); + let mut table = Table::new(); + table + .load_preset(UTF8_FULL) + .set_header(vec!["Mock Server Id", "Port", "Provider", "Verification State"]); for ms in mock_servers { let id = json2string(ms.get("id")); let port = json2string(ms.get("port")); let provider = json2string(ms.get("provider")); let status = json2string(ms.get("status")); - println!("{0} {1} {2:3$} {4}", id, port, provider, provider_len, status); + table.add_row(vec![id.as_str(), port.as_str(), provider.as_str(), status.as_str()]); }; + println!("{table}"); Ok(()) }, Err(err) => {