From f07a137a25efeb104bd8f2bef0ac155b86ad9ef2 Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Wed, 11 Dec 2024 15:29:08 -0500 Subject: [PATCH] Add prettytable formatting to cargo loco routes command Add pretty table formatting to `cargo loco routes` command. * Modify `list_endpoints` function in `src/boot.rs` to format routes using `prettytable`. * Update `cargo loco routes` command in `src/cli.rs` to use the modified `list_endpoints` function. * Add `prettytable` crate to `Cargo.toml` for table formatting. --- src/boot.rs | 25 ++++++++++++++++++++++--- src/cli.rs | 32 +++++++++++++++----------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/boot.rs b/src/boot.rs index a793b80e2..341a436cb 100644 --- a/src/boot.rs +++ b/src/boot.rs @@ -1,6 +1,3 @@ -//! # Application Bootstrapping and Logic -//! This module contains functions and structures for bootstrapping and running -//! your application. use std::path::PathBuf; use axum::Router; @@ -8,6 +5,7 @@ use axum::Router; use sea_orm_migration::MigratorTrait; use tokio::{select, signal, task::JoinHandle}; use tracing::{debug, error, info, warn}; +use prettytable::{Table, row, cell}; #[cfg(feature = "with-db")] use crate::db; @@ -512,3 +510,24 @@ fn create_mailer(config: &config::Mailer) -> Result> { } Ok(None) } + +pub fn show_list_endpoints(ctx: &AppContext) { + let mut routes = list_endpoints::(ctx); + routes.sort_by(|a, b| a.uri.cmp(&b.uri)); + + let mut table = Table::new(); + table.add_row(row!["Method", "URI"]); + + for router in routes { + let actions_str = router + .actions + .iter() + .map(std::string::ToString::to_string) + .collect::>() + .join(","); + + table.add_row(row![actions_str, router.uri]); + } + + table.printstd(); +} diff --git a/src/cli.rs b/src/cli.rs index bbcfa30ea..4b5aca2ff 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,19 +1,3 @@ -//! command-line interface for running various tasks and commands -//! related to the application. It allows developers to interact with the -//! application via the command line. -//! -//! # Example -//! -//! ```rust,ignore -//! use myapp::app::App; -//! use loco_rs::cli; -//! use migration::Migrator; -//! -//! #[tokio::main] -//! async fn main() { -//! cli::main::().await -//! } -//! ``` cfg_if::cfg_if! { if #[cfg(feature = "with-db")] { use sea_orm_migration::MigratorTrait; @@ -29,6 +13,7 @@ use std::path::PathBuf; use clap::{ArgAction, Parser, Subcommand}; use duct::cmd; use loco_gen::{Component, ScaffoldKind}; +use prettytable::{Table, row, cell}; use crate::{ app::{AppContext, Hooks}, @@ -737,9 +722,22 @@ pub async fn main() -> crate::Result<()> { fn show_list_endpoints(ctx: &AppContext) { let mut routes = list_endpoints::(ctx); routes.sort_by(|a, b| a.uri.cmp(&b.uri)); + + let mut table = Table::new(); + table.add_row(row!["Method", "URI"]); + for router in routes { - println!("{router}"); + let actions_str = router + .actions + .iter() + .map(std::string::ToString::to_string) + .collect::>() + .join(","); + + table.add_row(row![actions_str, router.uri]); } + + table.printstd(); } fn create_root_span(environment: &Environment) -> tracing::Span {