From 56a030e6dd8715d01f5bab650a2b70bf09bcea17 Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Thu, 25 Aug 2022 01:15:38 -0500 Subject: [PATCH] Update SystemInfo to support username and shell --- Cargo.lock | 1 + distant-core/Cargo.toml | 1 + distant-core/src/api/local.rs | 6 ++++++ distant-core/src/data/system.rs | 12 ++++++++++++ distant-ssh2/src/api.rs | 5 +++++ src/cli/commands/client/format.rs | 14 +++++++++----- 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38d17a8..fcff2fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -779,6 +779,7 @@ dependencies = [ "tokio", "tokio-util", "walkdir", + "whoami", "winsplit", ] diff --git a/distant-core/Cargo.toml b/distant-core/Cargo.toml index 7cd2fb1..79de0b9 100644 --- a/distant-core/Cargo.toml +++ b/distant-core/Cargo.toml @@ -35,6 +35,7 @@ strum = { version = "0.24.1", features = ["derive"] } tokio = { version = "1.20.1", features = ["full"] } tokio-util = { version = "0.7.3", features = ["codec"] } walkdir = "2.3.2" +whoami = "1.2.1" winsplit = "0.1.0" # Optional dependencies based on features diff --git a/distant-core/src/api/local.rs b/distant-core/src/api/local.rs index 819d816..2cff342 100644 --- a/distant-core/src/api/local.rs +++ b/distant-core/src/api/local.rs @@ -2125,6 +2125,12 @@ mod tests { arch: std::env::consts::ARCH.to_string(), current_dir: std::env::current_dir().unwrap_or_default(), main_separator: std::path::MAIN_SEPARATOR, + username: whoami::username(), + shell: if cfg!(windows) { + std::env::var("ComSpec").unwrap_or_else(|_| String::from("cmd.exe")) + } else { + std::env::var("SHELL").unwrap_or_else(|_| String::from("/bin/sh")) + } } ); } diff --git a/distant-core/src/data/system.rs b/distant-core/src/data/system.rs index fb3d4f0..ddb0576 100644 --- a/distant-core/src/data/system.rs +++ b/distant-core/src/data/system.rs @@ -23,6 +23,12 @@ pub struct SystemInfo { /// Primary separator for path components for the current platform /// as defined in https://doc.rust-lang.org/std/path/constant.MAIN_SEPARATOR.html pub main_separator: char, + + /// Name of the user running the server process + pub username: String, + + /// Default shell tied to user running the server process + pub shell: String, } #[cfg(feature = "schemars")] @@ -40,6 +46,12 @@ impl Default for SystemInfo { arch: env::consts::ARCH.to_string(), current_dir: env::current_dir().unwrap_or_default(), main_separator: std::path::MAIN_SEPARATOR, + username: whoami::username(), + shell: if cfg!(windows) { + env::var("ComSpec").unwrap_or_else(|_| String::from("cmd.exe")) + } else { + env::var("SHELL").unwrap_or_else(|_| String::from("/bin/sh")) + }, } } } diff --git a/distant-ssh2/src/api.rs b/distant-ssh2/src/api.rs index 0b5942c..fbd578f 100644 --- a/distant-ssh2/src/api.rs +++ b/distant-ssh2/src/api.rs @@ -837,6 +837,11 @@ impl DistantApi for SshDistantApi { arch: "".to_string(), current_dir, main_separator: if is_windows { '\\' } else { '/' }, + + // TODO: We should be able to calculate these once the problem described with SIGPIPE + // is resolved, but for now we will just return empty strings + username: "".to_string(), + shell: "".to_string(), }) } } diff --git a/src/cli/commands/client/format.rs b/src/cli/commands/client/format.rs index e596619..ed252e5 100644 --- a/src/cli/commands/client/format.rs +++ b/src/cli/commands/client/format.rs @@ -301,16 +301,20 @@ fn format_shell(data: DistantResponseData) -> Output { arch, current_dir, main_separator, + username, + shell, }) => Output::StdoutLine( format!( concat!( - "Family: {:?}\n", - "Operating System: {:?}\n", - "Arch: {:?}\n", - "Cwd: {:?}\n", + "Family: {:?}", + "Operating System: {:?}", + "Arch: {:?}", + "Cwd: {:?}", "Path Sep: {:?}", + "Username: {:?}", + "Shell: {:?}" ), - family, os, arch, current_dir, main_separator, + family, os, arch, current_dir, main_separator, username, shell ) .into_bytes(), ),