Skip to content

Commit

Permalink
chore(shell): retrieve first ancestor shell.
Browse files Browse the repository at this point in the history
  • Loading branch information
pipelight committed Jan 15, 2025
1 parent 60bf67c commit e30e466
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
27 changes: 18 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::prompt::Prompt;
use super::utils::is_ssh_session;
use super::utils::{get_spawning_shell, is_ssh_session};

use owo_colors::colors::*;
use owo_colors::OwoColorize;
use std::env;

use std::process;

use clap::FromArgMatches;
use clap::{Args, Command, Parser, Subcommand, ValueEnum, ValueHint};
use clap::{Args, Command, Parser, ValueEnum, ValueHint};
// use clap_verbosity_flag::{InfoLevel, Verbosity};
use std::process;

// Error Handling
use miette::{IntoDiagnostic, Result};
Expand Down Expand Up @@ -76,10 +75,20 @@ impl Cli {
}
}

let default_shell = env::var("SHELL").into_diagnostic()?;
let mut p = process::Command::new(default_shell);
p.arg("-c").arg(&cli.cmd);
p.spawn().into_diagnostic()?;
// Use a subshell
let shell = get_spawning_shell()?;
let args: Vec<&str> = cli.cmd.split(' ').collect();
let mut p = process::Command::new(&shell.name);
p.arg("-c");
if !args.is_empty() {
p.arg(cli.cmd);
}

p.stdin(process::Stdio::null())
.stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit())
.output()
.into_diagnostic()?;
Ok(())
}
}
5 changes: 2 additions & 3 deletions src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Colors
use super::utils::is_ssh_session;

use owo_colors::colors::*;
use owo_colors::OwoColorize;
use std::iter::repeat_with;

Expand All @@ -10,7 +9,7 @@ use std::env;

use gethostname::gethostname;

use inquire::{validator::Validation, Confirm, Text};
use inquire::{Confirm, Text};

// Error handling
use miette::{Error, IntoDiagnostic, Result};
Expand Down Expand Up @@ -56,7 +55,7 @@ impl Prompt {
short_cmd = self.cmd.clone()
};
let text = format!(
"Do you really want to execute \"{}\" on {}\n",
"Do you really want to execute `{}` on {}\n",
short_cmd.purple(),
hostname.green().bold()
);
Expand Down
53 changes: 51 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use miette::{IntoDiagnostic, Result};
use miette::{Error, Result};
use std::env;
use std::process;
use sysinfo::{Pid, Process, ProcessRefreshKind, RefreshKind, System, UpdateKind};
use sysinfo::{Pid, ProcessRefreshKind, RefreshKind, System, UpdateKind};

pub fn is_ssh_session() -> bool {
if env::var("SSH_TTY").is_ok() {
Expand Down Expand Up @@ -42,6 +42,49 @@ pub fn is_parent_ssh(pid: &u32, s: &System) -> bool {
return false;
}

#[derive(Debug, Clone)]
pub struct FastProc {
pub name: String,
}
/*
* Get the current process spawning shell
*/
pub fn get_spawning_shell() -> Result<FastProc> {
// Get system process list
let s = System::new_with_specifics(
RefreshKind::nothing().with_processes(
ProcessRefreshKind::nothing()
.with_cmd(UpdateKind::Never)
.with_environ(UpdateKind::Never),
),
);
return get_parent_spawning_shell(&process::id(), &s);
}

/*
* Recursive function to get a process parent
*/
pub fn get_parent_spawning_shell(pid: &u32, s: &System) -> Result<FastProc> {
let shells = vec!["sh", "bash", "zsh", "fish", "nu"];

if let Some(process) = s.process(Pid::from(pid.to_owned() as usize)) {
if let Some(parent_pid) = process.parent() {
if let Some(parent) = s.process(parent_pid) {
let name = parent.name().to_str().unwrap();
if shells.contains(&name) {
return Ok(FastProc {
name: name.to_owned(),
});
} else {
return get_parent_spawning_shell(&parent_pid.as_u32(), &s);
}
}
}
}
let message = format!("Couldn't get the spawning shell");
Err(Error::msg(message))
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -50,4 +93,10 @@ mod test {
is_nested_ssh_session();
Ok(())
}
#[test]
fn _get_spawning_shell() -> Result<()> {
let shell = get_spawning_shell()?;
println!("spawning_shell = {:#?}", shell);
Ok(())
}
}

0 comments on commit e30e466

Please sign in to comment.