surrealpatch/src/cli/export.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

2022-07-19 08:28:24 +00:00
use crate::cli::LOG;
2022-02-16 23:45:23 +00:00
use crate::err::Error;
use surrealdb::engines::any::connect;
use surrealdb::error::Api as ApiError;
use surrealdb::opt::auth::Root;
use surrealdb::Error as SurrealError;
2020-06-29 15:36:01 +00:00
#[tokio::main]
pub async fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
// Set the default logging level
crate::cli::log::init(1);
// Try to parse the file argument
2020-06-29 15:36:01 +00:00
let file = matches.value_of("file").unwrap();
// Parse all other cli arguments
let username = matches.value_of("user").unwrap();
let password = matches.value_of("pass").unwrap();
let endpoint = matches.value_of("conn").unwrap();
2020-06-29 15:36:01 +00:00
let ns = matches.value_of("ns").unwrap();
let db = matches.value_of("db").unwrap();
// Connect to the database engine
let client = connect(endpoint).await?;
// Sign in to the server if the specified dabatabase engine supports it
let root = Root {
username,
password,
};
if let Err(error) = client.signin(root).await {
match error {
// Authentication not supported by this engine, we can safely continue
SurrealError::Api(ApiError::AuthNotSupported) => {}
error => {
return Err(error.into());
}
}
}
// Use the specified namespace / database
client.use_ns(ns).use_db(db).await?;
// Export the data from the database
client.export(file).await?;
info!(target: LOG, "The SQL file was exported successfully");
// Everything OK
2020-06-29 15:36:01 +00:00
Ok(())
}