Improve handling of pe tool toml files
This commit is contained in:
parent
15b5d5e131
commit
2aede33db6
1 changed files with 40 additions and 16 deletions
|
|
@ -76,7 +76,7 @@ impl App {
|
||||||
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
|
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
|
||||||
let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
|
let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
|
||||||
let mut list = StatefulList::default();
|
let mut list = StatefulList::default();
|
||||||
list.set_items(load_tools());
|
list.set_items(load_tools(action_tx.clone()));
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
// TUI
|
// TUI
|
||||||
action_rx,
|
action_rx,
|
||||||
|
|
@ -441,21 +441,45 @@ fn build_right_items(app: &App) -> Action {
|
||||||
Action::UpdateRight(labels, start_index, items)
|
Action::UpdateRight(labels, start_index, items)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_tools() -> Vec<Tool> {
|
pub fn load_tools(action_tx: mpsc::UnboundedSender<Action>) -> Vec<Tool> {
|
||||||
|
let mut entries: Vec<PathBuf>;
|
||||||
|
let mut tools: Vec<Tool> = Vec::new();
|
||||||
let exe_path = env::current_exe().expect("Failed to find main executable");
|
let exe_path = env::current_exe().expect("Failed to find main executable");
|
||||||
let tool_config_path = exe_path.parent().unwrap().join("menu_entries");
|
let tool_config_path = exe_path.parent().unwrap().join("menu_entries");
|
||||||
let mut entries: Vec<PathBuf> = std::fs::read_dir(tool_config_path)
|
if let Ok(read_dir) = std::fs::read_dir(tool_config_path) {
|
||||||
.expect("Failed to find any tool configs")
|
entries = read_dir
|
||||||
.map(|res| res.map(|e| e.path()))
|
.map(|res| res.map(|e| e.path()))
|
||||||
.filter_map(Result::ok)
|
.filter_map(Result::ok)
|
||||||
.collect();
|
.collect();
|
||||||
entries.sort();
|
entries.sort();
|
||||||
entries
|
} else {
|
||||||
.iter()
|
action_tx
|
||||||
.map(|entry| {
|
.send(Action::Error(String::from(
|
||||||
let contents = fs::read_to_string(entry).expect("Failed to read tool config file");
|
"Failed to find any tool configs",
|
||||||
let tool: Tool = toml::from_str(&contents).expect("Failed to parse tool config file");
|
)))
|
||||||
tool
|
.unwrap();
|
||||||
})
|
entries = Vec::new();
|
||||||
.collect()
|
}
|
||||||
|
entries.iter().for_each(|entry| {
|
||||||
|
if let Ok(toml_str) = fs::read_to_string(entry) {
|
||||||
|
if let Ok(tool) = toml::from_str(&toml_str) {
|
||||||
|
tools.push(tool);
|
||||||
|
} else {
|
||||||
|
action_tx
|
||||||
|
.send(Action::Error(format!(
|
||||||
|
"Failed to parse tool config file: {:?}",
|
||||||
|
&entry,
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
action_tx
|
||||||
|
.send(Action::Error(format!(
|
||||||
|
"Failed to read tool config file: {:?}",
|
||||||
|
&entry,
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
tools
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue