Skip to content

Commit

Permalink
change: move previous attempt to session file
Browse files Browse the repository at this point in the history
...rather than getting last modified folder.
This makes it more reliable and make more sense especially
when using a custom attempts folder.
  • Loading branch information
marcelohdez committed May 24, 2024
1 parent 8948360 commit ffcc3ce
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 49 deletions.
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
53 changes: 11 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
Expand All @@ -50,36 +52,3 @@ fn main() -> anyhow::Result<()> {

Ok(())
}

fn last_modified_attempt(tmp_dir: &Option<PathBuf>) -> anyhow::Result<PathBuf> {
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?")
}
}
19 changes: 19 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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)
}
}
2 changes: 1 addition & 1 deletion tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit ffcc3ce

Please sign in to comment.