-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(catalog): introduce the archives catalog
- Loading branch information
Showing
6 changed files
with
153 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! Catalog of all archives. | ||
//! | ||
|
||
use std::path; | ||
|
||
use anyhow::Result; | ||
use async_std::fs; | ||
use async_std::stream::StreamExt; | ||
use regex::Regex; | ||
|
||
/// Catalag of all archives. | ||
pub struct Catalog { | ||
/// Location of the catalog. | ||
pub dirpath: path::PathBuf, | ||
|
||
/// Sorted list of outdated archive files (oldest to newest). | ||
pub outdated: Vec<path::PathBuf>, | ||
|
||
/// Sorted list of recent archive files (oldest to newest). | ||
pub recent: Vec<path::PathBuf>, | ||
} | ||
|
||
impl Catalog { | ||
/// Return a new `Catalog` by listing the archives in `dirpath`. | ||
/// | ||
/// Only archive files such as `archive-20220804T221153.tar.zst` are added to the catalog. | ||
pub async fn new(dirpath: &path::Path, rotate_size: usize) -> Result<Catalog> { | ||
let mut archives: Vec<path::PathBuf> = vec![]; | ||
|
||
let pattern = r#".*archive-\d{8}T\d{6}\.tar\.zst"#; | ||
let matcher = Regex::new(pattern).unwrap(); | ||
|
||
let mut entries = fs::read_dir(dirpath).await?; | ||
while let Some(entry) = entries.next().await { | ||
let entry = entry?; | ||
let path = entry.path(); | ||
if matcher.captures(&path.to_string_lossy()).is_some() { | ||
archives.push(path.into()); | ||
} | ||
} | ||
|
||
archives.sort(); | ||
|
||
let index = std::cmp::max(0, archives.len() - rotate_size); | ||
let recent = archives.split_off(index); | ||
|
||
Ok(Catalog { | ||
dirpath: dirpath.to_path_buf(), | ||
outdated: archives, | ||
recent, | ||
}) | ||
} | ||
|
||
/// Total number of archives in the catalog. | ||
pub fn size(&self) -> usize { | ||
self.outdated.len() + self.recent.len() | ||
} | ||
|
||
/// Filepath of the current archive. | ||
/// | ||
/// This is usually the most recent archive. | ||
pub fn current(&self) -> Option<&path::Path> { | ||
self.recent.last().map(|p| p.as_ref()) | ||
} | ||
|
||
/// Compact the catalog by deleting old archives files, keeping only the `rotate_size` most | ||
/// recent ones. | ||
pub async fn compact(&mut self, rotate_size: usize) -> Result<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,71 @@ | ||
use async_std::task; | ||
use clap::Parser; | ||
|
||
use tmux_revive::{config::Command, config::Config, display_message, save}; | ||
use tmux_revive::{ | ||
config::CatalogSubcommand, config::Command, config::Config, save, tmux_display_message, Catalog, | ||
}; | ||
|
||
fn main() { | ||
let config = Config::parse(); | ||
|
||
match config.command { | ||
Command::Save { | ||
stdout, | ||
num_archives, | ||
} => { | ||
match task::block_on(save::save(&config.archive_dirpath, num_archives)) { | ||
Command::Save { rotate_size } => { | ||
match task::block_on(save::save(&config.archive_dirpath, rotate_size)) { | ||
Ok(report) => { | ||
let message = format!( | ||
"{report}, persisted to {}", | ||
config.archive_dirpath.to_string_lossy() | ||
); | ||
if stdout { | ||
println!("{message}"); | ||
} else { | ||
display_message(&message); | ||
} | ||
success_message(&message, config.stdout); | ||
} | ||
Err(e) => { | ||
let message = format!("🛑 Could not save sessions: {}", e); | ||
if stdout { | ||
eprintln!("{message}"); | ||
std::process::exit(1); | ||
} else { | ||
display_message(&message); | ||
} | ||
failure_message(&message, config.stdout); | ||
} | ||
}; | ||
} | ||
|
||
Command::Restore { .. } => unimplemented!(), | ||
|
||
Command::Catalog { command } => match command { | ||
CatalogSubcommand::List { rotate_size } => { | ||
match task::block_on(Catalog::new(&config.archive_dirpath, rotate_size)) { | ||
Ok(catalog) => { | ||
println!( | ||
"Catalog: {} archives in `{}`\n", | ||
&catalog.size(), | ||
&catalog.dirpath.to_string_lossy() | ||
); | ||
for archive_path in catalog.outdated.iter() { | ||
println!("{} (outdated)", archive_path.to_string_lossy()); | ||
} | ||
for archive_path in catalog.recent.iter() { | ||
println!("{}", archive_path.to_string_lossy()); | ||
} | ||
} | ||
Err(e) => { | ||
let message = format!("🛑 Could not list archives: {}", e); | ||
failure_message(&message, config.stdout); | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
|
||
fn success_message(message: &str, stdout: bool) { | ||
if stdout { | ||
println!("{message}"); | ||
} else { | ||
tmux_display_message(message); | ||
} | ||
} | ||
|
||
fn failure_message(message: &str, stdout: bool) { | ||
if stdout { | ||
eprintln!("{message}"); | ||
std::process::exit(1); | ||
} else { | ||
tmux_display_message(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters