surrealpatch/src/cli/export.rs

70 lines
1.7 KiB
Rust
Raw Normal View History

use crate::cli::abstraction::{
AuthArguments, DatabaseConnectionArguments, DatabaseSelectionArguments,
};
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 clap::Args;
use surrealdb::engine::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
#[derive(Args, Debug)]
pub struct ExportCommandArguments {
#[arg(help = "Path to the sql file to export. Use dash - to write into stdout.")]
#[arg(default_value = "-")]
#[arg(index = 1)]
file: String,
#[command(flatten)]
conn: DatabaseConnectionArguments,
#[command(flatten)]
auth: AuthArguments,
#[command(flatten)]
sel: DatabaseSelectionArguments,
}
pub async fn init(
ExportCommandArguments {
file,
conn: DatabaseConnectionArguments {
endpoint,
},
auth: AuthArguments {
username,
password,
},
sel: DatabaseSelectionArguments {
namespace: ns,
database: db,
},
}: ExportCommandArguments,
) -> Result<(), Error> {
// Initialize opentelemetry and logging
crate::o11y::builder().with_log_level("error").init();
// Connect to the database engine
let client = connect(endpoint).await?;
// Sign in to the server if the specified database engine supports it
let root = Root {
username: &username,
password: &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(())
}