aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util.rs56
1 files changed, 56 insertions, 0 deletions
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<u64> {
let mut size = 0;
@@ -19,6 +53,28 @@ pub fn dir_size(path: &Path) -> std::io::Result<u64> {
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();