From 3a163f844196dbec30e4f8b8312b61ac8e59e455 Mon Sep 17 00:00:00 2001 From: David Chalifoux Date: Tue, 23 Jan 2024 22:25:05 -0500 Subject: [PATCH] Add --self flag, --json flag, fix typo --- Cargo.lock | 216 ++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 + src/main.rs | 37 +++++++- src/put/files.rs | 6 ++ 4 files changed, 254 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33b7577..1e42bdc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.11" @@ -155,6 +170,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41daef31d7a747c5c847246f36de49ced6f7403b4cdabc807a97b5cc184cda7a" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "serde", + "windows-targets 0.52.0", +] + [[package]] name = "clap" version = "4.4.18" @@ -229,6 +257,51 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "darling" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.48", +] + +[[package]] +name = "darling_macro" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", + "serde", +] + [[package]] name = "directories" version = "5.0.1" @@ -388,13 +461,19 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap", + "indexmap 2.1.0", "slab", "tokio", "tokio-util", "tracing", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.14.3" @@ -413,6 +492,12 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "http" version = "0.2.11" @@ -484,6 +569,35 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.5.0" @@ -494,6 +608,17 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", + "serde", +] + [[package]] name = "indexmap" version = "2.1.0" @@ -501,7 +626,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.14.3", + "serde", ] [[package]] @@ -534,6 +660,8 @@ dependencies = [ "confy", "reqwest", "serde", + "serde_json", + "serde_with", "tabled", ] @@ -632,6 +760,15 @@ dependencies = [ "tempfile", ] +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -752,6 +889,12 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -991,6 +1134,35 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5c9fdb6b00a489875b22efd4b78fe2b363b72265cc5f6eb2e2b9ee270e6140c" +dependencies = [ + "base64", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.1.0", + "serde", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbff351eb4b33600a2e138dfa0b10b65a238ea8ff8fb2387c422c5022a3e8298" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "slab" version = "0.4.9" @@ -1116,6 +1288,35 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "time" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f657ba42c3f86e7680e53c8cd3af8abbe56b5491790b46e22e19c0d57463583e" +dependencies = [ + "deranged", + "itoa", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26197e33420244aeb70c3e8c78376ca46571bc4e701e4791c2cd9f57dcb3a43f" +dependencies = [ + "time-core", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1198,7 +1399,7 @@ version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" dependencies = [ - "indexmap", + "indexmap 2.1.0", "serde", "serde_spanned", "toml_datetime", @@ -1392,6 +1593,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index e3d9f5d..dbcae13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ serde = { version = "1.0.195", features = ["derive"] } reqwest = { version = "0.11.23", features = ["json", "blocking", "multipart", "native-tls-vendored"] } tabled = { version = "0.15.0", features = ["derive"] } bytefmt = "0.1.7" +serde_json = { version = "1.0.111", features = ["std"] } +serde_with = { version = "3.5.1", features = [] } [[bin]] name = "kaput" diff --git a/src/main.rs b/src/main.rs index b46efd7..9f8193d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,18 @@ fn cli() -> Command { Command::new("list") .about("List your files and folders") .long_about("Lists your files and folders.") - .arg(arg!([FOLDER_ID] "Lists the contents of a folder (optional)")), + .arg(arg!([FOLDER_ID] "Lists the contents of a folder (optional)")) + .arg(Arg::new("self") + .short('s') + .long("self") + .help("If set, returns the info for the folder itself in JSON format") + .required(false) + .num_args(0)) + .arg(Arg::new("json") + .long("json") + .help("If set, returns the output in JSON format") + .required(false) + .num_args(0)), ) .subcommand( Command::new("search") @@ -151,7 +162,7 @@ fn cli() -> Command { .long_about("Extracts ZIP and RAR archives.") .arg_required_else_help(true) .arg(arg!( "ID(s) of the file(s) to extract (required)")), - ), + ) ) .subcommand( Command::new("transfers") @@ -193,9 +204,9 @@ fn cli() -> Command { ) .subcommand( Command::new("clean") - .about("Clear all finshed transfers") + .about("Clear all finished transfers") .long_about( - "Clears all finshed transfers on your account. Does not remove files.", + "Clears all finished transfers on your account. Does not remove files.", ), ) , @@ -327,17 +338,35 @@ fn main() { require_auth(&config); let folder_id_result = sub_matches.get_one::("FOLDER_ID"); + let folder_id = match folder_id_result { Some(folder_id) => folder_id.parse::().expect("parsing folder_id"), None => 0, }; + let files = put::files::list(config.api_token, folder_id).expect("fetching files"); + let should_only_show_self = sub_matches.get_one::("self"); + + if *should_only_show_self.unwrap_or(&false) { + // Only show info for the parent + println!("{}", serde_json::to_string_pretty(&files.parent).unwrap()); + return; + } + if files.parent.file_type != "FOLDER" { println!("The ID provided should be for a folder and not a file"); return; } + let should_return_json = sub_matches.get_one::("json"); + if *should_return_json.unwrap_or(&false) { + // Return in JSON format + println!("{}", serde_json::to_string_pretty(&files.files).unwrap()); + return; + } + + // Return table format let table = Table::new(&files.files).with(Style::markdown()).to_string(); println!("\n# {}\n", &files.parent.name); println!("{}\n", table); diff --git a/src/put/files.rs b/src/put/files.rs index 23a30de..b4cc374 100644 --- a/src/put/files.rs +++ b/src/put/files.rs @@ -2,22 +2,28 @@ use std::fmt; use reqwest::blocking::multipart; use serde::{Deserialize, Serialize}; +use serde_with::{serde_as, DefaultOnNull}; use tabled::Tabled; #[derive(Debug, Serialize, Deserialize)] pub struct FileSize(u64); + impl fmt::Display for FileSize { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", bytefmt::format(self.0)) } } +#[serde_as] #[derive(Debug, Serialize, Deserialize, Tabled)] pub struct File { pub id: u32, pub name: String, pub file_type: String, pub size: FileSize, + pub created_at: String, + #[serde_as(as = "DefaultOnNull")] + pub parent_id: u32, } #[derive(Debug, Serialize, Deserialize)]