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 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}