From 54f329619ea6954b40900cfd7bfb40237a9c8ec8 Mon Sep 17 00:00:00 2001 From: BrewingWeasel Date: Wed, 8 Nov 2023 02:07:27 -0500 Subject: [PATCH] feat: allow automatically running commands at launch #25 --- src-tauri/src/commands.rs | 12 ++++++++++++ src-tauri/src/dictionary.rs | 13 +++---------- src-tauri/src/main.rs | 9 +++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src-tauri/src/commands.rs diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs new file mode 100644 index 0000000..0186677 --- /dev/null +++ b/src-tauri/src/commands.rs @@ -0,0 +1,12 @@ +use std::{ + error::Error, + process::{Command, Output}, +}; + +pub fn run_command(real_command: &str) -> Result> { + if cfg!(target_os = "windows") { + Ok(Command::new("cmd").args(["/C", real_command]).output()?) + } else { + Ok(Command::new("sh").args(["-c", real_command]).output()?) + } +} diff --git a/src-tauri/src/dictionary.rs b/src-tauri/src/dictionary.rs index 349ba73..5b63e8a 100644 --- a/src-tauri/src/dictionary.rs +++ b/src-tauri/src/dictionary.rs @@ -1,8 +1,8 @@ use shared::*; -use std::{error::Error, fs, process::Command}; +use std::{error::Error, fs}; use tauri::State; -use crate::SakinyjeState; +use crate::{commands::run_command, SakinyjeState}; fn get_def_from_file( lemma: &str, @@ -47,14 +47,7 @@ async fn get_def_url(lemma: &str, url: &str) -> Result> { async fn get_def_command(lemma: &str, cmd: &str) -> Result> { let real_command = cmd.replacen("{word}", lemma, 1); - let output = if cfg!(target_os = "windows") { - Command::new("cmd").args(["/C", &real_command]).output()? - } else { - Command::new("sh") - .args(["-c", &real_command]) - .output() - .expect("failed to execute process") - }; + let output = run_command(&real_command)?; Ok(String::from_utf8(output.stdout)?) } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a13de1b..c471047 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,6 +4,7 @@ use crate::{add_to_anki::add_to_anki, dictionary::get_defs, language_parsing::parse_text}; use ankiconnect::get_anki_card_statuses; use chrono::{DateTime, Utc}; +use commands::run_command; use serde::{Deserialize, Serialize}; use shared::{SakinyjeResult, Settings}; use std::{collections::HashMap, fs}; @@ -11,6 +12,7 @@ use tauri::{async_runtime::block_on, GlobalWindowEvent, Manager, State, WindowEv mod add_to_anki; mod ankiconnect; +mod commands; mod dictionary; mod language_parsing; @@ -60,6 +62,13 @@ impl Default for SharedInfo { // TODO: handle error } } + + if let Some(cmds) = &settings.to_run { + for cmd in cmds { + _ = run_command(cmd); + } + } + to_save.last_launched = new_time; Self { to_save, settings } }