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
|
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use colored::Colorize;
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct RepositoryConfig {
pub url: String,
#[serde(default)]
pub mirror: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct GlobalConfig {
#[serde(default = "default_ignore_speed")]
pub ignore_speed: bool,
#[serde(default = "default_strict_gpg")]
pub strict_gpg: bool,
#[serde(default)]
pub allow_external_binaries: bool,
#[serde(default = "default_parallel_downloads")]
pub parallel_downloads: usize,
}
impl Default for GlobalConfig {
fn default() -> Self {
Self {
ignore_speed: false,
strict_gpg: false,
allow_external_binaries: false,
parallel_downloads: default_parallel_downloads(),
}
}
}
fn default_ignore_speed() -> bool { false }
fn default_strict_gpg() -> bool { false }
fn default_parallel_downloads() -> usize { 4 }
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct Config {
#[serde(default)]
pub global: GlobalConfig,
#[serde(default)]
pub repositories: HashMap<String, RepositoryConfig>,
}
impl Config {
pub fn load() -> Self {
let path = Path::new("/etc/ror/ror.conf");
if path.exists() {
let content = fs::read_to_string(path).unwrap_or_default();
Self::from_ini(&content).unwrap_or_else(|e| {
eprintln!(
"{} Failed to parse config: {}, using defaults",
"[ror]".red().bold(),
e
);
Config::default()
})
} else {
Config::default()
}
}
pub fn to_ini(&self) -> Result<String, config::ConfigError> {
let mut lines = vec!["[global]".to_string()];
lines.push(format!("ignore_speed = {}", self.global.ignore_speed));
lines.push(format!("strict_gpg = {}", self.global.strict_gpg));
lines.push(format!("parallel_downloads = {}", self.global.parallel_downloads));
lines.push(String::new());
for (name, repo) in &self.repositories {
lines.push(format!("[repositories.{}]", name));
lines.push(format!("url = \"{}\"", repo.url));
if let Some(m) = &repo.mirror {
lines.push(format!("mirror = \"{}\"", m));
}
lines.push(String::new());
}
Ok(lines.join("\n"))
}
pub fn from_ini(ini: &str) -> Result<Self, config::ConfigError> {
let builder = config::Config::builder()
.add_source(config::File::from_str(ini, config::FileFormat::Ini));
let cfg = builder.build()?;
cfg.try_deserialize()
}
pub fn create_default_config(path: &Path) -> std::io::Result<()> {
let ini = concat!(
"[global]\n",
"ignore_speed = false\n",
"strict_gpg = false\n",
"parallel_downloads = 4\n",
"\n",
"[repositories.rorkos]\n",
"url = \"https://github.com/RorkOS/RorkOS_package\"\n",
"mirror = \"https://github.com/rork-os/RorkOS_package\"\n",
);
fs::create_dir_all(path.parent().unwrap())?;
fs::write(path, ini)
}
}
|