repository configuration reader

This commit is contained in:
neo_1993
2026-09-03 18:15:55 +02:00
parent 0e732deb66
commit 3fcfba2588
5 changed files with 461 additions and 0 deletions
Generated
+186
View File
@@ -2,6 +2,192 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d91e0c145792ef73a6ad36d27c75ac09f1832222a3c209689d90f534685ee5b7"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f09628afdcc538b57f3c6341e9c8e9970f18e4a481690a64974d7023bd33548b"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d012d2b9d65aca7f18f4d9878a045bc17899bba951561ba5ec3c2ba1eed9a061"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "log"
version = "0.4.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ruport"
version = "0.1.0"
dependencies = [
"clap",
"log",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
+36
View File
@@ -3,4 +3,40 @@ name = "ruport"
version = "0.1.0"
edition = "2024"
[features]
default = []
drop-in = []
[lib]
name = "portage"
path = "libportage/lib.rs"
crate-type = ["rlib"]
[[bin]]
name = "ebuild"
path = "src/ebuild.rs"
required-features = ["drop-in"]
[[bin]]
name = "emerge"
path = "src/emerge.rs"
required-features = ["drop-in"]
[[bin]]
name = "emaint"
path = "src/emaint.rs"
required-features = ["drop-in"]
[[bin]]
name = "ruport"
path = "src/main.rs"
[dependencies]
clap = {version="4.6", features=["derive"]}
log = "0.4.33"
[profile.release]
opt-level = 'z'
lto = true
strip = true
panic = "abort"
+16
View File
@@ -0,0 +1,16 @@
use std::io::Result;
use portage::repository::RepoConfig;
fn main() -> Result<()> {
// the repo configuration
let mut repos = RepoConfig::new()?;
// read the config
repos.read_config()?;
// print repo config
repos.print_repos()?;
Ok(())
}
+36
View File
@@ -0,0 +1,36 @@
use std::io::Result;
use clap::ValueEnum;
use crate::repository::RepoConfig;
pub mod config;
pub mod util;
pub mod repository;
#[derive(Debug,Clone,ValueEnum)]
pub enum Action {
Search,
Update,
Remove,
Sync,
Depclean,
Install,
}
/// PortageContext
///
/// holds the system and repository configuration.
/// It is the first thing, you need to create, to have full access to this library.
pub struct PortageContext {
repo_config: RepoConfig,
}
impl PortageContext {
pub fn new() -> Result<Self> {
let mut repo_config = RepoConfig::new()?;
repo_config.read_config()?;
Ok(Self {
repo_config
})
}
}
+187
View File
@@ -0,0 +1,187 @@
use std::env::current_exe;
use std::fmt::Display;
use std::fs::self;
use std::io::{BufRead, BufReader, Result};
use std::{fs::File, path::{Path,PathBuf}};
use std::collections::HashMap;
#[derive(Clone, Default)]
enum SyncType {
#[default]
None,
Rsync(String),
Git(String),
}
impl SyncType {
pub fn get_uri(&self) -> String {
match self {
Self::Git(uri) => uri.to_string(),
Self::Rsync(uri) => uri.to_string(),
Self::None => String::new()
}
}
}
impl Display for SyncType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (typ, uri) = match self {
Self::Git(uri) => ("git", uri.as_str()),
Self::Rsync(uri) => ("rsync", uri.as_str()),
Self::None => ("none",""),
};
write!(f, "{typ} uri: {uri}")
}
}
#[derive(Clone, Default)]
struct RepositoryConfig {
pub name: String,
pub location: PathBuf,
sync_type: SyncType,
auto_sync: bool,
}
pub struct RepoConfig {
main_repo: String,
repos: Vec<RepositoryConfig>,
initialized: bool,
}
impl RepoConfig {
pub fn new() -> Result<Self> {
Ok(Self {
main_repo: String::new(),
repos: Vec::new(),
initialized: false,
})
}
pub fn read_config(&mut self) -> Result<()> {
// test portage internal config
if PathBuf::from("/usr/share/portage/config/repos.conf").exists() {
let configs = self.read_file(PathBuf::from("/usr/share/portage/config/repos.conf"))?;
self.main_repo = String::from(configs[0].name.clone());
self.repos.push(configs[0].clone());
}
// test the portage config directory: /etc/portage/repos.conf
let dir = Path::new("/etc/portage/repos.conf");
// check if directory exists...
if dir.is_dir() {
let entries = fs::read_dir(dir)?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>>>()?;
for entry in entries {
let configs = self.read_file(entry)?;
for config in configs {
self.repos.push(config);
}
}
}
self.initialized = true;
Ok(())
}
fn read_file(&mut self, path: PathBuf) -> Result<Vec<RepositoryConfig>> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
let mut repos = Vec::new();
let mut data = HashMap::<String,HashMap<String,String>>::new();
let mut current_repo = String::new();
for line in reader.lines() {
let line = line.unwrap();
let line = line.trim();
// ignore empty lines
if line.is_empty() {
continue;
}
// ignore comments
if line.starts_with(';') || line.starts_with('#') {
continue;
}
if line.starts_with('[') && line.ends_with(']') {
current_repo = line[1..line.len() -1].to_string();
} else if let Some(index) = line.find('=') {
let key = line[..index].trim().to_string();
let value = line[index +1 ..].trim().to_string();
if !current_repo.is_empty() {
let repo = data.entry(current_repo.clone()).or_insert(HashMap::new());
repo.insert(key, value);
}
}
}
// parse hashmap
println!("data: {:#?}", data);
for d in data {
if d.0 == "DEFAULT" {
let main_repo = d.1.get("main_repo");
if main_repo.is_some() {
self.main_repo = main_repo.unwrap().clone();
}
} else {
let mut repo = RepositoryConfig::default();
// get repo name
repo.name = d.0;
// get repo sync type & uri
let sync_type = d.1.get("sync-type");
let sync_uri = d.1.get("sync-uri");
if sync_type.is_some() && sync_uri.is_some() {
let sync_type = sync_type.unwrap();
let sync_uri = sync_uri.unwrap();
let sync = match sync_type.to_ascii_lowercase().as_str() {
"rsync" => SyncType::Rsync(sync_uri.to_string()),
"git" => SyncType::Git(sync_uri.to_string()),
_ => SyncType::None,
};
repo.sync_type = sync;
}
// get auto-sync option
let auto_sync = d.1.get("auto-sync");
if auto_sync.is_some() {
match auto_sync.unwrap().to_ascii_lowercase().as_str() {
"true" => repo.auto_sync = true,
_ => repo.auto_sync = false,
}
} else {
repo.auto_sync = false;
}
// get repository location
let location = d.1.get("location");
if location.is_some() {
let location = location.unwrap();
let path = PathBuf::from(location.clone());
repo.location = path;
}
// parsed all relevant data, so push the repo config
repos.push(repo);
}
}
Ok(repos)
}
pub fn print_repos(&self) -> Result<()> {
println!("Main Repository: {}", self.main_repo);
println!();
for repo in self.repos.clone().into_iter() {
println!("Repository: {}", repo.name);
println!("\tlocation: {}", repo.location.display());
println!("\tsync-type: {}", repo.sync_type);
println!();
}
Ok(())
}
}