2022-07-19 08:28:24 +00:00
|
|
|
use crate::cli::LOG;
|
2022-02-16 23:45:23 +00:00
|
|
|
use crate::err::Error;
|
2023-01-07 08:32:18 +00:00
|
|
|
use surrealdb::engine::any::connect;
|
2022-12-31 08:02:41 +00:00
|
|
|
use surrealdb::error::Api as ApiError;
|
|
|
|
use surrealdb::opt::auth::Root;
|
|
|
|
use surrealdb::Error as SurrealError;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-12-31 08:02:41 +00:00
|
|
|
#[tokio::main]
|
|
|
|
pub async fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
|
2022-07-19 11:57:59 +00:00
|
|
|
// Set the default logging level
|
2022-12-31 08:02:41 +00:00
|
|
|
crate::cli::log::init(1);
|
2022-07-19 11:57:59 +00:00
|
|
|
// 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
|
2022-12-31 08:02:41 +00:00
|
|
|
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();
|
2022-12-31 08:02:41 +00:00
|
|
|
// Connect to the database engine
|
|
|
|
let client = connect(endpoint).await?;
|
2023-03-07 09:53:56 +00:00
|
|
|
// Sign in to the server if the specified database engine supports it
|
2022-12-31 08:02:41 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 09:35:29 +00:00
|
|
|
}
|
2022-12-31 08:02:41 +00:00
|
|
|
// 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");
|
2022-07-19 11:57:59 +00:00
|
|
|
// Everything OK
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok(())
|
|
|
|
}
|