Skip to main content

mars_agents/cli/
sync.rs

1//! `mars sync` — resolve + install (make reality match config).
2
3use crate::error::MarsError;
4use crate::sync::{ResolutionMode, SyncOptions, SyncRequest};
5
6use super::output;
7
8/// Arguments for `mars sync`.
9#[derive(Debug, clap::Args)]
10pub struct SyncArgs {
11    /// Overwrite local modifications for managed files.
12    #[arg(long)]
13    pub force: bool,
14
15    /// Dry run — show what would change.
16    #[arg(long)]
17    pub diff: bool,
18
19    /// Install exactly from lock file, error if stale.
20    #[arg(long)]
21    pub frozen: bool,
22
23    /// Refresh models.dev catalog and harness probes synchronously before sync (blocks until complete).
24    #[arg(long, conflicts_with = "no_refresh_models")]
25    pub refresh_models: bool,
26
27    /// Skip the automatic models-cache refresh during sync.
28    #[arg(long, conflicts_with = "refresh_models")]
29    pub no_refresh_models: bool,
30
31    /// Suppress the post-sync upgrade hint line.
32    #[arg(long)]
33    pub no_upgrade_hint: bool,
34}
35
36/// Run `mars sync`.
37pub fn run(args: &SyncArgs, ctx: &super::MarsContext, json: bool) -> Result<i32, MarsError> {
38    let no_upgrade_hint = args.no_upgrade_hint || no_upgrade_hint_from_env();
39    let request = SyncRequest {
40        resolution: ResolutionMode::Normal,
41        mutation: None,
42        options: SyncOptions {
43            force: args.force,
44            dry_run: args.diff,
45            frozen: args.frozen,
46            refresh_models: args.refresh_models,
47            no_refresh_models: args.no_refresh_models,
48            check_upgrades: !no_upgrade_hint,
49        },
50    };
51
52    let report = crate::sync::execute(ctx, &request)?;
53
54    output::print_sync_report(&report, json, no_upgrade_hint);
55
56    if report.has_conflicts() { Ok(1) } else { Ok(0) }
57}
58
59fn no_upgrade_hint_from_env() -> bool {
60    match std::env::var("MARS_NO_UPGRADE_HINT") {
61        Ok(value) => value.trim() == "1",
62        Err(_) => false,
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use crate::cli::{Cli, Command};
69    use clap::Parser;
70
71    #[test]
72    fn parses_no_refresh_models() {
73        let cli = Cli::try_parse_from(["mars", "sync", "--no-refresh-models"]).unwrap();
74        let Command::Sync(args) = cli.command else {
75            panic!("expected sync command");
76        };
77        assert!(args.no_refresh_models);
78    }
79
80    #[test]
81    fn parses_refresh_models() {
82        let cli = Cli::try_parse_from(["mars", "sync", "--refresh-models"]).unwrap();
83        let Command::Sync(args) = cli.command else {
84            panic!("expected sync command");
85        };
86        assert!(args.refresh_models);
87    }
88
89    #[test]
90    fn refresh_and_no_refresh_conflict() {
91        assert!(
92            Cli::try_parse_from(["mars", "sync", "--refresh-models", "--no-refresh-models"])
93                .is_err()
94        );
95    }
96
97    #[test]
98    fn parses_no_upgrade_hint() {
99        let cli = Cli::try_parse_from(["mars", "sync", "--no-upgrade-hint"]).unwrap();
100        let Command::Sync(args) = cli.command else {
101            panic!("expected sync command");
102        };
103        assert!(args.no_upgrade_hint);
104    }
105}