-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Add display feature to CLI
- Loading branch information
1 parent
66c3ae8
commit e67bac4
Showing
6 changed files
with
179 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use clap::{Parser, Subcommand}; | ||
use console::style; | ||
use geekorm::{GEEKORM_BANNER, GEEKORM_VERSION}; | ||
use std::path::PathBuf; | ||
|
||
pub const AUTHOR: &str = env!("CARGO_PKG_AUTHORS"); | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
pub struct Arguments { | ||
/// Enable Debugging | ||
#[clap(long, env, default_value_t = false)] | ||
pub debug: bool, | ||
|
||
/// Disable Banner | ||
#[clap(long, default_value_t = false)] | ||
pub disable_banner: bool, | ||
|
||
/// Configuration file path | ||
#[clap(short, long, env, default_value = "./config.toml")] | ||
pub config: PathBuf, | ||
|
||
/// Working Directory | ||
#[clap(short, long, env, default_value = "./")] | ||
pub working_dir: PathBuf, | ||
|
||
/// Subcommands | ||
#[clap(subcommand)] | ||
pub commands: Option<ArgumentCommands>, | ||
} | ||
|
||
#[derive(Subcommand, Debug)] | ||
pub enum ArgumentCommands { | ||
/// Read and display the database schema generated by GeekORM | ||
Display, | ||
} | ||
|
||
pub fn init() -> Arguments { | ||
let arguments = Arguments::parse(); | ||
|
||
let log_level = match &arguments.debug { | ||
false => log::LevelFilter::Info, | ||
true => log::LevelFilter::Debug, | ||
}; | ||
|
||
env_logger::builder() | ||
.parse_default_env() | ||
.filter_level(log_level) | ||
.init(); | ||
|
||
if !arguments.disable_banner { | ||
println!( | ||
"{} {} - v{}\n", | ||
style(GEEKORM_BANNER).green(), | ||
style(AUTHOR).red(), | ||
style(GEEKORM_VERSION).blue() | ||
); | ||
} | ||
|
||
arguments | ||
} |
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,10 +1,24 @@ | ||
use anyhow::Result; | ||
|
||
mod cli; | ||
mod utils; | ||
mod workflows; | ||
|
||
use crate::cli::*; | ||
use crate::workflows::*; | ||
|
||
fn main() -> Result<()> { | ||
println!( | ||
"{} - v{}\n", | ||
geekorm::GEEKORM_BANNER, | ||
geekorm::GEEKORM_VERSION | ||
); | ||
let arguments = init(); | ||
|
||
match arguments.commands { | ||
Some(ArgumentCommands::Display) => display_database(&arguments)?, | ||
None => { | ||
println!("No subcommand selected..."); | ||
println!("Use --help for more information.\n"); | ||
|
||
println!("Thank you for trying out / using GeekORM!"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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,51 @@ | ||
#![allow(dead_code)] | ||
use anyhow::Result; | ||
use geekorm::Table; | ||
use glob::glob; | ||
use std::path::PathBuf; | ||
|
||
use crate::Arguments; | ||
|
||
/// This struct represents a database and is based on the `internal` | ||
/// module of the `geekorm_derive` crate. | ||
#[derive(Debug, Clone, serde::Deserialize)] | ||
pub(crate) struct Database { | ||
pub created_at: chrono::DateTime<chrono::Utc>, | ||
pub updated_at: chrono::DateTime<chrono::Utc>, | ||
pub tables: Vec<Table>, | ||
} | ||
|
||
impl Database { | ||
pub fn find_database(arguments: &Arguments) -> Result<Self> { | ||
let target_path = "target/*/build/geekorm-derive-*/out/geekorm-*.json"; | ||
|
||
let path = arguments.working_dir.join(target_path); | ||
let path_str = path.to_str().ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"Failed to convert path to string: {:?}", | ||
arguments.working_dir | ||
) | ||
})?; | ||
|
||
// Find the latest database file based on the creation date | ||
glob(path_str)? | ||
.filter_map(|entry| entry.ok()) | ||
.fold(None, |acc, entry| { | ||
let database = Self::load_database(entry).ok()?; | ||
Some(acc.map_or(database.clone(), |ref acc: Database| { | ||
if database.updated_at < acc.updated_at { | ||
database | ||
} else { | ||
acc.clone() | ||
} | ||
})) | ||
}) | ||
.ok_or_else(|| anyhow::anyhow!("Database not found")) | ||
} | ||
|
||
pub fn load_database(path: PathBuf) -> Result<Self> { | ||
let database = std::fs::read_to_string(path)?; | ||
let database: Database = serde_json::from_str(&database)?; | ||
Ok(database) | ||
} | ||
} |
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 @@ | ||
pub mod database; |
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,31 @@ | ||
use crate::utils::database::Database; | ||
use anyhow::Result; | ||
use console::style; | ||
use log::debug; | ||
|
||
pub(crate) fn display_database(arguments: &super::cli::Arguments) -> Result<()> { | ||
println!("Displaying the database schema generated by GeekORM...\n"); | ||
|
||
let database = Database::find_database(arguments)?; | ||
debug!("Database: {:#?}", database); | ||
|
||
for table in database.tables { | ||
println!(" Table({}) {{", style(table.name).green()); | ||
|
||
for column in table.columns { | ||
if column.skip { | ||
continue; | ||
} | ||
|
||
println!( | ||
" Column({}, {})", | ||
style(column.name).blue(), | ||
style(column.column_type).yellow() | ||
); | ||
} | ||
|
||
println!(" }}\n"); | ||
} | ||
|
||
Ok(()) | ||
} |