Skip to main content

tracevault_cli/commands/
sync.rs

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