1use crate::error::MarsError;
4use crate::sync::{ResolutionMode, SyncOptions, SyncRequest};
5
6use super::output;
7
8#[derive(Debug, clap::Args)]
10pub struct SyncArgs {
11 #[arg(long)]
13 pub force: bool,
14
15 #[arg(long)]
17 pub diff: bool,
18
19 #[arg(long)]
21 pub frozen: bool,
22
23 #[arg(long, conflicts_with = "no_refresh_models")]
25 pub refresh_models: bool,
26
27 #[arg(long, conflicts_with = "refresh_models")]
29 pub no_refresh_models: bool,
30
31 #[arg(long)]
33 pub no_upgrade_hint: bool,
34}
35
36pub fn run(args: &SyncArgs, ctx: &super::MarsContext, json: bool) -> Result<i32, MarsError> {
38 let request = SyncRequest {
39 resolution: ResolutionMode::Normal,
40 mutation: None,
41 options: SyncOptions {
42 force: args.force,
43 dry_run: args.diff,
44 frozen: args.frozen,
45 refresh_models: args.refresh_models,
46 no_refresh_models: args.no_refresh_models,
47 },
48 };
49
50 let report = crate::sync::execute(ctx, &request)?;
51
52 let no_upgrade_hint = args.no_upgrade_hint || no_upgrade_hint_from_env();
53 output::print_sync_report(&report, json, no_upgrade_hint);
54
55 if report.has_conflicts() { Ok(1) } else { Ok(0) }
56}
57
58fn no_upgrade_hint_from_env() -> bool {
59 match std::env::var("MARS_NO_UPGRADE_HINT") {
60 Ok(value) => value.trim() == "1",
61 Err(_) => false,
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use crate::cli::{Cli, Command};
68 use clap::Parser;
69
70 #[test]
71 fn parses_no_refresh_models() {
72 let cli = Cli::try_parse_from(["mars", "sync", "--no-refresh-models"]).unwrap();
73 let Command::Sync(args) = cli.command else {
74 panic!("expected sync command");
75 };
76 assert!(args.no_refresh_models);
77 }
78
79 #[test]
80 fn parses_refresh_models() {
81 let cli = Cli::try_parse_from(["mars", "sync", "--refresh-models"]).unwrap();
82 let Command::Sync(args) = cli.command else {
83 panic!("expected sync command");
84 };
85 assert!(args.refresh_models);
86 }
87
88 #[test]
89 fn refresh_and_no_refresh_conflict() {
90 assert!(
91 Cli::try_parse_from(["mars", "sync", "--refresh-models", "--no-refresh-models"])
92 .is_err()
93 );
94 }
95
96 #[test]
97 fn parses_no_upgrade_hint() {
98 let cli = Cli::try_parse_from(["mars", "sync", "--no-upgrade-hint"]).unwrap();
99 let Command::Sync(args) = cli.command else {
100 panic!("expected sync command");
101 };
102 assert!(args.no_upgrade_hint);
103 }
104}