Skip to content

Commit

Permalink
new: --retry command
Browse files Browse the repository at this point in the history
recreates and opens the last template you used this session
  • Loading branch information
marcelohdez committed May 15, 2024
1 parent 37b9244 commit 4f692d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub struct RequiredArgs {

#[arg(group = "main", short, long, help = "List available templates")]
pub list_templates: bool,

#[arg(group = "main", short, long, help = "Retry last template")]
pub retry: bool,
}

#[derive(Debug, PartialEq, Eq)]
Expand Down
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ use std::{
env,
fs::{self, File},
io::{self, BufWriter},
path::Path,
path::{Path, PathBuf},
process::Command,
};

use anyhow::{bail, Context, Ok};
use chrono::Local;

pub fn get_atmpt_dir() -> PathBuf {
env::temp_dir().join("atmpt")
}

pub fn get_session_path() -> PathBuf {
get_atmpt_dir().join("session.json")
}

pub fn try_template(
template: &str,
editor: &str,
Expand All @@ -25,7 +33,7 @@ pub fn try_template(
) -> anyhow::Result<()> {
let templates = Templates::try_from(data_dir)?;
let wanted_dir = templates.find(template)?;
let tmp_dir = env::temp_dir().join("atmpt");
let tmp_dir = get_atmpt_dir();

let time = Local::now().format("%Y_%m_%d-%H_%M_%S");
let project_dir = tmp_dir.join(format!("{template}_{time}"));
Expand All @@ -37,7 +45,7 @@ pub fn try_template(
}

// save session data to file
let file = File::create(tmp_dir.join("session.json"))?;
let file = File::create(get_session_path())?;
let session = Session {
last_template: template.to_owned(),
};
Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, path::PathBuf};
use std::{borrow::Cow, fs::File, io::BufReader, path::PathBuf};

use anyhow::bail;
use atmpt::{templates::Templates, Atmpt};
use anyhow::{bail, Context};
use atmpt::{get_session_path, session::Session, templates::Templates, Atmpt};
use clap::Parser;
use directories::ProjectDirs;

Expand All @@ -24,6 +24,16 @@ fn main() -> anyhow::Result<()> {
};

atmpt::try_template(&template, &editor, &data_dir, action)?;
} else if args.required.retry {
let file = File::open(get_session_path())
.context("Could not open session file, have you run atmpt recently?")?;
let session: Session =
serde_json::from_reader(BufReader::new(file)).context("Failed to read session file")?;
let Some(editor) = args.editor else {
bail!("No editor to use! Set your $VISUAL variable or pass a command to --editor");
};

atmpt::try_template(&session.last_template, &editor, &data_dir, action)?;
} else if args.required.list_template_dir {
println!("{}", data_dir.display());
} else {
Expand Down

0 comments on commit 4f692d0

Please sign in to comment.