ctb_formats_internetarchive/cli.rs
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! CLI execution helpers for Internet Archive commands.
21
22#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate as ctb_formats_internetarchive;
30use anyhow::Context;
31use clap::Subcommand;
32use std::path::PathBuf;
33
34#[derive(Subcommand, Debug)]
35pub enum IACommand {
36 /// Verify a local Internet Archive item directory against its files XML.
37 Verify {
38 /// Path to the item directory, or omit to use the current directory.
39 item_path: Option<PathBuf>,
40 /// Override the identifier if it cannot be inferred from the path.
41 #[arg(long)]
42 identifier: Option<String>,
43 /// Fetch the current files XML from archive.org instead of using a
44 /// local copy.
45 #[arg(long)]
46 check_live: bool,
47 /// Only verify files with source="original".
48 #[arg(long)]
49 original: bool,
50 },
51 /// Print the expected sha1 for a file in an Internet Archive item.
52 Sha1 {
53 /// A local file path, item/file path, or archive.org download URL.
54 target: String,
55 /// Override the identifier if it cannot be inferred from the path.
56 #[arg(long)]
57 identifier: Option<String>,
58 /// Fetch live metadata instead of reading a local files XML.
59 #[arg(long)]
60 check_live: bool,
61 },
62 /// Print the expected md5 for a file in an Internet Archive item.
63 Md5 {
64 /// A local file path, item/file path, or archive.org download URL.
65 target: String,
66 /// Override the identifier if it cannot be inferred from the path.
67 #[arg(long)]
68 identifier: Option<String>,
69 /// Fetch live metadata instead of reading a local files XML.
70 #[arg(long)]
71 check_live: bool,
72 },
73 /// Check whether an item contains a particular file.
74 #[command(name = "contains")]
75 Contains {
76 /// An item identifier, item/file path, or archive.org URL.
77 target: String,
78 /// File path inside the item to check for.
79 desired_file: String,
80 },
81 /// List item files one per line.
82 #[command(name = "listplain")]
83 ListPlain {
84 /// An item identifier or archive.org URL.
85 target: String,
86 },
87 /// Fetch live item metadata as pretty JSON.
88 Metadata {
89 /// An item identifier or archive.org URL.
90 target: String,
91 },
92 /// Fetch the live `_files.xml` document.
93 #[command(name = "filesxml")]
94 FilesXml {
95 /// An item identifier or archive.org URL.
96 target: String,
97 },
98 /// Fetch the live `_meta.xml` document.
99 #[command(name = "metaxml")]
100 MetaXml {
101 /// An item identifier or archive.org URL.
102 target: String,
103 },
104 /// Download an item or file from archive.org (resuming partial `.part` files).
105 Download {
106 /// An item identifier, item/file path, or archive.org download URL.
107 target: String,
108 /// Destination directory. Defaults to the current directory.
109 #[arg(long)]
110 output_dir: Option<PathBuf>,
111 /// Only download files with source="original".
112 #[arg(long)]
113 original: bool,
114 /// Show progress during download
115 #[arg(long, overrides_with = "no_progress")]
116 progress: bool,
117 /// Suppress progress during download
118 #[arg(long, overrides_with = "progress")]
119 no_progress: bool,
120 },
121 /// Download a single file and write it to stdout.
122 #[command(name = "downloadAsStream")]
123 DownloadAsStream {
124 /// An item/file path or archive.org download URL.
125 target: String,
126 },
127 /// Download a single file into the current directory (resuming partial `.part` files).
128 #[command(name = "downloadHere")]
129 DownloadHere {
130 /// An item/file path or archive.org download URL.
131 target: String,
132 /// Destination directory. Defaults to the current directory.
133 #[arg(long)]
134 output_dir: Option<PathBuf>,
135 /// Show progress during download
136 #[arg(long, overrides_with = "no_progress")]
137 progress: bool,
138 /// Suppress progress during download
139 #[arg(long, overrides_with = "progress")]
140 no_progress: bool,
141 },
142 /// Download an item or file (resuming partial `.part` files), then verify checksums.
143 Checkeddl {
144 /// An item identifier, item/file path, or archive.org download URL.
145 target: String,
146 /// Destination directory. Defaults to the current directory.
147 #[arg(long)]
148 output_dir: Option<PathBuf>,
149 /// Only download and verify files with source="original".
150 #[arg(long)]
151 original: bool,
152 /// Show progress during download
153 #[arg(long, overrides_with = "no_progress")]
154 progress: bool,
155 /// Suppress progress during download
156 #[arg(long, overrides_with = "progress")]
157 no_progress: bool,
158 },
159}
160
161pub fn run_internetarchive(cmd: &IACommand) -> Result<ToolResult> {
162 match cmd {
163 IACommand::Verify {
164 item_path,
165 identifier,
166 check_live,
167 original,
168 } => {
169 let item_path = if let Some(item_path) = item_path {
170 item_path.clone()
171 } else {
172 std::env::current_dir()
173 .context("Failed to get current directory")?
174 };
175 Ok(ToolResult::immediate_ok(
176 ctb_formats_internetarchive::verify(
177 item_path.as_path(),
178 identifier.as_deref(),
179 *check_live,
180 *original,
181 )?,
182 ))
183 }
184 IACommand::Sha1 {
185 target,
186 identifier,
187 check_live,
188 } => Ok(ToolResult::immediate_ok(
189 ctb_formats_internetarchive::iasha1(
190 target,
191 identifier.as_deref(),
192 *check_live,
193 )?,
194 )),
195 IACommand::Md5 {
196 target,
197 identifier,
198 check_live,
199 } => Ok(ToolResult::immediate_ok(
200 ctb_formats_internetarchive::iamd5(
201 target,
202 identifier.as_deref(),
203 *check_live,
204 )?,
205 )),
206 IACommand::Contains {
207 target,
208 desired_file,
209 } => Ok(ToolResult::immediate_ok(
210 ctb_formats_internetarchive::contains(target, desired_file)?,
211 )),
212 IACommand::ListPlain { target } => Ok(ToolResult::immediate_ok(
213 ctb_formats_internetarchive::listplain(target)?,
214 )),
215 IACommand::Metadata { target } => Ok(ToolResult::immediate_ok(
216 ctb_formats_internetarchive::metadata(target)?,
217 )),
218 IACommand::FilesXml { target } => Ok(ToolResult::immediate_ok(
219 ctb_formats_internetarchive::filesxml(target)?,
220 )),
221 IACommand::MetaXml { target } => Ok(ToolResult::immediate_ok(
222 ctb_formats_internetarchive::metaxml(target)?,
223 )),
224 IACommand::Download {
225 target,
226 output_dir,
227 original,
228 progress,
229 no_progress,
230 } => {
231 let progress_reporter =
232 ctb_utilities::ui::progress::Progress::from_flags(
233 *progress,
234 *no_progress,
235 );
236 Ok(ToolResult::immediate_ok(
237 ctb_formats_internetarchive::download(
238 target,
239 output_dir.as_deref(),
240 *original,
241 progress_reporter,
242 )?,
243 ))
244 }
245 IACommand::DownloadAsStream { target } => Ok(ToolResult::immediate_ok(
246 ctb_formats_internetarchive::download_as_stream(target)?,
247 )),
248 IACommand::DownloadHere {
249 target,
250 output_dir,
251 progress,
252 no_progress,
253 } => {
254 let progress_reporter =
255 ctb_utilities::ui::progress::Progress::from_flags(
256 *progress,
257 *no_progress,
258 );
259 Ok(ToolResult::immediate_ok(
260 ctb_formats_internetarchive::download_here(
261 target,
262 output_dir.as_deref(),
263 progress_reporter,
264 )?,
265 ))
266 }
267 IACommand::Checkeddl {
268 target,
269 output_dir,
270 original,
271 progress,
272 no_progress,
273 } => {
274 let progress_reporter =
275 ctb_utilities::ui::progress::Progress::from_flags(
276 *progress,
277 *no_progress,
278 );
279 Ok(ToolResult::immediate_ok(
280 ctb_formats_internetarchive::checkeddl(
281 target,
282 output_dir.as_deref(),
283 *original,
284 progress_reporter,
285 )?,
286 ))
287 }
288 }
289}
290
291#[cfg(test)]
292#[allow(
293 clippy::panic,
294 clippy::expect_used,
295 clippy::unwrap_used,
296 clippy::unwrap_in_result,
297 clippy::panic_in_result_fn,
298 clippy::indexing_slicing,
299 clippy::arithmetic_side_effects,
300 reason = "Standard repository test boilerplate"
301)]
302mod tests {
303 use super::*;
304
305 #[crate::ctb_test]
306 fn test_ia_cli() {}
307}