From 477f40bf969c6de2955cdaaaa571c7b7e9838f06 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Thu, 26 Feb 2026 00:29:43 -0500 Subject: updates to add, remove and list --- src/util.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index c3d573c..ae114d0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,41 @@ +use std::env; use std::fs; use std::io; use std::io::Write; use std::path::Path; +use std::path::PathBuf; +use std::process::Command; +use libc; + +pub const TEMP_CONFIG_PATH: &str = "/var/lib/forge/.tmp"; + +pub fn create_config(package: &str) -> Result<(), String> { + let filename = format!("{package}.toml"); + let mut path = PathBuf::from(TEMP_CONFIG_PATH); + + if !path.exists() { + fs::create_dir_all(&path).map_err(|e| { + format!("failed to create temp config directory: {}", e) + })?; + } + + path.push(filename); + + let template = format!( + r#"# {package} configuration +update = "tagged" # no | live | tagged +build = "make" +dependencies = [] +install = "make install" + "# + ); + + fs::write(path, template).map_err(|e| { + format!("failed to create config: {}", e) + })?; + + Ok(()) +} pub fn dir_size(path: &Path) -> std::io::Result { let mut size = 0; @@ -19,6 +53,28 @@ pub fn dir_size(path: &Path) -> std::io::Result { Ok(size) } +pub fn get_editor() -> String { + env::var("VISUAL") + .or_else(|_| env::var("EDITOR")) + .unwrap_or_else(|_| "nano".to_string()) +} + +pub fn is_root() -> bool { + unsafe { libc::geteuid() == 0} +} + +pub fn open_in_editor(editor: &str, file: &str) -> Result<(), String> { + let status = Command::new(editor).arg(file).status().map_err(|e| { + format!("failed to execute editor: {}", e) + })?; + + if !status.success() { + return Err(format!("editor exited with non-zero status: {}", status)); + } + + Ok(()) +} + pub fn yn_prompt(prompt: &str) -> bool { print!("{} [y/n]: ", prompt); io::stdout().flush().unwrap(); -- cgit v1.2.3-59-g8ed1b