combine ExecError with LaunchError and use Session::try_get() instead of matching
This commit is contained in:
parent
890f715388
commit
a51b20add7
@ -1,3 +1,4 @@
|
||||
use std::ffi::OsString;
|
||||
use std::process::Command as ChildCommand;
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::process::CommandExt;
|
||||
@ -90,15 +91,28 @@ pub fn exec(args: &ArgMatches) -> Result<(), CliError> {
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let e = cmd.exec(); // never returns if successful
|
||||
Err(ExecError::ExecutionFailed(e))?;
|
||||
Ok(())
|
||||
// cmd.exec() never returns if successful
|
||||
let e = cmd.exec();
|
||||
match e.kind() {
|
||||
std::io::ErrorKind::NotFound => {
|
||||
let name: OsString = cmd_name.into();
|
||||
Err(ExecError::NotFound(name).into())
|
||||
}
|
||||
e => Err(ExecError::ExecutionFailed(e).into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let mut child = cmd.spawn()
|
||||
.map_err(|e| ExecError::ExecutionFailed(e))?;
|
||||
let mut child = match cmd.spawn() {
|
||||
Ok(c) => c,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
let name: OsString = cmd_name.into();
|
||||
return Err(ExecError::NotFound(name).into());
|
||||
}
|
||||
Err(e) => return Err(ExecError::ExecutionFailed(e).into()),
|
||||
};
|
||||
|
||||
let status = child.wait()
|
||||
.map_err(|e| ExecError::ExecutionFailed(e))?;
|
||||
std::process::exit(status.code().unwrap_or(1));
|
||||
|
@ -136,7 +136,7 @@ fn default_term_config() -> TermConfig {
|
||||
{
|
||||
if let Ok(path) = which::which("pwsh.exe") {
|
||||
return TermConfig {
|
||||
name: exe.into(),
|
||||
name: "pwsh.exe".into(),
|
||||
exec: "conhost.exe".into(),
|
||||
args: vec![path.into_os_string()]
|
||||
};
|
||||
|
@ -217,16 +217,6 @@ pub enum RequestError {
|
||||
}
|
||||
|
||||
|
||||
// Errors encountered while running a subprocess via creddy exec
|
||||
#[derive(Debug, ThisError, AsRefStr)]
|
||||
pub enum ExecError {
|
||||
#[error("Please specify a command")]
|
||||
NoCommand,
|
||||
#[error("Failed to execute command: {0}")]
|
||||
ExecutionFailed(#[from] std::io::Error)
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, ThisError, AsRefStr)]
|
||||
pub enum CliError {
|
||||
#[error(transparent)]
|
||||
@ -240,9 +230,11 @@ pub enum CliError {
|
||||
|
||||
// Errors encountered while trying to launch a child process
|
||||
#[derive(Debug, ThisError, AsRefStr)]
|
||||
pub enum LaunchError {
|
||||
pub enum ExecError {
|
||||
#[error("Please specify a command")]
|
||||
NoCommand,
|
||||
#[error("Executable not found: {0:?}")]
|
||||
ExeNotFound(OsString),
|
||||
NotFound(OsString),
|
||||
#[error("Failed to execute command: {0}")]
|
||||
ExecutionFailed(#[from] std::io::Error),
|
||||
#[error(transparent)]
|
||||
@ -342,14 +334,14 @@ impl Serialize for UnlockError {
|
||||
}
|
||||
|
||||
|
||||
impl Serialize for LaunchError {
|
||||
impl Serialize for ExecError {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
let mut map = serializer.serialize_map(None)?;
|
||||
map.serialize_entry("code", self.as_ref())?;
|
||||
map.serialize_entry("msg", &format!("{self}"))?;
|
||||
|
||||
match self {
|
||||
LaunchError::GetCredentials(src) => map.serialize_entry("source", &src)?,
|
||||
ExecError::GetCredentials(src) => map.serialize_entry("source", &src)?,
|
||||
_ => serialize_upstream_err(self, &mut map)?,
|
||||
}
|
||||
map.end()
|
||||
|
@ -82,6 +82,6 @@ pub async fn save_config(config: AppConfig, app_state: State<'_, AppState>) -> R
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn launch_terminal(base: bool) -> Result<(), LaunchError> {
|
||||
pub async fn launch_terminal(base: bool) -> Result<(), ExecError> {
|
||||
terminal::launch(base).await
|
||||
}
|
||||
|
@ -142,21 +142,15 @@ impl AppState {
|
||||
}
|
||||
|
||||
pub async fn serialize_base_creds(&self) -> Result<String, GetCredentialsError> {
|
||||
let session = self.session.read().await;
|
||||
match *session {
|
||||
Session::Unlocked{ref base, ..} => Ok(serde_json::to_string(base).unwrap()),
|
||||
Session::Locked(_) => Err(GetCredentialsError::Locked),
|
||||
Session::Empty => Err(GetCredentialsError::Empty),
|
||||
}
|
||||
let app_session = self.session.read().await;
|
||||
let (base, _session) = app_session.try_get()?;
|
||||
Ok(serde_json::to_string(base).unwrap())
|
||||
}
|
||||
|
||||
pub async fn serialize_session_creds(&self) -> Result<String, GetCredentialsError> {
|
||||
let session = self.session.read().await;
|
||||
match *session {
|
||||
Session::Unlocked{ref session, ..} => Ok(serde_json::to_string(session).unwrap()),
|
||||
Session::Locked(_) => Err(GetCredentialsError::Locked),
|
||||
Session::Empty => Err(GetCredentialsError::Empty),
|
||||
}
|
||||
let app_session = self.session.read().await;
|
||||
let (_bsae, session) = app_session.try_get()?;
|
||||
Ok(serde_json::to_string(session).unwrap())
|
||||
}
|
||||
|
||||
async fn new_session(&self, base: BaseCredentials) -> Result<(), GetSessionError> {
|
||||
|
@ -7,7 +7,7 @@ use crate::errors::*;
|
||||
use crate::state::AppState;
|
||||
|
||||
|
||||
pub async fn launch(use_base: bool) -> Result<(), LaunchError> {
|
||||
pub async fn launch(use_base: bool) -> Result<(), ExecError> {
|
||||
let state = APP.get().unwrap().state::<AppState>();
|
||||
// do all this in a block so we don't hold the lock any longer than necessary
|
||||
let mut cmd = {
|
||||
@ -35,14 +35,9 @@ pub async fn launch(use_base: bool) -> Result<(), LaunchError> {
|
||||
|
||||
match cmd.spawn() {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => {
|
||||
use std::io::ErrorKind::*;
|
||||
if let NotFound = e.kind() {
|
||||
Err(LaunchError::ExeNotFound(cmd.get_program().to_owned()))
|
||||
}
|
||||
else {
|
||||
Err(LaunchError::from(e))
|
||||
}
|
||||
}
|
||||
Err(e) if std::io::ErrorKind::NotFound == e.kind() => {
|
||||
Err(ExecError::NotFound(cmd.get_program().to_owned()))
|
||||
},
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user