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

View file

@ -6,36 +6,21 @@ use std::fs::OpenOptions;
use std::io::copy; use std::io::copy;
pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> { pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
// Ensure that the command has a file // Set the default logging level
// argument. If no file argument has crate::cli::log::init(3);
// been provided, then return an error. // Try to parse the file argument
let file = matches.value_of("file").unwrap(); let file = matches.value_of("file").unwrap();
// Try to open the specified file
// Attempt to open the specified file,
// and if there is a problem opening
// the file, then return an error.
let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file)?; let mut file = OpenOptions::new().write(true).create(true).truncate(true).open(file)?;
// Parse all other cli arguments // Parse all other cli arguments
let user = matches.value_of("user").unwrap(); let user = matches.value_of("user").unwrap();
let pass = matches.value_of("pass").unwrap(); let pass = matches.value_of("pass").unwrap();
let conn = matches.value_of("conn").unwrap(); let conn = matches.value_of("conn").unwrap();
let ns = matches.value_of("ns").unwrap(); let ns = matches.value_of("ns").unwrap();
let db = matches.value_of("db").unwrap(); let db = matches.value_of("db").unwrap();
// Set the correct export URL
let conn = format!("{}/export", conn); let conn = format!("{}/export", conn);
// Export the data from the database
// Create and send the HTTP request
// specifying the basic auth header
// and the specified content-type.
let mut res = Client::new() let mut res = Client::new()
.get(&conn) .get(&conn)
.header(CONTENT_TYPE, "application/octet-stream") .header(CONTENT_TYPE, "application/octet-stream")
@ -44,18 +29,10 @@ pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
.header("DB", db) .header("DB", db)
.send()? .send()?
.error_for_status()?; .error_for_status()?;
// Copy the export to the file
// Copy the contents of the http get
// response to the specified ouput
// file and pass along any errors.
copy(&mut res, &mut file)?; copy(&mut res, &mut file)?;
// Output a success message
// Output an informational message
// and return an Ok to signify that
// this command has been successful.
info!(target: LOG, "The SQL file was exported successfully"); info!(target: LOG, "The SQL file was exported successfully");
// Everything OK
Ok(()) Ok(())
} }

View file

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

View file

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