feat: new config files

This commit is contained in:
Strix 2023-10-14 22:39:52 +02:00
parent 3902a6beaf
commit d1882114c7
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
7 changed files with 71 additions and 2 deletions

37
pkgr/src/config/mod.rs Normal file
View file

@ -0,0 +1,37 @@
use serde::{Deserialize, Serialize};
use storage::Storage;
mod storage;
pub mod repos;
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub build_by_default: bool,
#[serde(default)]
pub tmp_dir: Option<String>,
#[serde(default)]
pub storage: Storage
}
impl Default for Config {
fn default() -> Config {
Config {
build_by_default: false,
tmp_dir: Some(String::from("/tmp/pkgr")),
storage: Storage::default()
}
}
}
impl Config {
pub fn from_path(path: &str) -> Result<Config, String> {
match std::fs::read_to_string(path) {
Ok(s) => match toml::from_str(&s) {
Ok(c) => Ok(c),
Err(e) => Err(format!("failed to parse config: {}", e)),
},
Err(e) => Err(format!("failed to read config: {}", e)),
}
}
}

25
pkgr/src/config/repos.rs Normal file
View file

@ -0,0 +1,25 @@
use std::collections::HashMap;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Serialize, Deserialize)]
pub struct Repo {
#[serde(default)]
pub name: String,
pub uri: Url
}
impl Default for Repo {
fn default() -> Self {
Repo {
name: String::from("Repo"),
uri: Url::parse("tcp://0.0.0.0:0000").unwrap()
}
}
}
#[derive(Clone, Serialize, Deserialize)]
pub struct RepoFile {
pub repos: HashMap<String, Repo>
}

View file

@ -0,0 +1,20 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Storage {
/// Where the repositories are defined.
#[serde(default)]
pub repo_file: String,
/// Where to store pkgs data
#[serde(default)]
pub data_dir: String
}
impl Default for Storage {
fn default() -> Self {
Storage {
repo_file: String::from("/etc/pkgr.d/repos.toml"),
data_dir: String::from("/var/lib/pkgr/packages"),
}
}
}