Move temporary run scripts from /tmp to data_dir/.run to avoid Gatekeeper prompts

This commit is contained in:
SW 2026-06-15 12:22:45 +02:00
parent 67a37490e2
commit 44ebb4054b
2 changed files with 23 additions and 10 deletions

View file

@ -1,12 +1,13 @@
use std::fs;
use std::io::Write; use std::io::Write;
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::path::Path; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
use crate::config::Terminal; use crate::config::Terminal;
use crate::parser::{CommandFile, Section}; use crate::parser::{CommandFile, Section};
pub fn run_section(section: &Section, file: &CommandFile, terminal: &Terminal) { pub fn run_section(section: &Section, file: &CommandFile, terminal: &Terminal, data_dir: &Path) {
let commands = if section.is_run_all() { let commands = if section.is_run_all() {
file.sections file.sections
.iter() .iter()
@ -22,29 +23,41 @@ pub fn run_section(section: &Section, file: &CommandFile, terminal: &Terminal) {
} }
let script = build_script(&commands); let script = build_script(&commands);
execute_in_terminal(&script, terminal); execute_in_terminal(&script, terminal, data_dir);
} }
fn build_script(commands: &[String]) -> String { fn build_script(commands: &[String]) -> String {
commands.join("\n") commands.join("\n")
} }
fn execute_in_terminal(script: &str, terminal: &Terminal) { fn script_dir(data_dir: &Path) -> PathBuf {
let tmp_dir = std::env::temp_dir(); data_dir.join(".run")
let script_path = tmp_dir.join(format!("lines_run_{}.sh", random_suffix())); }
let mut file = match std::fs::File::create(&script_path) { fn execute_in_terminal(script: &str, terminal: &Terminal, data_dir: &Path) {
let run_dir = script_dir(data_dir);
if fs::create_dir_all(&run_dir).is_err() {
return;
}
let script_path = run_dir.join(format!("lines_run_{}.sh", random_suffix()));
let mut file = match fs::File::create(&script_path) {
Ok(f) => f, Ok(f) => f,
Err(_) => return, Err(_) => return,
}; };
let script_with_cleanup = format!("{}\nrm -f {}", script, escape_shell(&script_path.to_string_lossy())); let script_with_cleanup = format!(
"{}\nrm -f {}",
script,
escape_shell(&script_path.to_string_lossy())
);
if file.write_all(script_with_cleanup.as_bytes()).is_err() { if file.write_all(script_with_cleanup.as_bytes()).is_err() {
return; return;
} }
let _ = std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755)); let _ = fs::set_permissions(&script_path, fs::Permissions::from_mode(0o755));
match terminal { match terminal {
Terminal::Ghostty => run_in_ghostty(&script_path), Terminal::Ghostty => run_in_ghostty(&script_path),

View file

@ -43,7 +43,7 @@ pub fn handle_event(app: &mut App, event: AppEvent) -> bool {
} }
KeyCode::Enter => { KeyCode::Enter => {
if let (Some(section), Some(file)) = (app.current_section(), app.current_file()) { if let (Some(section), Some(file)) = (app.current_section(), app.current_file()) {
run_section(section, file, &app.terminal); run_section(section, file, &app.terminal, &app.data_dir);
} }
} }
_ => {} _ => {}