feat: support new profile

This commit is contained in:
GyDi
2022-02-07 17:26:05 +08:00
parent c596c875a6
commit 66b89ee817
5 changed files with 109 additions and 1 deletions

View File

@@ -43,6 +43,42 @@ pub async fn import_profile(
}
}
/// new a profile
/// append a temp profile item file to the `profiles` dir
/// view the temp profile file by using vscode or other editor
#[tauri::command]
pub async fn new_profile(
name: String,
desc: String,
profiles_state: State<'_, ProfilesState>,
) -> Result<(), String> {
let mut profiles = profiles_state.0.lock().unwrap();
let (_, path) = profiles.append_item(name, desc)?;
if !path.exists() {
return Err("the file not found".into());
}
// use vscode first
if let Ok(code) = which::which("code") {
return match Command::new(code).arg(path).status() {
Ok(_) => Ok(()),
Err(_) => Err("failed to open file by VScode".into()),
};
}
// use `open` command
if let Ok(open) = which::which("open") {
return match Command::new(open).arg(path).status() {
Ok(_) => Ok(()),
Err(_) => Err("failed to open file by `open`".into()),
};
}
return Err("failed to open the file, please edit the file manually".into());
}
/// Update the profile
#[tauri::command]
pub async fn update_profile(