git_adr/cli/
sync.rs

1//! Sync ADRs with remote.
2
3use anyhow::Result;
4use clap::Args as ClapArgs;
5use colored::Colorize;
6
7use crate::core::{ConfigManager, Git, NotesManager};
8
9/// Arguments for the sync command.
10#[derive(ClapArgs, Debug)]
11pub struct Args {
12    /// Remote name.
13    #[arg(default_value = "origin")]
14    pub remote: String,
15
16    /// Pull only.
17    #[arg(long)]
18    pub pull: bool,
19
20    /// Push only.
21    #[arg(long)]
22    pub push: bool,
23
24    /// Force push (use with caution).
25    #[arg(long, short)]
26    pub force: bool,
27}
28
29/// Run the sync command.
30///
31/// # Errors
32///
33/// Returns an error if sync fails.
34pub fn run(args: Args) -> Result<()> {
35    let git = Git::new();
36    git.check_repository()?;
37
38    let config = ConfigManager::new(git.clone()).load()?;
39    let notes = NotesManager::new(git, config);
40
41    // Determine what operations to perform
42    let do_push = args.push || !args.pull;
43    let do_fetch = args.pull || !args.push;
44
45    eprintln!("{} Syncing with remote: {}", "→".blue(), args.remote.cyan());
46
47    if do_fetch {
48        eprintln!("  Fetching notes...");
49        match notes.sync(&args.remote, false, true) {
50            Ok(()) => eprintln!("    {} Fetched ADR notes", "✓".green()),
51            Err(e) => {
52                // Fetch failures are often non-fatal (remote might not have notes yet)
53                eprintln!(
54                    "    {} Could not fetch notes: {}",
55                    "!".yellow(),
56                    e.to_string().lines().next().unwrap_or("unknown error")
57                );
58            },
59        }
60    }
61
62    if do_push {
63        eprintln!("  Pushing notes...");
64        match notes.sync(&args.remote, true, false) {
65            Ok(()) => eprintln!("    {} Pushed ADR notes", "✓".green()),
66            Err(e) => {
67                // Push failures are more serious
68                eprintln!(
69                    "    {} Failed to push notes: {}",
70                    "✗".red(),
71                    e.to_string().lines().next().unwrap_or("unknown error")
72                );
73                return Err(e.into());
74            },
75        }
76    }
77
78    eprintln!("{} Sync complete", "✓".green());
79
80    Ok(())
81}