summaryrefslogtreecommitdiff
path: root/src/search.rs
blob: fabfb470e9ad626e24a2562c4018d20306499cf8 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use colored::Colorize;
use std::fs;
use std::path::Path;
use crate::install::Package;

const REPOS_ROOT: &str = "/var/ror/packages";

fn load_package_from_path(path: &Path) -> Option<Package> {
    let content = fs::read_to_string(path).ok()?;
    serde_yaml::from_str(&content).ok()
}

fn find_all_package_files() -> Vec<String> {
    let mut results = Vec::new();
    let root = Path::new(REPOS_ROOT);
    let Ok(level1) = fs::read_dir(root) else { return results };

    for entry1 in level1.flatten() {
        let path1 = entry1.path();
        if !path1.is_dir() || path1.file_name().unwrap_or_default() == "groups" {
            continue;
        }

        
        let Ok(level2) = fs::read_dir(&path1) else { continue };
        for entry2 in level2.flatten() {
            let path2 = entry2.path();
            if !path2.is_dir() {
                continue;
            }

            
            let yaml_path = path2.join(path2.file_name().unwrap()).with_extension("yaml");
            if yaml_path.exists() {
                if let Some(s) = yaml_path.to_str() {
                    results.push(s.to_string());
                }
                continue; 
            }

            
            let Ok(level3) = fs::read_dir(&path2) else { continue };
            for entry3 in level3.flatten() {
                let path3 = entry3.path();
                if !path3.is_dir() {
                    continue;
                }
                let yaml_path = path3.join(path3.file_name().unwrap()).with_extension("yaml");
                if yaml_path.exists() {
                    if let Some(s) = yaml_path.to_str() {
                        results.push(s.to_string());
                    }
                }
            }
        }
    }
    results
}

pub fn search_packages(query: &str) {
    let query_lower = query.to_lowercase();
    let files = find_all_package_files();

    if files.is_empty() {
        println!("{} No packages found in repositories.", "[ror]".yellow());
        return;
    }

    let mut matches = Vec::new();
    for file in files {
        if let Some(pkg) = load_package_from_path(Path::new(&file)) {
            let name_lower = pkg.name.to_lowercase();
            let desc_lower = pkg.description.clone().unwrap_or_default().to_lowercase();
            if name_lower.contains(&query_lower) || desc_lower.contains(&query_lower) {
                matches.push(pkg);
            }
        }
    }

    if matches.is_empty() {
        println!("{} No packages matching '{}'.", "[ror]".yellow(), query);
        return;
    }

    println!("{} Packages matching '{}':", "[ror]".blue().bold(), query);
    println!("{:-<60}", "");
    for pkg in matches {
        let name_ver = format!("{} {}", pkg.name, pkg.version).cyan();
        let desc = pkg.description.unwrap_or_default();
        println!("  {}  {}", name_ver, desc);
    }
}