diff --git a/src/cloud/adapters/s3.rs b/src/cloud/adapters/s3.rs index 6873944..e18ae57 100644 --- a/src/cloud/adapters/s3.rs +++ b/src/cloud/adapters/s3.rs @@ -3,13 +3,7 @@ use crate::util::*; use crate::{SETTINGS, SYNC_DIR}; use s3::serde_types::ListBucketResult; use s3::{creds::Credentials, Bucket, Region}; -use std::fs; -use std::io::Write; -use std::path::PathBuf; - -pub struct Cloud { - bucket: Bucket, -} +use std::{fs, io::Write, path::PathBuf}; fn init_bucket() -> Result { let s3_secret = SETTINGS.get_string("s3.secret")?; @@ -32,6 +26,10 @@ fn key_to_path(key: &str) -> PathBuf { path } +pub struct Cloud { + bucket: Bucket, +} + impl CloudAdapter for Cloud { fn new() -> Self { Self { @@ -68,7 +66,7 @@ impl CloudAdapter for Cloud { for entry in walk_dir(SYNC_DIR.to_path_buf())? { let path = entry.path(); if !self.exists(&path)? { - self.save(&path).unwrap(); + self.save(&path)?; synced += 1; } } @@ -93,7 +91,7 @@ impl CloudAdapter for Cloud { fn save(&self, path: &Path) -> Result<()> { self.bucket - .put_object(normalize_path(path), &Cloud::read_file(path)?)?; + .put_object(normalize_path(path), &Self::read_file(path)?)?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 5bd14c3..1ab6eb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,12 +21,14 @@ use util::*; lazy_static! { pub static ref SETTINGS: Config = Config::builder() - .add_source(config::File::from(settings_file().unwrap())) + .add_source(config::File::from( + settings_file_path().expect("Couldn't retrieve settings file path") + )) .build() - .expect("Cannot init settings"); + .unwrap(); pub static ref SYNC_DIR: PathBuf = SETTINGS .get_string("main.path") - .expect("Missing main.path env") + .expect("Missing syncing path. Please fill main.path in the settings file") .parse() .expect("Invalid path string"); pub static ref IS_INTERNET_AVAILABLE: Mutex = Mutex::new(false); diff --git a/src/util.rs b/src/util.rs index 55faa0a..07a0fea 100644 --- a/src/util.rs +++ b/src/util.rs @@ -61,16 +61,17 @@ where { let mut sp = Spinner::new(Spinners::Dots9, message.into()); - operation().unwrap(); - - if stop_message.is_empty() { + if let Err(err) = operation() { + sp.stop(); + println!("An error has occurred: {err:?}"); + } else if stop_message.is_empty() { sp.stop(); } else { sp.stop_with_message(stop_message.into()); } } -pub fn settings_file() -> Result { +pub fn settings_file_path() -> Result { let mut path = dirs::config_dir().unwrap(); path.push("rsink");