Skip to main content

zoi_cli/cmd/
sync.rs

1use crate::cli::SetupScope;
2use crate::pkg;
3use anyhow::Result;
4use colored::*;
5
6pub fn run(
7    verbose: bool,
8    fallback: bool,
9    no_pm: bool,
10    force: bool,
11    scope: Option<SetupScope>,
12) -> Result<()> {
13    println!("{} Syncing package databases...", "::".bold().blue());
14
15    if force {
16        println!(
17            "{} Force sync enabled, removing existing databases...",
18            "::".bold().yellow()
19        );
20    }
21
22    let pkg_scope = match scope {
23        Some(SetupScope::User) => Some(crate::pkg::types::Scope::User),
24        Some(SetupScope::System) => Some(crate::pkg::types::Scope::System),
25        None => None,
26    };
27
28    pkg::sync::run(verbose, fallback, no_pm, force, pkg_scope)?;
29
30    println!("{}", "Sync complete.".green());
31    Ok(())
32}
33
34pub fn run_local(verbose: bool, fallback: bool, force: bool, frozen: bool) -> Result<()> {
35    if frozen {
36        crate::pkg::frozen::set_frozen(true);
37    }
38    println!(
39        "{} Syncing project-local package databases...",
40        "::".bold().blue()
41    );
42
43    pkg::sync::run_local(verbose, fallback, force, frozen)?;
44
45    println!("{}", "Local sync complete.".green());
46    Ok(())
47}
48
49pub fn set_registry(url_or_keyword: &str) -> Result<()> {
50    let url_storage;
51    let url = match url_or_keyword {
52        "default" => {
53            url_storage = pkg::config::get_default_registry();
54            &url_storage
55        }
56        "gitlab" => "https://gitlab.com/zillowe/zillwen/zusty/zoidberg.git",
57        "github" => "https://github.com/zillowe/zoidberg.git",
58        "codeberg" => "https://codeberg.org/Zillowe/Zoidberg.git",
59        _ => url_or_keyword,
60    };
61
62    pkg::config::set_default_registry(url)?;
63    println!("Default registry set to: {}", url.cyan());
64    println!("The new registry will be used the next time you run 'zoi sync'");
65    Ok(())
66}
67
68pub fn add_registry(url: &str) -> Result<()> {
69    let mut final_url = url.to_string();
70    let path = std::path::Path::new(url);
71    if path.is_dir() {
72        final_url = std::fs::canonicalize(path)?.to_string_lossy().to_string();
73    }
74
75    pkg::config::add_added_registry(&final_url)?;
76    println!("Registry '{}' added.", final_url.cyan());
77    println!("It will be synced on the next 'zoi sync' run.");
78    Ok(())
79}
80
81pub fn remove_registry(handle: &str) -> Result<()> {
82    pkg::config::remove_added_registry(handle)?;
83    println!("Registry '{}' removed.", handle.cyan());
84    Ok(())
85}
86
87pub fn list_registries() -> Result<()> {
88    let config = crate::pkg::config::read_config()?;
89    let db_root = crate::pkg::resolve::get_db_root()?;
90
91    println!("{} Configured Registries", "::".bold().blue());
92
93    if let Some(default) = config.default_registry {
94        let handle = &default.handle;
95        let mut desc = "".to_string();
96        if !handle.is_empty() {
97            let repo_path = db_root.join(handle);
98            if let Ok(repo_config) = crate::pkg::config::read_repo_config(&repo_path) {
99                desc = format!(" - {}", repo_config.description);
100            }
101        }
102        let handle_str = if handle.is_empty() {
103            "<not synced>".italic().to_string()
104        } else {
105            handle.cyan().to_string()
106        };
107        println!("[Set] {}: {}{}", handle_str, default.url, desc);
108    } else {
109        println!("[Set]: <not set>");
110    }
111
112    if !config.added_registries.is_empty() {
113        println!();
114        for reg in config.added_registries {
115            let handle = &reg.handle;
116            let mut desc = "".to_string();
117            if !handle.is_empty() {
118                let repo_path = db_root.join(handle);
119                if let Ok(repo_config) = crate::pkg::config::read_repo_config(&repo_path) {
120                    desc = format!(" - {}", repo_config.description);
121                }
122            }
123            let handle_str = if handle.is_empty() {
124                "<not synced>".italic().to_string()
125            } else {
126                handle.cyan().to_string()
127            };
128            println!("[Add] {}: {}{}", handle_str, reg.url, desc);
129        }
130    }
131    Ok(())
132}