0
0
mirror of https://github.com/TeFiLeDo/listload.git synced 2024-11-23 12:46:17 +01:00

list listing and list info

This commit is contained in:
Adrian Wannenmacher 2023-09-30 03:26:52 +02:00
parent b1c1b4f551
commit d57ed908e2
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
3 changed files with 68 additions and 12 deletions

View File

@ -3,7 +3,7 @@ use clap::Subcommand;
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum ListCommand { pub enum ListCommand {
/// Create a new download list. /// Create a new download list.
#[clap(visible_alias = "new", visible_alias = "c")] #[clap(visible_alias = "c", visible_alias = "new")]
Create { Create {
/// A unique name, which will be used to refer to the list. /// A unique name, which will be used to refer to the list.
name: String, name: String,
@ -12,12 +12,22 @@ pub enum ListCommand {
description: String, description: String,
}, },
/// Delete a download list. /// Delete a download list.
#[clap(visible_alias = "rm", visible_alias = "d")] #[clap(visible_alias = "d", visible_alias = "rm")]
Delete { Delete {
/// The name of the list to remove. /// The name of the list to remove.
name: String, name: String,
}, },
/// Update an existing list. /// Print a download lists properties.
///
#[clap(visible_alias = "i", visible_alias = "show")]
Info {
/// The name of the list to inspect.
name: String,
},
/// List all known download lists.
#[clap(visible_alias = "l", visible_alias = "ls")]
List,
/// Update an existing download list.
/// ///
/// Only values specified for this command are changed. /// Only values specified for this command are changed.
#[clap(visible_alias = "u")] #[clap(visible_alias = "u")]

View File

@ -20,6 +20,25 @@ fn main() -> anyhow::Result<()> {
match cli.command { match cli.command {
cli::Command::List { command } => match command { cli::Command::List { command } => match command {
cli::ListCommand::Create { name, description } => {
let mut list = DownloadListStore::new(name, &data)?;
list.set_description(description);
list.save()
}
cli::ListCommand::Delete { name } => DownloadListStore::delete(&name, &data),
cli::ListCommand::Info { name } => {
let list = DownloadListStore::load(&name, &data)?;
println!("name: {}", list.name());
println!("description: {}", list.description());
Ok(())
}
cli::ListCommand::List => Ok(DownloadListStore::list(&data)?
.into_iter()
.for_each(|n| println!("{n}"))),
cli::ListCommand::Update { name, description } => { cli::ListCommand::Update { name, description } => {
let mut list = DownloadListStore::load(&name, &data)?; let mut list = DownloadListStore::load(&name, &data)?;
@ -29,14 +48,6 @@ fn main() -> anyhow::Result<()> {
list.save() list.save()
} }
cli::ListCommand::Create { name, description } => {
let mut list = DownloadListStore::new(name, &data)?;
list.set_description(description);
list.save()
}
cli::ListCommand::Delete { name } => DownloadListStore::delete(&name, &data),
}, },
} }
} }

View File

@ -1,11 +1,12 @@
use std::{ use std::{
collections::HashSet,
fs::{self, File, OpenOptions}, fs::{self, File, OpenOptions},
io::{BufReader, BufWriter, Seek}, io::{BufReader, BufWriter, Seek},
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
path::Path, path::Path,
}; };
use anyhow::{ensure, Context}; use anyhow::{anyhow, ensure, Context};
use fs4::FileExt; use fs4::FileExt;
use crate::data::DownloadList; use crate::data::DownloadList;
@ -83,6 +84,40 @@ impl DownloadListStore {
.context("failed to remove list file") .context("failed to remove list file")
} }
/// Get a all list names inside `directory`.
pub fn list(directory: &Path) -> anyhow::Result<HashSet<String>> {
let ls = fs::read_dir(directory).context("failed to read list file directory")?;
// 6 steps process:
//
// 1. turn the entry into a file name
ls.map(|pe| {
pe.map(|e| {
e.file_name()
// 2. turn file name into string
.to_str()
.map(ToString::to_string)
.ok_or(anyhow!("os string not string"))
// 3. try to remove the `.json` suffix from the file name
.map(|n| n.strip_suffix(".json").map(ToString::to_string))
})
})
// 4. remove all files that had no `.json` suffix
.filter(|e| match e {
Ok(Ok(None)) => false,
_ => true,
})
// 5. flatten read error and os string error
.map(|e| match e {
Ok(Ok(x)) => Ok(x.expect("none filtered out before")),
Ok(Err(e)) => Err(e),
Err(e) => Err(e).context("failed to read list file directory entry"),
})
// 6. handle errors & collect return
.collect::<Result<_, _>>()
.context("failed to list lists")
}
fn path_in_directory(directory: &Path, name: &str) -> std::path::PathBuf { fn path_in_directory(directory: &Path, name: &str) -> std::path::PathBuf {
directory.join(name).with_extension("json") directory.join(name).with_extension("json")
} }