Skip to content

Commit 7505033

Browse files
committed
fix: write state.json to destination
Previously, the state.json would be written to the working directory and not into the destination directory.
1 parent bc9f9f2 commit 7505033

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/main.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tokio::time::{sleep, Duration};
1818

1919
use types::*;
2020

21-
const STATE_FILE_PATH: &str = "state.json";
21+
const STATE_FILE: &str = "state.json";
2222

2323
const MAX_PER_PAGE: u8 = 100;
2424
const START_PAGE: u32 = 1; // GitHub starts indexing at page 1
@@ -420,20 +420,25 @@ fn write(x: EntryWithMetadata, destination: PathBuf) -> Result<(), WriteError> {
420420
Ok(())
421421
}
422422

423-
fn write_backup_state(start_time: DateTime<Utc>) {
423+
fn write_backup_state(
424+
start_time: DateTime<Utc>,
425+
mut destination: PathBuf,
426+
) -> Result<(), WriteError> {
424427
let state = BackupState {
425428
version: STATE_VERSION,
426429
last_backup: start_time,
427430
};
428-
let json = serde_json::to_string_pretty(&state).unwrap();
429-
let mut file = File::create(STATE_FILE_PATH).unwrap();
430-
file.write_all(json.as_bytes()).unwrap();
431-
info!("written {}", STATE_FILE_PATH);
431+
destination.push(STATE_FILE);
432+
let json = serde_json::to_string_pretty(&state)?;
433+
let mut file = File::create(destination.clone())?;
434+
file.write_all(json.as_bytes())?;
435+
info!("Written backup state to {}", destination.display());
436+
Ok(())
432437
}
433438

434439
fn get_last_backup_time(destination: PathBuf) -> Option<DateTime<Utc>> {
435440
let mut path = destination;
436-
path.push(STATE_FILE_PATH);
441+
path.push(STATE_FILE);
437442
info!("Trying to read {} file", path.display());
438443
match fs::read_to_string(path.clone()) {
439444
Ok(contents) => {
@@ -586,7 +591,15 @@ async fn main() -> ExitCode {
586591
}
587592

588593
if task.await.is_ok() {
589-
write_backup_state(start_time);
594+
if let Err(e) = write_backup_state(start_time, args.destination.clone()) {
595+
error!(
596+
"Failed to write {} to {}: {}",
597+
STATE_FILE,
598+
args.destination.clone().display(),
599+
e
600+
);
601+
return ExitCode::from(EXIT_WRITING);
602+
}
590603
} else {
591604
return ExitCode::from(EXIT_API_ERROR);
592605
}

0 commit comments

Comments
 (0)