From b93f037017e768150d109bdc08f53f3c6918256f Mon Sep 17 00:00:00 2001 From: aleidk Date: Fri, 24 Jan 2025 19:15:21 -0300 Subject: [PATCH] feat: allow to write schema to any type who impl Write --- src/bin/cli/main.rs | 7 ++++++- src/sql.rs | 15 +++++++++++++-- src/sql/schema.rs | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/bin/cli/main.rs b/src/bin/cli/main.rs index e5c4456..2f6ca5b 100644 --- a/src/bin/cli/main.rs +++ b/src/bin/cli/main.rs @@ -30,6 +30,9 @@ enum Commands { Discover { /// Schema to discover, it defaults to `public` in postures. schema: Option, + + #[arg(short = 'o', long)] + output: Option, }, } @@ -48,7 +51,9 @@ async fn main() -> Result<()> { // matches just as you would the top level cmd let result = match &cli.command { Commands::Query { sql } => sql::handle_query(url, sql).await, - Commands::Discover { schema } => sql::discover_scheme(url, schema.to_owned()).await, + Commands::Discover { schema, output } => { + sql::discover_scheme(url, schema.to_owned(), output.to_owned()).await + } }; if let Err(err) = result { diff --git a/src/sql.rs b/src/sql.rs index 2f59159..af8509f 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -1,4 +1,6 @@ +use std::fs::File; use std::io; +use std::path::Path; use clap::ValueEnum; use sea_schema::postgres::discovery::SchemaDiscovery; @@ -45,7 +47,11 @@ pub async fn handle_query(url: String, query: &String) -> Result<()> { return Ok(()); } -pub async fn discover_scheme(url: String, schema: Option) -> Result<()> { +pub async fn discover_scheme( + url: String, + schema: Option, + output: Option, +) -> Result<()> { let (_, db_name) = get_connector(&url).await?; let schema_discovery = match db_name.as_str() { @@ -69,7 +75,12 @@ pub async fn discover_scheme(url: String, schema: Option) -> Result<()> .schemas .insert(schema.schema.to_owned(), Schema::from(schema)); - data_model.write(&mut io::stdout())?; + let mut buffer: Box = match output { + Some(path) => Box::new(File::create(Path::new(&path))?), + None => Box::new(io::stdout()), + }; + + data_model.write(&mut buffer)?; Ok(()) } diff --git a/src/sql/schema.rs b/src/sql/schema.rs index 5b61eee..10fc03c 100644 --- a/src/sql/schema.rs +++ b/src/sql/schema.rs @@ -48,8 +48,8 @@ impl DataModel { } impl DataModel { - pub fn write(&self, writter: &mut T) -> error::Result<()> { - writter.write_all(self.to_string().as_bytes())?; + pub fn write(&self, buffer: &mut T) -> error::Result<()> { + buffer.write_all(self.to_string().as_bytes())?; Ok(()) } }