2024-09-22 17:50:43 +01:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
io::{BufRead, BufReader, Read},
|
2024-11-06 21:28:17 +00:00
|
|
|
ops::{Deref, DerefMut},
|
2024-09-22 17:50:43 +01:00
|
|
|
os::unix::fs::PermissionsExt,
|
|
|
|
path::{Path, PathBuf},
|
2024-09-30 22:48:22 +01:00
|
|
|
rc::Rc,
|
2024-09-22 17:50:43 +01:00
|
|
|
};
|
2024-09-30 22:48:22 +01:00
|
|
|
|
|
|
|
use crate::{Command, ListNode, Tab};
|
|
|
|
use ego_tree::{NodeMut, Tree};
|
|
|
|
use include_dir::{include_dir, Dir};
|
|
|
|
use serde::Deserialize;
|
2024-10-03 20:20:50 +01:00
|
|
|
use temp_dir::TempDir;
|
2024-09-06 00:39:10 +01:00
|
|
|
|
2024-09-20 01:09:53 +01:00
|
|
|
const TAB_DATA: Dir = include_dir!("$CARGO_MANIFEST_DIR/tabs");
|
2024-09-06 00:39:10 +01:00
|
|
|
|
2024-11-06 21:28:17 +00:00
|
|
|
// Allow the unused TempDir to be stored for later destructor call
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct TabList(pub Vec<Tab>, TempDir);
|
|
|
|
|
|
|
|
// Implement deref to allow Vec<Tab> methods to be called on TabList
|
|
|
|
impl Deref for TabList {
|
|
|
|
type Target = Vec<Tab>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for TabList {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl IntoIterator for TabList {
|
|
|
|
type Item = Tab;
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_tabs(validate: bool) -> TabList {
|
|
|
|
let (temp_dir, tab_files) = TabDirectories::get_tabs();
|
2024-09-06 00:39:10 +01:00
|
|
|
|
2024-10-03 20:20:50 +01:00
|
|
|
let tabs: Vec<_> = tab_files
|
|
|
|
.into_iter()
|
|
|
|
.map(|path| {
|
|
|
|
let directory = path.parent().unwrap().to_owned();
|
|
|
|
let data = std::fs::read_to_string(path).expect("Failed to read tab data");
|
|
|
|
let mut tab_data: TabEntry = toml::from_str(&data).expect("Failed to parse tab data");
|
|
|
|
|
|
|
|
if validate {
|
|
|
|
filter_entries(&mut tab_data.data);
|
|
|
|
}
|
|
|
|
(tab_data, directory)
|
|
|
|
})
|
|
|
|
.collect();
|
2024-09-06 00:39:10 +01:00
|
|
|
|
|
|
|
let tabs: Vec<Tab> = tabs
|
2024-10-03 20:20:50 +01:00
|
|
|
.into_iter()
|
2024-11-05 19:59:57 +00:00
|
|
|
.map(|(TabEntry { name, data }, directory)| {
|
|
|
|
let mut tree = Tree::new(Rc::new(ListNode {
|
|
|
|
name: "root".to_string(),
|
|
|
|
description: String::new(),
|
|
|
|
command: Command::None,
|
|
|
|
task_list: String::new(),
|
|
|
|
multi_select: false,
|
|
|
|
}));
|
|
|
|
let mut root = tree.root_mut();
|
|
|
|
create_directory(data, &mut root, &directory, validate, true);
|
|
|
|
Tab { name, tree }
|
|
|
|
})
|
2024-09-06 00:39:10 +01:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
if tabs.is_empty() {
|
|
|
|
panic!("No tabs found");
|
|
|
|
}
|
2024-11-06 21:28:17 +00:00
|
|
|
TabList(tabs, temp_dir)
|
2024-09-06 00:39:10 +01:00
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
#[derive(Deserialize)]
|
2024-11-06 21:28:17 +00:00
|
|
|
struct TabDirectories {
|
2024-08-15 07:09:31 +01:00
|
|
|
directories: Vec<PathBuf>,
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2024-08-15 07:09:31 +01:00
|
|
|
struct TabEntry {
|
|
|
|
name: String,
|
|
|
|
data: Vec<Entry>,
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2024-08-15 07:09:31 +01:00
|
|
|
struct Entry {
|
2024-08-13 22:55:59 +01:00
|
|
|
name: String,
|
2024-08-11 04:05:19 +01:00
|
|
|
#[allow(dead_code)]
|
2024-08-11 03:52:49 +01:00
|
|
|
#[serde(default)]
|
|
|
|
description: String,
|
|
|
|
#[serde(default)]
|
|
|
|
preconditions: Option<Vec<Precondition>>,
|
2024-09-22 17:50:43 +01:00
|
|
|
#[serde(flatten)]
|
|
|
|
entry_type: EntryType,
|
2024-09-22 17:10:05 +01:00
|
|
|
#[serde(default)]
|
|
|
|
task_list: String,
|
2024-11-05 19:59:57 +00:00
|
|
|
#[serde(default = "default_true")]
|
|
|
|
multi_select: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_true() -> bool {
|
|
|
|
true
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-09-22 17:50:43 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
enum EntryType {
|
|
|
|
#[serde(rename = "entries")]
|
|
|
|
Entries(Vec<Entry>),
|
|
|
|
#[serde(rename = "command")]
|
|
|
|
Command(String),
|
|
|
|
#[serde(rename = "script")]
|
|
|
|
Script(PathBuf),
|
|
|
|
}
|
|
|
|
|
2024-08-15 07:09:31 +01:00
|
|
|
impl Entry {
|
2024-08-11 03:52:49 +01:00
|
|
|
fn is_supported(&self) -> bool {
|
|
|
|
self.preconditions.as_deref().map_or(true, |preconditions| {
|
|
|
|
preconditions.iter().all(
|
|
|
|
|Precondition {
|
|
|
|
matches,
|
|
|
|
data,
|
|
|
|
values,
|
|
|
|
}| {
|
|
|
|
match data {
|
|
|
|
SystemDataType::Environment(var_name) => std::env::var(var_name)
|
|
|
|
.map_or(false, |var| values.contains(&var) == *matches),
|
|
|
|
SystemDataType::File(path) => {
|
|
|
|
std::fs::read_to_string(path).map_or(false, |data| {
|
|
|
|
values
|
|
|
|
.iter()
|
|
|
|
.any(|matching_value| data.contains(matching_value))
|
|
|
|
== *matches
|
|
|
|
})
|
|
|
|
}
|
2024-08-16 16:48:09 +01:00
|
|
|
SystemDataType::CommandExists => values
|
|
|
|
.iter()
|
|
|
|
.all(|command| which::which(command).is_ok() == *matches),
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Precondition {
|
|
|
|
// If true, the data must be contained within the list of values.
|
|
|
|
// Otherwise, the data must not be contained within the list of values
|
|
|
|
matches: bool,
|
|
|
|
data: SystemDataType,
|
|
|
|
values: Vec<String>,
|
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
enum SystemDataType {
|
|
|
|
#[serde(rename = "environment")]
|
|
|
|
Environment(String),
|
|
|
|
#[serde(rename = "file")]
|
|
|
|
File(PathBuf),
|
2024-08-16 16:48:09 +01:00
|
|
|
#[serde(rename = "command_exists")]
|
|
|
|
CommandExists,
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
|
|
|
|
2024-08-13 22:55:59 +01:00
|
|
|
fn filter_entries(entries: &mut Vec<Entry>) {
|
2024-08-15 07:09:31 +01:00
|
|
|
entries.retain_mut(|entry| {
|
|
|
|
if !entry.is_supported() {
|
|
|
|
return false;
|
|
|
|
}
|
2024-09-22 17:50:43 +01:00
|
|
|
if let EntryType::Entries(entries) = &mut entry.entry_type {
|
2024-08-15 07:09:31 +01:00
|
|
|
filter_entries(entries);
|
|
|
|
!entries.is_empty()
|
|
|
|
} else {
|
|
|
|
true
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-08-11 03:52:49 +01:00
|
|
|
|
2024-09-22 17:50:43 +01:00
|
|
|
fn create_directory(
|
|
|
|
data: Vec<Entry>,
|
2024-09-30 22:48:22 +01:00
|
|
|
node: &mut NodeMut<Rc<ListNode>>,
|
2024-09-22 17:50:43 +01:00
|
|
|
command_dir: &Path,
|
|
|
|
validate: bool,
|
2024-11-05 19:59:57 +00:00
|
|
|
parent_multi_select: bool,
|
2024-09-22 17:50:43 +01:00
|
|
|
) {
|
2024-08-13 22:55:59 +01:00
|
|
|
for entry in data {
|
2024-11-05 19:59:57 +00:00
|
|
|
let multi_select = parent_multi_select && entry.multi_select;
|
|
|
|
|
2024-09-22 17:50:43 +01:00
|
|
|
match entry.entry_type {
|
|
|
|
EntryType::Entries(entries) => {
|
2024-09-30 22:48:22 +01:00
|
|
|
let mut node = node.append(Rc::new(ListNode {
|
2024-09-22 17:50:43 +01:00
|
|
|
name: entry.name,
|
|
|
|
description: entry.description,
|
|
|
|
command: Command::None,
|
|
|
|
task_list: String::new(),
|
2024-11-05 19:59:57 +00:00
|
|
|
multi_select,
|
2024-09-30 22:48:22 +01:00
|
|
|
}));
|
2024-11-05 19:59:57 +00:00
|
|
|
create_directory(entries, &mut node, command_dir, validate, multi_select);
|
2024-09-22 17:50:43 +01:00
|
|
|
}
|
|
|
|
EntryType::Command(command) => {
|
2024-09-30 22:48:22 +01:00
|
|
|
node.append(Rc::new(ListNode {
|
2024-09-22 17:50:43 +01:00
|
|
|
name: entry.name,
|
|
|
|
description: entry.description,
|
|
|
|
command: Command::Raw(command),
|
|
|
|
task_list: String::new(),
|
2024-11-05 19:59:57 +00:00
|
|
|
multi_select,
|
2024-09-30 22:48:22 +01:00
|
|
|
}));
|
2024-09-22 17:50:43 +01:00
|
|
|
}
|
|
|
|
EntryType::Script(script) => {
|
|
|
|
let script = command_dir.join(script);
|
|
|
|
if !script.exists() {
|
|
|
|
panic!("Script {} does not exist", script.display());
|
|
|
|
}
|
2024-08-15 07:09:31 +01:00
|
|
|
|
2024-09-22 17:50:43 +01:00
|
|
|
if let Some((executable, args)) = get_shebang(&script, validate) {
|
2024-09-30 22:48:22 +01:00
|
|
|
node.append(Rc::new(ListNode {
|
2024-09-22 17:50:43 +01:00
|
|
|
name: entry.name,
|
|
|
|
description: entry.description,
|
|
|
|
command: Command::LocalFile {
|
|
|
|
executable,
|
|
|
|
args,
|
|
|
|
file: script,
|
|
|
|
},
|
|
|
|
task_list: entry.task_list,
|
2024-11-05 19:59:57 +00:00
|
|
|
multi_select,
|
2024-09-30 22:48:22 +01:00
|
|
|
}));
|
2024-09-22 17:50:43 +01:00
|
|
|
}
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
2024-09-06 00:39:10 +01:00
|
|
|
|
2024-09-22 17:50:43 +01:00
|
|
|
fn get_shebang(script_path: &Path, validate: bool) -> Option<(String, Vec<String>)> {
|
|
|
|
let default_executable = || Some(("/bin/sh".into(), vec!["-e".into()]));
|
|
|
|
|
|
|
|
let script = File::open(script_path).expect("Failed to open script file");
|
|
|
|
let mut reader = BufReader::new(script);
|
|
|
|
|
|
|
|
// Take the first 2 characters from the reader; check whether it's a shebang
|
|
|
|
let mut two_chars = [0; 2];
|
|
|
|
if reader.read_exact(&mut two_chars).is_err() || two_chars != *b"#!" {
|
|
|
|
return default_executable();
|
|
|
|
}
|
|
|
|
|
|
|
|
let first_line = reader.lines().next().unwrap().unwrap();
|
|
|
|
|
|
|
|
let mut parts = first_line.split_whitespace();
|
|
|
|
|
|
|
|
let Some(executable) = parts.next() else {
|
|
|
|
return default_executable();
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_valid = !validate || is_executable(Path::new(executable));
|
|
|
|
|
|
|
|
is_valid.then(|| {
|
|
|
|
let mut args: Vec<String> = parts.map(ToString::to_string).collect();
|
|
|
|
args.push(script_path.to_string_lossy().to_string());
|
|
|
|
(executable.to_string(), args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_executable(path: &Path) -> bool {
|
|
|
|
path.metadata()
|
|
|
|
.map(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2024-11-06 21:28:17 +00:00
|
|
|
impl TabDirectories {
|
2024-10-03 20:20:50 +01:00
|
|
|
fn get_tabs() -> (TempDir, Vec<PathBuf>) {
|
2024-11-06 21:28:17 +00:00
|
|
|
let temp_dir = TempDir::with_prefix("linutil_scripts").unwrap();
|
2024-09-06 00:39:10 +01:00
|
|
|
TAB_DATA
|
|
|
|
.extract(&temp_dir)
|
|
|
|
.expect("Failed to extract the saved directory");
|
|
|
|
|
2024-10-03 20:20:50 +01:00
|
|
|
let tab_files = std::fs::read_to_string(temp_dir.path().join("tabs.toml"))
|
|
|
|
.expect("Failed to read tabs.toml");
|
2024-08-15 07:09:31 +01:00
|
|
|
let data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml");
|
2024-10-03 20:20:50 +01:00
|
|
|
let tab_paths = data
|
|
|
|
.directories
|
2024-09-06 00:39:10 +01:00
|
|
|
.iter()
|
2024-10-03 20:20:50 +01:00
|
|
|
.map(|path| temp_dir.path().join(path).join("tab_data.toml"))
|
|
|
|
.collect();
|
|
|
|
(temp_dir, tab_paths)
|
2024-08-15 07:09:31 +01:00
|
|
|
}
|
|
|
|
}
|