2022-07-19 08:28:24 +00:00
|
|
|
use crate::cli::LOG;
|
2022-12-20 10:30:06 +00:00
|
|
|
use crate::cnf::SERVER_AGENT;
|
2022-02-16 23:45:23 +00:00
|
|
|
use crate::err::Error;
|
2020-06-29 15:36:01 +00:00
|
|
|
use reqwest::blocking::Client;
|
2022-09-27 15:35:03 +00:00
|
|
|
use reqwest::header::ACCEPT;
|
2022-12-20 10:30:06 +00:00
|
|
|
use reqwest::header::USER_AGENT;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::fs::OpenOptions;
|
|
|
|
|
|
|
|
pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
|
2022-07-19 11:57:59 +00:00
|
|
|
// Set the default logging level
|
|
|
|
crate::cli::log::init(3);
|
|
|
|
// Try to parse the file argument
|
2020-06-29 15:36:01 +00:00
|
|
|
let file = matches.value_of("file").unwrap();
|
2022-07-19 11:57:59 +00:00
|
|
|
// Try to open the specified file
|
2021-03-29 15:43:37 +00:00
|
|
|
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
// Parse all other cli arguments
|
|
|
|
let user = matches.value_of("user").unwrap();
|
|
|
|
let pass = matches.value_of("pass").unwrap();
|
|
|
|
let conn = matches.value_of("conn").unwrap();
|
|
|
|
let ns = matches.value_of("ns").unwrap();
|
|
|
|
let db = matches.value_of("db").unwrap();
|
2022-07-19 11:57:59 +00:00
|
|
|
// Set the correct export URL
|
2022-12-18 16:00:36 +00:00
|
|
|
let conn = format!("{conn}/export");
|
2022-07-19 11:57:59 +00:00
|
|
|
// Export the data from the database
|
2022-11-23 09:35:29 +00:00
|
|
|
let mut res = Client::new()
|
2022-12-18 16:00:36 +00:00
|
|
|
.get(conn)
|
2022-12-20 10:30:06 +00:00
|
|
|
.header(USER_AGENT, SERVER_AGENT)
|
2022-09-27 14:58:20 +00:00
|
|
|
.header(ACCEPT, "application/octet-stream")
|
2020-06-29 15:36:01 +00:00
|
|
|
.basic_auth(user, Some(pass))
|
|
|
|
.header("NS", ns)
|
|
|
|
.header("DB", db)
|
2022-11-23 09:35:29 +00:00
|
|
|
.send()?;
|
|
|
|
// Check import result and report error
|
|
|
|
if res.status().is_success() {
|
|
|
|
res.copy_to(&mut file)?;
|
|
|
|
info!(target: LOG, "The SQL file was exported successfully");
|
|
|
|
} else if res.status().is_client_error() || res.status().is_server_error() {
|
|
|
|
error!(target: LOG, "Request failed with status {}. Body: {}", res.status(), res.text()?);
|
|
|
|
} else {
|
|
|
|
error!(target: LOG, "Unexpected response status {}", res.status());
|
|
|
|
}
|
2022-07-19 11:57:59 +00:00
|
|
|
// Everything OK
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok(())
|
|
|
|
}
|