Skip to main content

tracevault_cli/commands/
sync.rs

1use crate::api_client::{resolve_credentials, ApiClient};
2use crate::resolution::{git_remote_url, git_repo_name};
3use std::path::Path;
4
5pub async fn sync_repo(project_root: &Path) -> Result<(), Box<dyn std::error::Error>> {
6    let (server_url, token) = resolve_credentials(project_root);
7
8    let server_url = match server_url {
9        Some(url) => url,
10        None => {
11            eprintln!("No server_url configured. Skipping sync.");
12            return Ok(());
13        }
14    };
15
16    if token.is_none() {
17        eprintln!("Not logged in. Run 'tracevault login' to sync.");
18        return Ok(());
19    }
20
21    let remote = match git_remote_url(project_root) {
22        Some(url) => url,
23        None => {
24            eprintln!("No git remote 'origin' configured. Skipping sync.");
25            return Ok(());
26        }
27    };
28
29    let client = ApiClient::new(&server_url, token.as_deref());
30
31    let repo_name = git_repo_name(project_root);
32
33    match client
34        .register_repo(crate::api_client::RegisterRepoRequest {
35            repo_name,
36            github_url: Some(remote.clone()),
37        })
38        .await
39    {
40        Ok(resp) => {
41            println!(
42                "Repo synced with server (id: {}, remote: {})",
43                resp.repo_id, remote
44            );
45        }
46        Err(e) => {
47            eprintln!("Warning: could not sync repo with server: {e}");
48        }
49    }
50
51    Ok(())
52}