From 38a828629e9f9330cd519b5d5304118ddb1135ad Mon Sep 17 00:00:00 2001 From: Bauke Date: Sat, 11 Nov 2023 13:08:11 +0100 Subject: [PATCH] Add the file metadata subcommand. --- source/cli/mod.rs | 15 +++++++++++++++ source/cli/run.rs | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/source/cli/mod.rs b/source/cli/mod.rs index b24aab6..43665bc 100644 --- a/source/cli/mod.rs +++ b/source/cli/mod.rs @@ -98,6 +98,21 @@ pub enum FileSubcommand { file: PathBuf, }, + /// Extract metadata of a file. + Metadata { + /// The chrono format string for dates, defaults to `%FT%T%z` (ISO 8601). + #[arg(long, default_value = "%FT%T%z")] + date_format: String, + + /// Get the modified date (uses --date-format as the format string). + #[arg(long, default_value = "false")] + modified: bool, + + /// The file to get the metadata of. + #[arg()] + file: PathBuf, + }, + /// Extract parts of a file. Parts { /// Print the base name of the file (without the extension). diff --git a/source/cli/run.rs b/source/cli/run.rs index 8bbad2e..ee9258f 100644 --- a/source/cli/run.rs +++ b/source/cli/run.rs @@ -8,7 +8,7 @@ use { }, logging::append_line_to_file, }, - chrono::{SecondsFormat, Utc}, + chrono::{DateTime, Local, SecondsFormat, Utc}, std::{ffi::OsStr, path::Path}, }; @@ -49,6 +49,19 @@ pub fn run() { } } + FileSubcommand::Metadata { + date_format, + modified, + file, + } => { + let metadata = std::fs::metadata(file).unwrap(); + if modified { + let date_modified = + DateTime::::from(metadata.modified().unwrap()); + print!("{}", date_modified.format(&date_format)); + } + } + FileSubcommand::Parts { basename, directory,