summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorrorka900 <rorka900@gmail.com>2026-03-19 11:44:27 +0000
committerrorka900 <rorka900@gmail.com>2026-03-19 11:44:27 +0000
commit693f5143adb352b9fa1ee262b3537f6e429678f9 (patch)
treeb8c9bc10fc0901bcbfd3c1d5990b70d601a08828 /src/config.rs
downloadror-master.tar.gz
ror-master.zip
Initial commitmaster
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..d79ad28
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,88 @@
+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, Default)]
+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,
+}
+
+fn default_ignore_speed() -> bool { false }
+fn default_strict_gpg() -> bool { false }
+
+#[derive(Debug, Deserialize, Serialize, Default)]
+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("/var/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("".to_string());
+ 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("".to_string());
+ }
+ 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 = r#"[global]
+ignore_speed = false
+strict_gpg = false
+
+[repositories.rorkos]
+url = "https:
+mirror = "https:
+"#;
+ fs::create_dir_all(path.parent().unwrap())?;
+ fs::write(path, ini)
+ }
+}
+