feat: support edit profile item

This commit is contained in:
GyDi
2022-01-17 02:16:17 +08:00
parent ceec72b869
commit 0bad1794c4
6 changed files with 61 additions and 5 deletions

View File

@@ -3,11 +3,13 @@ use crate::{
states::{ClashState, ProfilesState, VergeState},
utils::{
config::{read_clash, save_clash},
dirs::app_home_dir,
fetch::fetch_profile,
sysopt::SysProxyConfig,
},
};
use serde_yaml::Mapping;
use std::process::Command;
use tauri::State;
/// get all profiles from `profiles.yaml`
@@ -142,6 +144,34 @@ pub fn patch_profile(
}
}
/// run vscode command to edit the profile
#[tauri::command]
pub fn edit_profile(index: usize, profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
let mut profiles = profiles_state.0.lock().unwrap();
let items = profiles.items.take().unwrap_or(vec![]);
if index >= items.len() {
profiles.items = Some(items);
return Err("the index out of bound".into());
}
let file = items[index].file.clone().unwrap_or("".into());
profiles.items = Some(items);
let path = app_home_dir().join("profiles").join(file);
if !path.exists() {
return Err("failed to open the file".into());
}
match which::which("code") {
Ok(code) => match Command::new(code).arg(path).status() {
Ok(_) => Ok(()),
Err(_) => Err("failed to open file by VScode".into()),
},
Err(_) => Err("please install VScode for edit".into()),
}
}
/// restart the sidecar
#[tauri::command]
pub fn restart_sidecar(

View File

@@ -3,8 +3,6 @@
windows_subsystem = "windows"
)]
extern crate tauri;
mod cmds;
mod core;
mod states;
@@ -82,13 +80,14 @@ fn main() -> std::io::Result<()> {
cmds::get_verge_config,
cmds::patch_verge_config,
// profile
cmds::edit_profile,
cmds::patch_profile,
cmds::import_profile,
cmds::update_profile,
cmds::delete_profile,
cmds::select_profile,
cmds::patch_profile,
cmds::sync_profiles,
cmds::get_profiles,
cmds::sync_profiles,
])
.build(tauri::generate_context!())
.expect("error while running tauri application")