summaryrefslogtreecommitdiff
path: root/src/repo.rs
blob: bd87ea558559a22714e4cfc5c49b32fcd4b2052c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

use colored::Colorize;
use std::fs;
use std::path::Path;
use crate::config::{Config, RepositoryConfig};

pub fn add_repository(name: &str, url: &str, mirror: Option<&str>) -> Result<(), String> {
    let mut cfg = Config::load();
    if cfg.repositories.contains_key(name) {
        return Err(format!("Repository '{}' already exists", name));
    }
    let repo = RepositoryConfig {
        url: url.to_string(),
        mirror: mirror.map(String::from),
    };
    cfg.repositories.insert(name.to_string(), repo);
    save_config(&cfg)?;
    println!("{} Repository '{}' added.", "[ror]".green().bold(), name);
    Ok(())
}

pub fn remove_repository(name: &str) -> Result<(), String> {
    let mut cfg = Config::load();
    if !cfg.repositories.contains_key(name) {
        return Err(format!("Repository '{}' not found", name));
    }
    cfg.repositories.remove(name);
    save_config(&cfg)?;
    println!("{} Repository '{}' removed.", "[ror]".green().bold(), name);
    Ok(())
}

pub fn list_repositories() {
    let cfg = Config::load();
    if cfg.repositories.is_empty() {
        println!("{} No repositories configured.", "[ror]".yellow());
        return;
    }
    println!("{} Configured repositories:", "[ror]".blue().bold());
    for (name, repo) in &cfg.repositories {
        println!("  {} {}", name.green(), repo.url);
        if let Some(m) = &repo.mirror {
            println!("    mirror: {}", m);
        }
    }
}

fn save_config(cfg: &Config) -> Result<(), String> {
    let path = Path::new("/var/ror/ror.conf");
    let ini = cfg.to_ini().map_err(|e| format!("Failed to serialize config: {}", e))?;
    fs::write(path, ini).map_err(|e| format!("Failed to write config: {}", e))?;
    Ok(())
}