Skip to content

Commit

Permalink
Add prettytable formatting to cargo loco routes command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AngelOnFira committed Dec 11, 2024
1 parent 9d0dfb7 commit f07a137
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
25 changes: 22 additions & 3 deletions src/boot.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//! # Application Bootstrapping and Logic
//! This module contains functions and structures for bootstrapping and running
//! your application.
use std::path::PathBuf;

use axum::Router;
#[cfg(feature = "with-db")]
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;
Expand Down Expand Up @@ -512,3 +510,24 @@ fn create_mailer(config: &config::Mailer) -> Result<Option<EmailSender>> {
}
Ok(None)
}

pub fn show_list_endpoints<H: Hooks>(ctx: &AppContext) {
let mut routes = list_endpoints::<H>(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::<Vec<_>>()
.join(",");

table.add_row(row![actions_str, router.uri]);
}

table.printstd();
}
32 changes: 15 additions & 17 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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::<App, Migrator>().await
//! }
//! ```
cfg_if::cfg_if! {
if #[cfg(feature = "with-db")] {
use sea_orm_migration::MigratorTrait;
Expand All @@ -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},
Expand Down Expand Up @@ -737,9 +722,22 @@ pub async fn main<H: Hooks>() -> crate::Result<()> {
fn show_list_endpoints<H: Hooks>(ctx: &AppContext) {
let mut routes = list_endpoints::<H>(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::<Vec<_>>()
.join(",");

table.add_row(row![actions_str, router.uri]);
}

table.printstd();
}

fn create_root_span(environment: &Environment) -> tracing::Span {
Expand Down

0 comments on commit f07a137

Please sign in to comment.