Skip to main content

chronicle/cli/
export.rs

1use std::fs::File;
2use std::io::{self, BufWriter};
3
4use crate::error::Result;
5use crate::export::export_annotations;
6use crate::git::CliOps;
7
8/// Run `git chronicle export`.
9pub fn run(output: Option<String>) -> Result<()> {
10    let repo_dir = std::env::current_dir().map_err(|e| crate::error::ChronicleError::Io {
11        source: e,
12        location: snafu::Location::default(),
13    })?;
14    let git_ops = CliOps::new(repo_dir);
15
16    let count = match output {
17        Some(path) => {
18            let file = File::create(&path).map_err(|e| crate::error::ChronicleError::Io {
19                source: e,
20                location: snafu::Location::default(),
21            })?;
22            let mut writer = BufWriter::new(file);
23            let c = export_annotations(&git_ops, &mut writer)?;
24            eprintln!("exported {c} annotations to {path}");
25            c
26        }
27        None => {
28            let stdout = io::stdout();
29            let mut writer = BufWriter::new(stdout.lock());
30            let c = export_annotations(&git_ops, &mut writer)?;
31            eprintln!("exported {c} annotations");
32            c
33        }
34    };
35
36    if count == 0 {
37        eprintln!("no annotations found to export");
38    }
39
40    Ok(())
41}