surrealpatch/src/cli/export.rs

61 lines
1.5 KiB
Rust
Raw Normal View History

2021-03-29 15:43:37 +00:00
use anyhow::Error;
2020-06-29 15:36:01 +00:00
use reqwest::blocking::Client;
2021-03-29 15:43:37 +00:00
use reqwest::header::CONTENT_TYPE;
2020-06-29 15:36:01 +00:00
use std::fs::OpenOptions;
use std::io::copy;
pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
// Ensure that the command has a file
// argument. If no file argument has
// been provided, then return an error.
let file = matches.value_of("file").unwrap();
// Attempt to open the specified file,
// and if there is a problem opening
// the file, then return an error.
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();
let conn = format!("{}/export", conn);
// Create and send the HTTP request
// specifying the basic auth header
// and the specified content-type.
let mut res = Client::new()
.get(&conn)
.header(CONTENT_TYPE, "application/octet-stream")
.basic_auth(user, Some(pass))
.header("NS", ns)
.header("DB", db)
.send()?
.error_for_status()?;
// Copy the contents of the http get
// response to the specified ouput
// file and pass along any errors.
copy(&mut res, &mut file)?;
// Output an informational message
// and return an Ok to signify that
// this command has been successful.
info!("The SQL file was exported successfully");
Ok(())
}