entrenar/cli/commands/research/
bundle.rs1use crate::cli::logging::log;
4use crate::cli::LogLevel;
5use crate::config::BundleArgs;
6use crate::research::{ResearchArtifact, RoCrate};
7
8pub fn run_research_bundle(args: BundleArgs, level: LogLevel) -> Result<(), String> {
9 log(level, LogLevel::Normal, &format!("Bundling RO-Crate: {}", args.output.display()));
10
11 let yaml = std::fs::read_to_string(&args.artifact)
13 .map_err(|e| format!("Failed to read artifact: {e}"))?;
14
15 let artifact: ResearchArtifact =
16 serde_yaml::from_str(&yaml).map_err(|e| format!("Failed to parse artifact: {e}"))?;
17
18 let mut crate_pkg = RoCrate::from_artifact(&artifact, &args.output);
19
20 for file_path in &args.file {
22 let content = std::fs::read_to_string(file_path)
23 .map_err(|e| format!("Failed to read {}: {e}", file_path.display()))?;
24
25 let file_name = file_path
26 .file_name()
27 .and_then(|n| n.to_str())
28 .ok_or_else(|| format!("Invalid file name: {}", file_path.display()))?;
29
30 crate_pkg.add_text_file(file_name, &content);
31 }
32
33 if args.zip {
34 let zip_path = args.output.with_extension("zip");
35 let zip_data = crate_pkg.to_zip().map_err(|e| format!("ZIP error: {e}"))?;
36 std::fs::write(&zip_path, &zip_data).map_err(|e| format!("Failed to write ZIP: {e}"))?;
37
38 log(
39 level,
40 LogLevel::Normal,
41 &format!("RO-Crate ZIP created: {} ({} bytes)", zip_path.display(), zip_data.len()),
42 );
43 } else {
44 crate_pkg.to_directory().map_err(|e| format!("Failed to create directory: {e}"))?;
45
46 log(
47 level,
48 LogLevel::Normal,
49 &format!(
50 "RO-Crate directory created: {} ({} entities)",
51 args.output.display(),
52 crate_pkg.entity_count()
53 ),
54 );
55 }
56
57 Ok(())
58}