Skip to content

Commit

Permalink
Error handling for file deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
golden-lucky-monkey committed Apr 14, 2022
1 parent 709c46e commit d7e845a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ pub(crate) fn write_to_csv(
positions_count: usize) -> Result<(), Box<dyn std::error::Error>> {
let utc_time: chrono::prelude::DateTime<chrono::prelude::Utc> = chrono::prelude::Utc::now();

// Delete any existing file on first run
if positions_count == 1 as usize {
let remove_file = std::fs::remove_file(filename);
match remove_file {
Err(_e) => {
log::info!("Positions file does not exist, creating new file")
}
Ok(o) => o
}
}

// Append to existing file, or create new file
let file = std::fs::OpenOptions::new()
.write(true)
Expand All @@ -71,10 +82,12 @@ pub(crate) fn write_to_csv(
log::debug!("Writing position to {:?}", String::from(filename));

let mut wtr = csv::Writer::from_writer(file);

// On first run, write header
if positions_count == 1 as usize {
// On first run, write header
wtr.write_record(&["utc_time", "price", "size", "side"])?;
}
// Write row
wtr.write_record(
&[
utc_time.to_string(),
Expand Down

0 comments on commit d7e845a

Please sign in to comment.