kifi/commands/
snapshot.rs

1use crate::commands::common::get_user;
2use crate::errors::Error;
3use std::fs;
4use std::path::PathBuf;
5use std::time::{SystemTime, UNIX_EPOCH};
6
7pub fn snap_file(file_name: &PathBuf, snap_dir: &PathBuf) -> Result<(), Error> {
8    fs::create_dir_all(snap_dir).map_err(Error::CreateDirectory)?;
9
10    if let Some(dir) = file_name.parent() {
11        fs::create_dir_all(snap_dir.join(dir)).map_err(Error::CreateDirectory)?;
12    }
13
14    match fs::copy(file_name, snap_dir.join(file_name)) {
15        Ok(_) => Ok(()),
16        Err(io_error) => Err(Error::FileCopy(
17            file_name.to_owned(),
18            snap_dir.join(file_name).to_owned(),
19            io_error,
20        )),
21    }
22}
23
24pub fn gen_name() -> Result<String, Error> {
25    let user = get_user()?;
26    // let email = String::from("test@testing.com");
27
28    let current_timestamp = SystemTime::now()
29        .duration_since(UNIX_EPOCH)
30        .expect("Right now is before 1970? Check the system clock.")
31        .as_secs();
32
33    Ok(format!("{}_{}", user.name(), current_timestamp))
34}