mirror of
https://github.com/samsonjs/vibetunnel.git
synced 2026-04-27 15:17:38 +00:00
Fix CI: Apply final Rust formatting
This commit is contained in:
parent
6f20d395f5
commit
9b3839e361
2 changed files with 30 additions and 16 deletions
|
|
@ -2,15 +2,18 @@ use anyhow::Result;
|
||||||
use nix::pty::{openpty, Winsize};
|
use nix::pty::{openpty, Winsize};
|
||||||
use nix::unistd::{close, dup2, fork, setsid, ForkResult};
|
use nix::unistd::{close, dup2, fork, setsid, ForkResult};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
use signal_hook::{
|
||||||
|
consts::{SIGINT, SIGTERM},
|
||||||
|
iterator::Signals,
|
||||||
|
};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::os::unix::io::{AsRawFd, RawFd};
|
use std::os::unix::io::{AsRawFd, RawFd};
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
use uuid::Uuid;
|
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use signal_hook::{consts::{SIGTERM, SIGINT}, iterator::Signals};
|
use uuid::Uuid;
|
||||||
|
|
||||||
/// Spawn a terminal session with PTY fallback
|
/// Spawn a terminal session with PTY fallback
|
||||||
pub fn spawn_terminal_via_socket(command: &[String], working_dir: Option<&str>) -> Result<String> {
|
pub fn spawn_terminal_via_socket(command: &[String], working_dir: Option<&str>) -> Result<String> {
|
||||||
|
|
@ -405,7 +408,9 @@ fn handle_pty_session(
|
||||||
// Update session status to exited
|
// Update session status to exited
|
||||||
let session_json_path = format!("{}/session.json", session_dir);
|
let session_json_path = format!("{}/session.json", session_dir);
|
||||||
if let Ok(content) = std::fs::read_to_string(&session_json_path) {
|
if let Ok(content) = std::fs::read_to_string(&session_json_path) {
|
||||||
if let Ok(mut session_info) = serde_json::from_str::<serde_json::Value>(&content) {
|
if let Ok(mut session_info) =
|
||||||
|
serde_json::from_str::<serde_json::Value>(&content)
|
||||||
|
{
|
||||||
session_info["status"] = json!("exited");
|
session_info["status"] = json!("exited");
|
||||||
if let Ok(updated_content) = serde_json::to_string_pretty(&session_info) {
|
if let Ok(updated_content) = serde_json::to_string_pretty(&session_info) {
|
||||||
let _ = std::fs::write(&session_json_path, updated_content);
|
let _ = std::fs::write(&session_json_path, updated_content);
|
||||||
|
|
@ -480,8 +485,12 @@ fn handle_stdin_to_pty(master_fd: RawFd, stdin_path: &str) -> Result<()> {
|
||||||
|
|
||||||
/// Update all running sessions to "exited" status when server shuts down
|
/// Update all running sessions to "exited" status when server shuts down
|
||||||
pub fn update_all_sessions_to_exited() -> Result<()> {
|
pub fn update_all_sessions_to_exited() -> Result<()> {
|
||||||
let control_dir = env::var("TTY_FWD_CONTROL_DIR")
|
let control_dir = env::var("TTY_FWD_CONTROL_DIR").unwrap_or_else(|_| {
|
||||||
.unwrap_or_else(|_| format!("{}/.vibetunnel/control", env::var("HOME").unwrap_or_default()));
|
format!(
|
||||||
|
"{}/.vibetunnel/control",
|
||||||
|
env::var("HOME").unwrap_or_default()
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
if !std::path::Path::new(&control_dir).exists() {
|
if !std::path::Path::new(&control_dir).exists() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
@ -496,13 +505,17 @@ pub fn update_all_sessions_to_exited() -> Result<()> {
|
||||||
if session_json_path.exists() {
|
if session_json_path.exists() {
|
||||||
// Read current session info
|
// Read current session info
|
||||||
if let Ok(content) = std::fs::read_to_string(&session_json_path) {
|
if let Ok(content) = std::fs::read_to_string(&session_json_path) {
|
||||||
if let Ok(mut session_info) = serde_json::from_str::<serde_json::Value>(&content) {
|
if let Ok(mut session_info) =
|
||||||
|
serde_json::from_str::<serde_json::Value>(&content)
|
||||||
|
{
|
||||||
// Update status to exited if it was running
|
// Update status to exited if it was running
|
||||||
if let Some(status) = session_info.get("status").and_then(|s| s.as_str()) {
|
if let Some(status) = session_info.get("status").and_then(|s| s.as_str()) {
|
||||||
if status == "running" {
|
if status == "running" {
|
||||||
session_info["status"] = json!("exited");
|
session_info["status"] = json!("exited");
|
||||||
// Write back the updated session info
|
// Write back the updated session info
|
||||||
if let Ok(updated_content) = serde_json::to_string_pretty(&session_info) {
|
if let Ok(updated_content) =
|
||||||
|
serde_json::to_string_pretty(&session_info)
|
||||||
|
{
|
||||||
let _ = std::fs::write(&session_json_path, updated_content);
|
let _ = std::fs::write(&session_json_path, updated_content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -522,7 +535,8 @@ pub fn setup_shutdown_handler() -> Result<()> {
|
||||||
let shutdown_clone = shutdown.clone();
|
let shutdown_clone = shutdown.clone();
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let mut signals = Signals::new(&[SIGTERM, SIGINT]).expect("Failed to create signals iterator");
|
let mut signals =
|
||||||
|
Signals::new(&[SIGTERM, SIGINT]).expect("Failed to create signals iterator");
|
||||||
|
|
||||||
for sig in signals.forever() {
|
for sig in signals.forever() {
|
||||||
eprintln!("Received signal {:?}, updating session statuses...", sig);
|
eprintln!("Received signal {:?}, updating session statuses...", sig);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue