-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from threefoldtech/cli_improvements
Cli improvements
- Loading branch information
Showing
7 changed files
with
271 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod inspect; | ||
mod message; | ||
mod peer; | ||
mod routes; | ||
|
||
pub use inspect::inspect; | ||
pub use message::{recv_msg, send_msg}; | ||
pub use peer::{add_peers, list_peers, remove_peers}; | ||
pub use routes::{list_fallback_routes, list_selected_routes}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use mycelium_api::Route; | ||
use prettytable::{row, Table}; | ||
use std::net::SocketAddr; | ||
|
||
use tracing::{debug, error}; | ||
|
||
pub async fn list_selected_routes( | ||
server_addr: SocketAddr, | ||
json_print: bool, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
let request_url = format!("http://{server_addr}/api/v1/admin/routes/selected"); | ||
match reqwest::get(&request_url).await { | ||
Err(e) => { | ||
error!("Failed to retrieve selected routes"); | ||
return Err(e.into()); | ||
} | ||
Ok(resp) => { | ||
debug!("Listing selected routes"); | ||
|
||
if json_print { | ||
// API call returns routes in JSON format by default | ||
let selected_routes = resp.text().await?; | ||
println!("{selected_routes}"); | ||
} else { | ||
// Print routes in table format | ||
let routes: Vec<Route> = resp.json().await?; | ||
let mut table = Table::new(); | ||
table.add_row(row!["Subnet", "Next Hop", "Metric", "Seq No"]); | ||
|
||
for route in routes.iter() { | ||
table.add_row(row![ | ||
&route.subnet, | ||
&route.next_hop, | ||
route.metric, | ||
route.seqno, | ||
]); | ||
} | ||
|
||
table.printstd(); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn list_fallback_routes( | ||
server_addr: SocketAddr, | ||
json_print: bool, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
let request_url = format!("http://{server_addr}/api/v1/admin/routes/fallback"); | ||
match reqwest::get(&request_url).await { | ||
Err(e) => { | ||
error!("Failed to retrieve fallback routes"); | ||
return Err(e.into()); | ||
} | ||
Ok(resp) => { | ||
debug!("Listing fallback routes"); | ||
|
||
if json_print { | ||
// API call returns routes in JSON format by default | ||
let fallback_routes = resp.text().await?; | ||
println!("{fallback_routes}"); | ||
} else { | ||
// Print routes in table format | ||
let routes: Vec<Route> = resp.json().await?; | ||
let mut table = Table::new(); | ||
table.add_row(row!["Subnet", "Next Hop", "Metric", "Seq No"]); | ||
|
||
for route in routes.iter() { | ||
table.add_row(row![ | ||
&route.subnet, | ||
&route.next_hop, | ||
route.metric, | ||
route.seqno, | ||
]); | ||
} | ||
|
||
table.printstd(); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |
Oops, something went wrong.