Skip to main content

chronicle/cli/
summary.rs

1use crate::error::Result;
2use crate::git::CliOps;
3use crate::read::summary::{build_summary, SummaryQuery};
4
5pub fn run(path: String, anchor: Option<String>, format: String) -> Result<()> {
6    let repo_dir = std::env::current_dir().map_err(|e| crate::error::ChronicleError::Io {
7        source: e,
8        location: snafu::Location::default(),
9    })?;
10    let git_ops = CliOps::new(repo_dir);
11
12    let query = SummaryQuery { file: path, anchor };
13
14    let result =
15        build_summary(&git_ops, &query).map_err(|e| crate::error::ChronicleError::Git {
16            source: e,
17            location: snafu::Location::default(),
18        })?;
19
20    let json = if format == "pretty" {
21        serde_json::to_string_pretty(&result)
22    } else {
23        serde_json::to_string(&result)
24    }
25    .map_err(|e| crate::error::ChronicleError::Json {
26        source: e,
27        location: snafu::Location::default(),
28    })?;
29
30    println!("{json}");
31    Ok(())
32}