patchy/
backup.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fs::read_to_string;
use std::io::Write;
use std::{
    ffi::OsString,
    fs::{File, ReadDir},
    path::PathBuf,
};
use tempfile::tempfile;

use crate::git_commands::GIT_ROOT;
use crate::CONFIG_ROOT;

pub fn backup_files(config_files: ReadDir) -> anyhow::Result<Vec<(OsString, File, String)>> {
    let mut backups = Vec::new();

    for entry in config_files {
        let config_file = entry?;

        let path = config_file.path();
        let contents = read_to_string(&path)?;

        let filename = config_file.file_name();
        let mut destination_backed_up = tempfile()?;

        write!(destination_backed_up, "{contents}")?;

        backups.push((filename, destination_backed_up, contents));
    }

    Ok(backups)
}
pub fn restore_backup(file_name: &OsString, contents: &str) -> anyhow::Result<()> {
    let path = GIT_ROOT.join(PathBuf::from(CONFIG_ROOT).join(file_name));
    let mut file = File::create(&path)?;

    write!(file, "{contents}")?;

    Ok(())
}