1use anyhow::Result;
4use clap::Args as ClapArgs;
5use colored::Colorize;
6
7use crate::core::{ConfigManager, Git, NotesManager};
8
9#[derive(ClapArgs, Debug)]
11pub struct Args {
12 #[arg(default_value = "origin")]
14 pub remote: String,
15
16 #[arg(long)]
18 pub pull: bool,
19
20 #[arg(long)]
22 pub push: bool,
23
24 #[arg(long, short)]
26 pub force: bool,
27}
28
29pub 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 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 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 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}