diff --git a/src/lib.rs b/src/lib.rs index 31849ba..f2be644 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,19 +52,20 @@ pub fn try_template( bail!(e); } + if should_keep(action)? { + println!("Saved as {project_dir:?}."); + } else { + remove_attempt(&project_dir)?; + } + // save session data to file let file = File::create(get_session_path(tmp_dir))?; let session = Session { last_template: template.to_owned(), + previous_attempt: project_dir, }; serde_json::to_writer(BufWriter::new(file), &session)?; - if should_keep(action)? { - println!("Saved as {project_dir:?}."); - } else { - remove_attempt(&project_dir)?; - } - Ok(()) } diff --git a/src/main.rs b/src/main.rs index ad0f76a..2f3ef3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,7 @@ -use std::{ - borrow::Cow, - fs::{self, File}, - io::BufReader, - path::PathBuf, - str::FromStr, -}; +use std::{borrow::Cow, fs::File, io::BufReader, path::PathBuf, str::FromStr}; use anyhow::{bail, Context}; -use atmpt::{get_atmpt_dir, get_session_path, session::Session, templates::Templates, Atmpt}; +use atmpt::{get_session_path, session::Session, templates::Templates, Atmpt}; use clap::Parser; use directories::ProjectDirs; @@ -30,7 +24,15 @@ fn main() -> anyhow::Result<()> { } else if args.required.list_templates { println!("{}", Templates::try_from(data_dir.as_ref())?); } else if args.required.previous { - atmpt::summon_and_wait(&args.editor, &last_modified_attempt(&tmp_dir)?)?; + let session = Session::from_file(&get_session_path(&tmp_dir))?; + if !session.previous_attempt.exists() { + bail!( + "Last modified attempt, {:?} does not exist! Did it move?\nMake a new attempt!", + session.previous_attempt + ); + } + + atmpt::summon_and_wait(&args.editor, &session.previous_attempt)?; } else { let template = match args.required.template { Some(t) => t, @@ -50,36 +52,3 @@ fn main() -> anyhow::Result<()> { Ok(()) } - -fn last_modified_attempt(tmp_dir: &Option) -> anyhow::Result { - let atmpt_dir = get_atmpt_dir(tmp_dir); - if !atmpt_dir.exists() { - bail!("Could not find atmpt folder, have you run atmpt recently?"); - } - - let entries = fs::read_dir(atmpt_dir.as_ref())?; - let mut last = None; - for entry in entries { - let entry = entry?; - - let metadata = entry.metadata()?; - if !metadata.is_dir() { - continue; - } - - let new_time = metadata.modified()?; - if let Some((_, time)) = last { - if new_time <= time { - continue; - } - } - - last = Some((entry.path(), new_time)); - } - - if let Some((path, _)) = last { - Ok(path) - } else { - bail!("Could not find last modified attempt, have you saved any?") - } -} diff --git a/src/session.rs b/src/session.rs index d61bf16..d4a0c85 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,6 +1,25 @@ +use std::{ + fs::File, + io::BufReader, + path::{Path, PathBuf}, +}; + +use anyhow::Context; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Session { pub last_template: String, + pub previous_attempt: PathBuf, +} + +impl Session { + pub fn from_file(path: &Path) -> anyhow::Result { + let file = File::open(path) + .context("Could not open session file, have you run atmpt recently?")?; + let session: Self = serde_json::from_reader(BufReader::new(file)) + .context("Failed to read session file!")?; + + Ok(session) + } } diff --git a/tests/mod.rs b/tests/mod.rs index 0d87c0f..53042ea 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -94,6 +94,6 @@ fn pass_on_retry() { fn pass_on_previous() { const DIR: &str = "previous"; - cmd_opts(DIR, ["c", "-y"], false, false).success(); // keep directory with attempt + cmd_opts(DIR, ["c", "-y"], false, true).success(); // keep directory with attempt cmd_always_delete(DIR, ["-p"]).success(); }