generated from alecodes/base-template
feat: allow to write schema to any type who impl Write
This commit is contained in:
parent
de506a73df
commit
b93f037017
3 changed files with 21 additions and 5 deletions
|
|
@ -30,6 +30,9 @@ enum Commands {
|
||||||
Discover {
|
Discover {
|
||||||
/// Schema to discover, it defaults to `public` in postures.
|
/// Schema to discover, it defaults to `public` in postures.
|
||||||
schema: Option<String>,
|
schema: Option<String>,
|
||||||
|
|
||||||
|
#[arg(short = 'o', long)]
|
||||||
|
output: Option<String>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,7 +51,9 @@ async fn main() -> Result<()> {
|
||||||
// matches just as you would the top level cmd
|
// matches just as you would the top level cmd
|
||||||
let result = match &cli.command {
|
let result = match &cli.command {
|
||||||
Commands::Query { sql } => sql::handle_query(url, sql).await,
|
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 {
|
if let Err(err) = result {
|
||||||
|
|
|
||||||
15
src/sql.rs
15
src/sql.rs
|
|
@ -1,4 +1,6 @@
|
||||||
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use clap::ValueEnum;
|
use clap::ValueEnum;
|
||||||
use sea_schema::postgres::discovery::SchemaDiscovery;
|
use sea_schema::postgres::discovery::SchemaDiscovery;
|
||||||
|
|
@ -45,7 +47,11 @@ pub async fn handle_query(url: String, query: &String) -> Result<()> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn discover_scheme(url: String, schema: Option<String>) -> Result<()> {
|
pub async fn discover_scheme(
|
||||||
|
url: String,
|
||||||
|
schema: Option<String>,
|
||||||
|
output: Option<String>,
|
||||||
|
) -> Result<()> {
|
||||||
let (_, db_name) = get_connector(&url).await?;
|
let (_, db_name) = get_connector(&url).await?;
|
||||||
|
|
||||||
let schema_discovery = match db_name.as_str() {
|
let schema_discovery = match db_name.as_str() {
|
||||||
|
|
@ -69,7 +75,12 @@ pub async fn discover_scheme(url: String, schema: Option<String>) -> Result<()>
|
||||||
.schemas
|
.schemas
|
||||||
.insert(schema.schema.to_owned(), Schema::from(schema));
|
.insert(schema.schema.to_owned(), Schema::from(schema));
|
||||||
|
|
||||||
data_model.write(&mut io::stdout())?;
|
let mut buffer: Box<dyn io::Write> = match output {
|
||||||
|
Some(path) => Box::new(File::create(Path::new(&path))?),
|
||||||
|
None => Box::new(io::stdout()),
|
||||||
|
};
|
||||||
|
|
||||||
|
data_model.write(&mut buffer)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,8 @@ impl DataModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataModel {
|
impl DataModel {
|
||||||
pub fn write<T: Write>(&self, writter: &mut T) -> error::Result<()> {
|
pub fn write<T: Write>(&self, buffer: &mut T) -> error::Result<()> {
|
||||||
writter.write_all(self.to_string().as_bytes())?;
|
buffer.write_all(self.to_string().as_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue