aboutsummaryrefslogtreecommitdiffstats
path: root/src/action.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/action.rs67
1 files changed, 47 insertions, 20 deletions
diff --git a/src/action.rs b/src/action.rs
index cd3b32b..b94c8b3 100644
--- a/src/action.rs
+++ b/src/action.rs
@@ -1,7 +1,11 @@
-use crate::util::{TEMP_CONFIG_PATH, create_config, dir_size, get_editor, is_root, open_in_editor, yn_prompt};
+use crate::config::Config;
+use crate::util::{
+ TEMP_CONFIG_PATH, create_config, dir_size, get_editor, is_root, open_in_editor, yn_prompt,
+};
use git2::Repository;
use std::fs;
use std::path::{Path, PathBuf};
+use std::process::Command;
const BASE_REPO_PATH: &str = "/var/db/forge";
const BASE_CONFIG_PATH: &str = "/etc/forge/packages";
@@ -74,6 +78,32 @@ impl Action {
}
}
+//=========================================
+// command helpers
+//=========================================
+
+fn build(config_path: PathBuf, repo_path: PathBuf) -> Result<(), String> {
+ let config = Config::new(config_path);
+ if let Some(bld) = config.unwrap().build {
+ let status = Command::new(bld)
+ .current_dir(repo_path)
+ .status()
+ .map_err(|e| format!("failed to execute editor: {}", e))?;
+
+ if !status.success() {
+ return Err(format!("editor exited with non-zero status: {}", status));
+ }
+ } else {
+ return Err("no build command found".to_string());
+ }
+
+ Ok(())
+}
+
+//=========================================
+// commands
+//=========================================
+
fn add(url: &str) -> Result<(), String> {
if !is_root() {
return Err("add must be run as root".to_string());
@@ -85,10 +115,7 @@ fn add(url: &str) -> Result<(), String> {
};
let config_name = format!("{repo_name}.toml");
- println!(
- "Creating config: {}",
- config_name
- );
+ println!("Creating config: {}", config_name);
create_config(repo_name)?;
let editor = get_editor();
@@ -96,26 +123,29 @@ fn add(url: &str) -> Result<(), String> {
open_in_editor(&editor, &config_temp)?;
let clone_path = PathBuf::from(BASE_REPO_PATH).join(repo_name);
- let repo = Repository::clone(url, &clone_path).map_err(|e| {
- format!("failed to clone {}: {}", repo_name, e)
- })?;
+ let repo = Repository::clone(url, &clone_path)
+ .map_err(|e| format!("failed to clone {}: {}", repo_name, e))?;
let mut config_path = PathBuf::from(BASE_CONFIG_PATH);
if !config_path.exists() {
- fs::create_dir_all(&config_path).map_err(|e| {
- format!("failed to create config directory: {}", e)
- })?;
+ fs::create_dir_all(&config_path)
+ .map_err(|e| format!("failed to create config directory: {}", e))?;
}
config_path.push(config_name);
- fs::rename(config_temp, config_path).map_err(|e| format!("failed to place config in system directory: {}", e))?;
+ fs::rename(config_temp, &config_path)
+ .map_err(|e| format!("failed to place config in system directory: {}", e))?;
println!(
- "New package initialized at: {}",
+ "New package initialized at: {}\n",
repo.path().to_str().unwrap()
);
+ if yn_prompt("Run build and install commands?") {
+ build(config_path, clone_path)?;
+ }
+
Ok(())
}
@@ -174,12 +204,8 @@ fn remove(packages: Vec<String>) -> Result<(), String> {
if yn_prompt("Proceed with removal?") {
for (name, path, cfg_path) in package_paths {
- fs::remove_dir_all(&path).map_err(|e| {
- format!("failed to remove {}: {}", name, e)
- })?;
- fs::remove_file(&cfg_path).map_err(|e| {
- format!("failed to remove {}: {}", name, e)
- })?;
+ fs::remove_dir_all(&path).map_err(|e| format!("failed to remove {}: {}", name, e))?;
+ fs::remove_file(&cfg_path).map_err(|e| format!("failed to remove {}: {}", name, e))?;
println!("Removed {}", name);
}
}
@@ -190,7 +216,8 @@ fn remove(packages: Vec<String>) -> Result<(), String> {
fn list() -> Result<(), String> {
let config_path = Path::new(BASE_CONFIG_PATH);
for entry in fs::read_dir(config_path)
- .map_err(|e| format!("failed to iterate package directory: {}", e))? {
+ .map_err(|e| format!("failed to iterate package directory: {}", e))?
+ {
let entry = entry.map_err(|e| e.to_string())?;
let path = entry.path();
if path.is_file() {