summaryrefslogtreecommitdiff
path: root/src/group.rs
blob: 6623b40c8a3c8f6a366829b97f84fc2ec409bb6a (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use serde::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};
use colored::Colorize;
use crate::config;
use crate::install;

const GROUPS_DIR: &str = "/var/ror/packages/groups";

#[derive(Debug, Deserialize)]
pub struct Group {
    #[allow(dead_code)]
    pub name: String,
    pub description: Option<String>,
    pub libc: Option<String>,
    pub version: Option<String>,
    pub packages: Vec<String>,
    #[serde(default)]
    #[allow(dead_code)]
    pub install_steps: String,

    #[serde(rename = "init-system")]
    #[allow(dead_code)]
    pub init_system: Option<String>,
    #[serde(rename = "core-utils")]
    #[allow(dead_code)]
    pub core_utils: Option<String>,
    #[allow(dead_code)]
    pub udev: Option<String>,
    #[serde(default)]
    #[allow(dead_code)]
    pub sh: Vec<String>,
    #[serde(default)]
    #[allow(dead_code)]
    pub su: Vec<String>,
}

pub fn resolve_group_path(name_or_path: &str) -> PathBuf {
    let p = Path::new(name_or_path);

    if p.is_absolute() || name_or_path.contains('/') {
        return p.to_path_buf();
    }

    if name_or_path.ends_with(".yaml") {
        let cwd_path = std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join(name_or_path);
        if cwd_path.exists() {
            return cwd_path;
        }
        return PathBuf::from(format!("{}/{}", GROUPS_DIR, name_or_path));
    }

    PathBuf::from(format!("{}/{}.yaml", GROUPS_DIR, name_or_path))
}

#[allow(dead_code)]
pub fn install_group(group_name: &str, cfg: &config::Config) {
    let group_file = resolve_group_path(group_name);
    if !group_file.exists() {
        eprintln!("{} Group '{}' not found!", "[ror]".red().bold(), group_name);
        return;
    }

    let content = match fs::read_to_string(&group_file) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("{} Failed to read group file: {}", "[ror]".red().bold(), e);
            return;
        }
    };

    let group: Group = match serde_yaml::from_str(&content) {
        Ok(g) => g,
        Err(e) => {
            eprintln!("{} Group YAML error: {}", "[ror]".red().bold(), e);
            return;
        }
    };

    println!(
        "{} {} Installing group '{}' ({} packages)",
        "[ror]".blue().bold(),
        ">>>".green(),
        group.name,
        group.packages.len()
    );
    if let Some(desc) = &group.description {
        println!("{} {}", "Description:".yellow(), desc);
    }

    for pkg in &group.packages {
        println!("{} Installing package: {}", ">>>".cyan(), pkg);
        install::install_package(pkg, cfg);
    }

    println!("{} Group '{}' installation completed.", "[ror]".blue().bold(), group.name);
}

pub fn load_group(name_or_path: &str) -> Result<Group, String> {
    let group_file = resolve_group_path(name_or_path);
    let content = fs::read_to_string(&group_file)
        .map_err(|e| format!("Failed to read group file '{}': {}", group_file.display(), e))?;
    let group: Group = serde_yaml::from_str(&content)
        .map_err(|e| format!("Group YAML error: {}", e))?;
    Ok(group)
}

pub fn list_groups() {
    if !Path::new(GROUPS_DIR).exists() {
        println!("{} Groups directory not found.", "[ror]".yellow());
        return;
    }

    println!("{} Available package groups:", "[ror]".blue().bold());
    if let Ok(entries) = std::fs::read_dir(GROUPS_DIR) {
        let mut groups: Vec<String> = entries.flatten()
            .filter_map(|e| e.file_name().to_str().map(|s| s.to_string()))
            .filter(|n| n.ends_with(".yaml"))
            .map(|n| n.trim_end_matches(".yaml").to_string())
            .collect();
        groups.sort();
        for (i, group_name) in groups.iter().enumerate() {
            println!(
                "  {}  {}",
                format!("[{}]", i + 1).cyan().bold(),
                group_name.green()
            );
        }
    }
}