Set default logging level for import/export/backup commands

This commit is contained in:
Tobie Morgan Hitchcock 2022-07-19 12:57:59 +01:00
parent 7860a205db
commit 91b8baff3d
4 changed files with 46 additions and 104 deletions

View file

@ -8,22 +8,11 @@ use std::io::copy;
const TYPE: &str = "application/octet-stream";
pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
// Attempt to open the specified file,
// and if there is a problem opening
// the file, then return an error.
// Try to parse the specified source file
let from = matches.value_of("from").unwrap();
// Attempt to open the specified file,
// and if there is a problem opening
// the file, then return an error.
// Try to parse the specified output file
let into = matches.value_of("into").unwrap();
// Process the response, checking
// for any errors, and outputting
// the responses back to the user.
// Process the source->destination response
if from.ends_with(".db") && into.ends_with(".db") {
backup_file_to_file(matches, from, into)
} else if from.ends_with(".db") {
@ -36,45 +25,48 @@ pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
}
fn backup_file_to_file(_: &clap::ArgMatches, from: &str, into: &str) -> Result<(), Error> {
// Try to open the source file
let mut from = OpenOptions::new().read(true).open(from)?;
// Try to open the output file
let mut into = OpenOptions::new().write(true).create(true).truncate(true).open(into)?;
// Copy the data to the destination
copy(&mut from, &mut into)?;
// Everything OK
Ok(())
}
fn backup_http_to_file(matches: &clap::ArgMatches, from: &str, into: &str) -> Result<(), Error> {
// Parse the specified username
let user = matches.value_of("user").unwrap();
// Parse the specified password
let pass = matches.value_of("pass").unwrap();
// Set the correct source URL
let from = format!("{}/sync", from);
// Try to open the source http
let mut from = Client::new()
.get(&from)
.basic_auth(user, Some(pass))
.header(CONTENT_TYPE, TYPE)
.send()?
.error_for_status()?;
// Try to open the output file
let mut into = OpenOptions::new().write(true).create(true).truncate(true).open(into)?;
// Copy the data to the destination
copy(&mut from, &mut into)?;
// Everything OK
Ok(())
}
fn backup_file_to_http(matches: &clap::ArgMatches, from: &str, into: &str) -> Result<(), Error> {
// Parse the specified username
let user = matches.value_of("user").unwrap();
// Parse the specified password
let pass = matches.value_of("pass").unwrap();
let into = format!("{}/sync", into);
// Try to open the source file
let from = OpenOptions::new().read(true).open(from)?;
// Set the correct output URL
let into = format!("{}/sync", into);
// Copy the data to the destination
Client::new()
.post(&into)
.basic_auth(user, Some(pass))
@ -82,26 +74,27 @@ fn backup_file_to_http(matches: &clap::ArgMatches, from: &str, into: &str) -> Re
.body(from)
.send()?
.error_for_status()?;
// Everything OK
Ok(())
}
fn backup_http_to_http(matches: &clap::ArgMatches, from: &str, into: &str) -> Result<(), Error> {
// Parse the specified username
let user = matches.value_of("user").unwrap();
// Parse the specified password
let pass = matches.value_of("pass").unwrap();
// Set the correct source URL
let from = format!("{}/sync", from);
// Set the correct output URL
let into = format!("{}/sync", into);
// Try to open the source file
let from = Client::new()
.get(&from)
.basic_auth(user, Some(pass))
.header(CONTENT_TYPE, TYPE)
.send()?
.error_for_status()?;
// Copy the data to the destination
Client::new()
.post(&into)
.basic_auth(user, Some(pass))
@ -109,6 +102,6 @@ fn backup_http_to_http(matches: &clap::ArgMatches, from: &str, into: &str) -> Re
.body(Body::new(from))
.send()?
.error_for_status()?;
// Everything OK
Ok(())
}

View file

@ -6,36 +6,21 @@ 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.
// Set the default logging level
crate::cli::log::init(3);
// Try to parse the file argument
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.
// Try to open the specified file
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file)?;
// 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();
// Set the correct export URL
let conn = format!("{}/export", conn);
// Create and send the HTTP request
// specifying the basic auth header
// and the specified content-type.
// Export the data from the database
let mut res = Client::new()
.get(&conn)
.header(CONTENT_TYPE, "application/octet-stream")
@ -44,18 +29,10 @@ pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
.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 the export to the file
copy(&mut res, &mut file)?;
// Output an informational message
// and return an Ok to signify that
// this command has been successful.
// Output a success message
info!(target: LOG, "The SQL file was exported successfully");
// Everything OK
Ok(())
}

View file

@ -6,44 +6,24 @@ use std::fs::OpenOptions;
use std::io::prelude::*;
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.
// Set the default logging level
crate::cli::log::init(3);
// Try to parse the file argument
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.
// Try to open the specified file
let mut file = OpenOptions::new().read(true).open(file)?;
// Attempt to read the contents of the
// file into a string variable, and if
// not, then return an error.
// Read the full contents of the file
let mut body = String::new();
file.read_to_string(&mut body)?;
// 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();
// Set the correct import URL
let conn = format!("{}/import", conn);
// Create and send the HTTP request
// specifying the basic auth header
// and the specified content-type.
// Import the data into the database
Client::new()
.post(&conn)
.header(CONTENT_TYPE, "application/octet-stream")
@ -53,12 +33,8 @@ pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
.body(body)
.send()?
.error_for_status()?;
// Output an informational message
// and return an Ok to signify that
// this command has been successful.
// Output a success message
info!(target: LOG, "The SQL file was imported successfully");
// Everything OK
Ok(())
}

View file

@ -385,10 +385,6 @@ pub fn init() {
let matches = setup.get_matches();
let verbose = matches.occurrences_of("verbose") as usize;
log::init(verbose);
let output = match matches.subcommand() {
Some(("start", m)) => start::init(m),
Some(("backup", m)) => backup::init(m),