summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/group.rs53
-rw-r--r--src/sync.rs59
2 files changed, 48 insertions, 64 deletions
diff --git a/src/group.rs b/src/group.rs
index 8ce4a88..bc34a68 100644
--- a/src/group.rs
+++ b/src/group.rs
@@ -1,25 +1,12 @@
use serde::Deserialize;
use std::fs;
-use std::path::Path;
+use std::path::{Path, PathBuf};
use colored::Colorize;
use crate::config;
use crate::install;
const GROUPS_DIR: &str = "/var/ror/packages/groups";
-fn sanitize_group_name(name: &str) -> Result<(), String> {
- if name.is_empty() {
- return Err("Group name is empty".to_string());
- }
- if name.contains('/') || name.contains('\\') || name.contains("..") || name.contains('\0') {
- return Err(format!("Invalid group name: '{}' contains path traversal characters", name));
- }
- if name.starts_with('-') {
- return Err(format!("Invalid group name: '{}' starts with dash", name));
- }
- Ok(())
-}
-
#[derive(Debug, Deserialize)]
pub struct Group {
#[allow(dead_code)]
@@ -30,7 +17,7 @@ pub struct Group {
pub packages: Vec<String>,
#[serde(default)]
pub install_steps: String,
-
+
#[serde(rename = "init-system")]
#[allow(dead_code)]
pub init_system: Option<String>,
@@ -47,14 +34,30 @@ pub struct Group {
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) {
- if let Err(e) = sanitize_group_name(group_name) {
- eprintln!("{} {}", "[ror]".red().bold(), e);
- return;
- }
- let group_file = format!("{}/{}.yaml", GROUPS_DIR, group_name);
- if !Path::new(&group_file).exists() {
+ let group_file = resolve_group_path(group_name);
+ if !group_file.exists() {
eprintln!("{} Group '{}' not found!", "[ror]".red().bold(), group_name);
return;
}
@@ -94,11 +97,10 @@ pub fn install_group(group_name: &str, cfg: &config::Config) {
println!("{} Group '{}' installation completed.", "[ror]".blue().bold(), group.name);
}
-pub fn load_group(group_name: &str) -> Result<Group, String> {
- sanitize_group_name(group_name)?;
- let group_file = format!("{}/{}.yaml", GROUPS_DIR, 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: {}", e))?;
+ .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)
@@ -127,4 +129,3 @@ pub fn list_groups() {
}
}
}
-
diff --git a/src/sync.rs b/src/sync.rs
index 2d1b6b7..78687cd 100644
--- a/src/sync.rs
+++ b/src/sync.rs
@@ -59,24 +59,18 @@ pub fn run_sync(cfg: &Config) {
eprintln!("{} {}: {}", "[ror]".red().bold(), name, e);
continue;
}
- if let Some(ref mirror) = repo.mirror {
- if let Err(e) = enforce_repo_https(mirror) {
- eprintln!("{} {}: {}", "[ror]".red().bold(), name, e);
- continue;
- }
- }
println!("{} Syncing repository '{}'...", "[ror]".blue().bold(), name);
let repo_path = Path::new(REPOS_DIR).join(name);
if repo_path.exists() {
println!("{} Updating {}...", ">>>".green(), name);
- if let Err(e) = pull_repo(&repo_path, repo.mirror.as_deref()) {
+ if let Err(e) = pull_repo(&repo_path) {
eprintln!("{} {}", "[ror]".red(), e);
} else {
println!("{} Repository updated.", ">>>".green());
}
} else {
println!("{} Cloning {}...", ">>>".green(), name);
- if let Err(e) = clone_repo(name, &repo.url, repo.mirror.as_deref()) {
+ if let Err(e) = clone_repo(name, &repo.url) {
eprintln!("{} {}", "[ror]".red(), e);
} else {
println!("{} Repository cloned.", ">>>".green());
@@ -85,7 +79,7 @@ pub fn run_sync(cfg: &Config) {
}
}
-fn clone_repo(name: &str, primary: &str, mirror: Option<&str>) -> Result<(), String> {
+fn clone_repo(name: &str, primary: &str) -> Result<(), String> {
let path = Path::new(REPOS_DIR).join(name);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| format!("Failed to create repos dir: {}", e))?;
@@ -100,44 +94,33 @@ fn clone_repo(name: &str, primary: &str, mirror: Option<&str>) -> Result<(), Str
return Ok(());
}
- if let Some(mirror_url) = mirror {
- eprintln!("{} Primary clone failed, trying mirror...", "[ror]".yellow());
- let mirror_status = Command::new("git")
- .args(["clone", mirror_url, path.to_str().unwrap()])
- .status()
- .map_err(|e| format!("Failed to run git clone for mirror: {}", e))?;
-
- if mirror_status.success() {
- return Ok(());
- }
- }
-
Err(format!("Failed to clone repository '{}'", name))
}
-fn pull_repo(repo_path: &Path, mirror: Option<&str>) -> Result<(), String> {
+fn pull_repo(repo_path: &Path) -> Result<(), String> {
let path_str = repo_path.to_str().ok_or("Invalid repo path")?;
- let status = Command::new("git")
- .args(["-C", path_str, "pull"])
+
+ let fetch = Command::new("git")
+ .args(["-C", path_str, "fetch", "origin"])
.status()
- .map_err(|e| format!("Failed to run git pull: {}", e))?;
+ .map_err(|e| format!("Failed to run git fetch: {}", e))?;
- if status.success() {
- return Ok(());
+ if !fetch.success() {
+ return Err(format!("git fetch failed for '{}'", path_str));
}
- if let Some(mirror_url) = mirror {
- eprintln!("{} Pull from primary failed, trying mirror...", "[ror]".yellow());
- let mirror_status = Command::new("git")
- .args(["-C", path_str, "pull", mirror_url])
- .status()
- .map_err(|e| format!("Failed to run git pull from mirror: {}", e))?;
+ let _ = Command::new("git")
+ .args(["-C", path_str, "clean", "-fd"])
+ .status();
- if mirror_status.success() {
- return Ok(());
- }
+ let reset = Command::new("git")
+ .args(["-C", path_str, "reset", "--hard", "FETCH_HEAD"])
+ .status()
+ .map_err(|e| format!("Failed to run git reset: {}", e))?;
+
+ if reset.success() {
+ return Ok(());
}
- Err(format!("Failed to pull repository at '{}'", path_str))
+ Err(format!("Failed to update repository at '{}'", path_str))
}
-