Skip to main content

chronicle/cli/
decisions.rs

1use crate::error::Result;
2use crate::git::CliOps;
3
4/// Run the `git chronicle decisions` command.
5pub fn run(path: Option<String>, format: String, compact: bool) -> 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 = crate::read::decisions::DecisionsQuery { file: path };
13
14    let output = crate::read::decisions::query_decisions(&git_ops, &query).map_err(|e| {
15        crate::error::ChronicleError::Git {
16            source: e,
17            location: snafu::Location::default(),
18        }
19    })?;
20
21    match format.as_str() {
22        "json" => {
23            let json = if compact {
24                let compact_out = serde_json::json!({
25                    "decisions": output.decisions,
26                    "rejected_alternatives": output.rejected_alternatives,
27                });
28                serde_json::to_string_pretty(&compact_out)
29            } else {
30                serde_json::to_string_pretty(&output)
31            }
32            .map_err(|e| crate::error::ChronicleError::Json {
33                source: e,
34                location: snafu::Location::default(),
35            })?;
36            println!("{json}");
37        }
38        _ => {
39            if output.decisions.is_empty() && output.rejected_alternatives.is_empty() {
40                println!("No decisions or rejected alternatives found.");
41                return Ok(());
42            }
43            if !output.decisions.is_empty() {
44                println!("Decisions:");
45                for d in &output.decisions {
46                    println!("  [{}] {}: {}", d.stability, d.what, d.why);
47                    if let Some(ref rw) = d.revisit_when {
48                        println!("    Revisit when: {rw}");
49                    }
50                }
51            }
52            if !output.rejected_alternatives.is_empty() {
53                println!("Rejected alternatives:");
54                for ra in &output.rejected_alternatives {
55                    println!("  - {}: {}", ra.approach, ra.reason);
56                }
57            }
58        }
59    }
60
61    Ok(())
62}