Skip to content

Commit

Permalink
feat: Use comfy-table to show the list of mock servers
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 2, 2024
1 parent 96476c9 commit 3672850
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 34 deletions.
94 changes: 74 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pact_mock_server_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 9 additions & 14 deletions pact_mock_server_cli/src/list.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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!("<unknown>");
let provider = ms.get("provider").unwrap_or(unknown)
.as_str().unwrap_or("<unknown>");
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) => {
Expand Down

0 comments on commit 3672850

Please sign in to comment.