Skip to content

Commit

Permalink
feat(main): debug launched clients flag
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed Sep 4, 2024
1 parent 1ca054c commit 4683710
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ struct CliArgs {
#[clap(id = "PRIORITY", short = 'o', long = "overlay", action)]
overlay_priority: Option<u32>,

/// Debug the clients started by the server
#[clap(short = 'd', long = "debug", action)]
debug_launched_clients: bool,

/// Run a script when ready for clients to connect. If this is not set the script at $HOME/.config/stardust/startup will be ran if it exists.
#[clap(id = "PATH", short = 'e', long = "execute-startup-script", action)]
startup_script: Option<PathBuf>,
Expand Down
35 changes: 21 additions & 14 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,47 @@ pub async fn save_session(project_dirs: &ProjectDirs) {

pub fn launch_start(cli_args: &CliArgs, project_dirs: &ProjectDirs) -> Vec<Child> {
match (&cli_args.restore, &cli_args.startup_script) {
(Some(session_id), _) => {
restore_session(&project_dirs.state_dir().unwrap().join(session_id))
}
(None, Some(startup_script)) => {
run_script(&startup_script.clone().canonicalize().unwrap_or_default())
}
(None, None) => run_script(&project_dirs.config_dir().join("startup")),
(Some(session_id), _) => restore_session(
&project_dirs.state_dir().unwrap().join(session_id),
cli_args.debug_launched_clients,
),
(None, Some(startup_script)) => run_script(
&startup_script.clone().canonicalize().unwrap_or_default(),
cli_args.debug_launched_clients,
),
(None, None) => run_script(
&project_dirs.config_dir().join("startup"),
cli_args.debug_launched_clients,
),
}
}

pub fn restore_session(session_dir: &Path) -> Vec<Child> {
pub fn restore_session(session_dir: &Path, debug_launched_clients: bool) -> Vec<Child> {
let Ok(clients) = session_dir.read_dir() else {
return Vec::new();
};
clients
.filter_map(Result::ok)
.filter_map(|c| ClientStateParsed::from_file(&c.path()))
.filter_map(ClientStateParsed::launch_command)
.filter_map(run_client)
.filter_map(|c| run_client(c, debug_launched_clients))
.collect()
}

pub fn run_script(script_path: &Path) -> Vec<Child> {
pub fn run_script(script_path: &Path, debug_launched_clients: bool) -> Vec<Child> {
let _ = std::fs::set_permissions(script_path, std::fs::Permissions::from_mode(0o755));
let startup_command = Command::new(script_path);
run_client(startup_command)
run_client(startup_command, debug_launched_clients)
.map(|c| vec![c])
.unwrap_or_default()
}

pub fn run_client(mut command: Command) -> Option<Child> {
pub fn run_client(mut command: Command, debug_launched_clients: bool) -> Option<Child> {
command.stdin(Stdio::null());
command.stdout(Stdio::null());
command.stderr(Stdio::null());
if !debug_launched_clients {
command.stdout(Stdio::null());
command.stderr(Stdio::null());
}
for (var, value) in connection_env() {
command.env(var, value);
}
Expand Down

0 comments on commit 4683710

Please sign in to comment.