xvc_pipeline/pipeline/api/
step_output.rs

1use crate::error::Result;
2use log::info;
3use std::path::PathBuf;
4use xvc_core::{XvcPath, XvcRoot};
5use xvc_core::R1NStore;
6
7use crate::{XvcMetricsFormat, XvcOutput, XvcPipeline, XvcStep};
8
9/// Entry point for `xvc pipeline step output` command.
10///
11/// It loads the pipeline and the step with the given names. Then records
12/// `files`, `metrics` and `images` as [XvcOutput]s of the step.
13///
14/// TODO: This can be split like [crate::pipeline::api::step_dependency::XvcDependencyList].
15pub fn cmd_step_output(
16    xvc_root: &XvcRoot,
17    pipeline_name: &str,
18    step_name: String,
19    files: Option<Vec<String>>,
20    metrics: Option<Vec<String>>,
21    images: Option<Vec<String>>,
22) -> Result<()> {
23    let (pipeline_e, _) = XvcPipeline::from_name(xvc_root, pipeline_name)?;
24    let (step_e, step) = XvcStep::from_name(xvc_root, &pipeline_e, &step_name)?;
25    let current_dir = xvc_root.config().current_dir()?;
26    let mut outputs: Vec<XvcOutput> = Vec::new();
27
28    if let Some(output_files) = files {
29        for file in output_files {
30            let pathbuf = PathBuf::from(file);
31            let path = XvcPath::new(xvc_root, current_dir, &pathbuf)?;
32            outputs.push(XvcOutput::File { path });
33        }
34    }
35
36    if let Some(metrics) = metrics {
37        for metric in metrics {
38            let pathbuf = PathBuf::from(metric);
39            let format = XvcMetricsFormat::from_path(&pathbuf);
40            let path = XvcPath::new(xvc_root, current_dir, &pathbuf)?;
41            outputs.push(XvcOutput::Metric { format, path });
42        }
43    }
44
45    if let Some(images) = images {
46        for image in images {
47            let pathbuf = PathBuf::from(image);
48            let path = XvcPath::new(xvc_root, current_dir, &pathbuf)?;
49            outputs.push(XvcOutput::Image { path });
50        }
51    }
52
53    xvc_root.with_r1nstore_mut(|rs: &mut R1NStore<XvcStep, XvcOutput>| {
54        for o in &outputs {
55            rs.insert(step_e, step.clone(), xvc_root.new_entity(), o.clone());
56            info!("Adding {:?}", step.clone());
57        }
58        Ok(())
59    })?;
60
61    Ok(())
62}