1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 09:13:54 +00:00

feat(username): Try harder to guess if inside ssh (#1964)

* feat: Try harder to guess if inside ssh

* Add test for SSH_CLIENT

* Update documentation on checking ssh connection

* Update docs/config/README.md

Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>

Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
This commit is contained in:
yangsheng6810 2020-12-06 15:15:20 -06:00 committed by GitHub
parent 70dcd33aa2
commit a72977f7d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -2399,6 +2399,12 @@ The module will be shown if any of the following conditions are met:
- The user is currently connected as an SSH session - The user is currently connected as an SSH session
- The variable `show_always` is set to true - The variable `show_always` is set to true
::: tip
SSH connection is detected by checking environment variables
`SSH_CONNECTION`, `SSH_CLIENT`, and `SSH_TTY`. If your SSH host does not set up
these variables, one workaround is to set one of them with a dummy value.
:::
### Options ### Options
| Option | Default | Description | | Option | Default | Description |

View File

@ -13,7 +13,6 @@ use crate::utils;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let user = context.get_env("USER"); let user = context.get_env("USER");
let logname = context.get_env("LOGNAME"); let logname = context.get_env("LOGNAME");
let ssh_connection = context.get_env("SSH_CONNECTION");
const ROOT_UID: Option<u32> = Some(0); const ROOT_UID: Option<u32> = Some(0);
let user_uid = get_uid(); let user_uid = get_uid();
@ -21,7 +20,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("username"); let mut module = context.new_module("username");
let config: UsernameConfig = UsernameConfig::try_load(module.config); let config: UsernameConfig = UsernameConfig::try_load(module.config);
if user != logname || ssh_connection.is_some() || user_uid == ROOT_UID || config.show_always { if user != logname || is_ssh_connection(&context) || user_uid == ROOT_UID || config.show_always
{
let username = user?; let username = user?;
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
@ -55,6 +55,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
} }
} }
fn is_ssh_connection(context: &Context) -> bool {
let ssh_env: Vec<&str> = vec!["SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY"];
ssh_env
.into_iter()
.any(|env| context.get_env(env).is_some())
}
fn get_uid() -> Option<u32> { fn get_uid() -> Option<u32> {
utils::exec_cmd("id", &["-u"])? utils::exec_cmd("id", &["-u"])?
.stdout .stdout
@ -129,6 +136,30 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn ssh_connection_tty() -> io::Result<()> {
let actual = ModuleRenderer::new("username")
.env("USER", "astronaut")
.env("SSH_TTY", "/dev/pts/0")
.collect();
let expected = Some(format!("{} in ", Color::Yellow.bold().paint("astronaut")));
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn ssh_connection_client() -> io::Result<()> {
let actual = ModuleRenderer::new("username")
.env("USER", "astronaut")
.env("SSH_CLIENT", "192.168.0.101 39323 22")
.collect();
let expected = Some(format!("{} in ", Color::Yellow.bold().paint("astronaut")));
assert_eq!(expected, actual);
Ok(())
}
#[test] #[test]
fn show_always() -> io::Result<()> { fn show_always() -> io::Result<()> {
let actual = ModuleRenderer::new("username") let actual = ModuleRenderer::new("username")