Skip to content

Commit

Permalink
fix: log errors instead of crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Sep 3, 2022
1 parent 93e1031 commit 83c6fc4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
16 changes: 7 additions & 9 deletions src/cloud/adapters/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bucket> {
let s3_secret = SETTINGS.get_string("s3.secret")?;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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(())
}

Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> = Mutex::new(false);
Expand Down
9 changes: 5 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
pub fn settings_file_path() -> Result<PathBuf> {
let mut path = dirs::config_dir().unwrap();

path.push("rsink");
Expand Down

0 comments on commit 83c6fc4

Please sign in to comment.