Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed May 7, 2024
1 parent 941a8f8 commit 829da41
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src-tauri/src/device_manager/manager.rs
@@ -1,8 +1,7 @@
use std::fs;
use std::path::{Path, PathBuf};

use libssh_rs::{Session, SshKey, SshResult};
use log::log;
use libssh_rs::{Session, SshKey};
use tokio::fs::{remove_file, File};
use tokio::io::AsyncWriteExt;

Expand Down
10 changes: 1 addition & 9 deletions src-tauri/src/plugins/cmd.rs
Expand Up @@ -13,14 +13,6 @@ use crate::event_channel::{EventChannel, EventHandler};
use crate::session_manager::{Proc, ProcCallback, ProcData, SessionManager};
use crate::spawn_manager::SpawnManager;

#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(tag = "type")]
pub(crate) enum SpawnResult {
Exit { status: i32 },
Signal { signal: u32 },
Closed,
}

#[tauri::command]
async fn exec<R: Runtime>(
app: AppHandle<R>,
Expand Down Expand Up @@ -95,7 +87,7 @@ fn proc_worker<R: Runtime>(
match proc.wait_close(&app.state::<SessionManager>()) {
Ok(r) => {
log::info!("{proc:?} closed with {r:?}");
channel.closed(SpawnResult::Exit { status: r });
channel.closed(r);
}
Err(e) => {
log::warn!("{proc:?} closed with {e:?}");
Expand Down
8 changes: 8 additions & 0 deletions src-tauri/src/session_manager/mod.rs
Expand Up @@ -32,6 +32,14 @@ pub struct ProcData {
pub data: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(tag = "type")]
pub enum ProcResult {
Exit { status: i32 },
Signal { signal: Option<String>, core_dumped: bool },
Closed,
}

pub trait ProcCallback {
fn rx(&self, fd: u32, data: &[u8]);
}
19 changes: 14 additions & 5 deletions src-tauri/src/session_manager/proc.rs
Expand Up @@ -7,7 +7,7 @@ use libssh_rs::Channel;

use crate::conn_pool::ManagedDeviceConnection;
use crate::error::Error;
use crate::session_manager::{Proc, SessionManager};
use crate::session_manager::{Proc, ProcResult, SessionManager};

impl Proc {
pub fn is_ready(&self) -> bool {
Expand Down Expand Up @@ -53,7 +53,7 @@ impl Proc {
return Err(Error::Disconnected);
}

pub fn wait_close(&self, sessions: &SessionManager) -> Result<i32, Error> {
pub fn wait_close(&self, sessions: &SessionManager) -> Result<ProcResult, Error> {
let session: ManagedDeviceConnection;
let (sender, receiver) = channel::<Vec<u8>>();
*self.sender.lock().unwrap() = Some(sender);
Expand Down Expand Up @@ -99,14 +99,23 @@ impl Proc {
self.data(1, &buf[..buf_size])?;
}
}
let status = channel.get_exit_status().unwrap_or(-1);
let mut result = ProcResult::Closed;
if interrupted {
log::debug!("{self:?} channel interrupted by client");
} else {
} else if let Some(status) = channel.get_exit_status() {
log::debug!("{self:?} channel closed with status {status}");
result = ProcResult::Exit { status };
} else if let Some(signal) = channel.get_exit_signal() {
log::debug!("{self:?} channel closed with signal {signal:?}");
result = ProcResult::Signal {
signal: signal.signal_name,
core_dumped: signal.core_dumped,
};
} else {
log::debug!("{self:?} channel closed with unknown status");
}
session.mark_last_ok();
return Ok(status);
return Ok(result);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/app/core/services/remote-command.service.ts
Expand Up @@ -118,11 +118,11 @@ export class CommandSubject<T = Buffer | string> extends ReplaySubject<CommandDa
}
} else if (payload.type === 'Signal') {
// Treat user initiated SIGINT as success
if (this.interrupted && payload.signal === 2) {
if (this.interrupted && payload.signal === "INT") {
zone.run(() => subject.complete());
} else {
zone.run(() => subject.error(new ExecutionError(`Process exited with signal ${payload.signal}`,
128 + payload.signal, this.stderr, command)));
-1, this.stderr, command)));
}
} else {
zone.run(() => subject.error(new Error('Process closed')));
Expand Down Expand Up @@ -169,7 +169,8 @@ declare interface SpawnExited {

declare interface SpawnSignaled {
type: 'Signal';
signal: number;
signal?: string;
coreDumped: boolean;
}

declare interface SpawnClosed {
Expand Down

0 comments on commit 829da41

Please sign in to comment.