Skip to content

Commit 831188a

Browse files
committed
add error handling for non-Windows
1 parent 0dd3500 commit 831188a

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

sshdconfig/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ lto = true
1717
atty = { version = "0.2" }
1818
chrono = { version = "0.4" }
1919
clap = { version = "4.5", features = ["derive"] }
20-
registry_lib = { path = "../registry_lib" }
2120
schemars = "0.9"
2221
serde = { version = "1.0", features = ["derive"] }
2322
serde_json = { version = "1.0", features = ["preserve_order"] }
@@ -28,5 +27,8 @@ tree-sitter = "0.25"
2827
tree-sitter-rust = "0.24"
2928
tree-sitter-ssh-server-config = { path = "../tree-sitter-ssh-server-config" }
3029

30+
[target.'cfg(windows)'.dependencies]
31+
registry_lib = { path = "../registry_lib" }
32+
3133
[build-dependencies]
3234
cc="*"

sshdconfig/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub enum SshdConfigError {
1717
ParserError(String),
1818
#[error("Parser Int: {0}")]
1919
ParseIntError(#[from] std::num::ParseIntError),
20+
#[cfg(windows)]
2021
#[error("Registry: {0}")]
2122
RegistryError(#[from] registry_lib::error::RegistryError),
2223
}

sshdconfig/src/get.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
#[cfg(windows)]
45
use registry_lib::{config::{Registry, RegistryValueData}, RegistryHelper};
56

67
use crate::args::DefaultShell;
@@ -17,6 +18,7 @@ pub fn invoke_get() -> Result<(), SshdConfigError> {
1718
Ok(())
1819
}
1920

21+
#[cfg(windows)]
2022
fn get_default_shell() -> Result<(), SshdConfigError> {
2123
let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some("DefaultShell".to_string()), None)?;
2224
let default_shell: Registry = registry_helper.get()?;
@@ -74,3 +76,8 @@ fn get_default_shell() -> Result<(), SshdConfigError> {
7476
println!("{output}");
7577
Ok(())
7678
}
79+
80+
#[cfg(not(windows))]
81+
pub fn get_default_shell() -> Result<(), SshdConfigError> {
82+
Err(SshdConfigError::InvalidInput("Windows registry operations not applicable to this platform".to_string()))
83+
}

sshdconfig/src/set.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
#[cfg(windows)]
45
use registry_lib::{config::RegistryValueData, RegistryHelper};
6+
57
use std::path::Path;
68

79
use crate::args::DefaultShell;
@@ -24,6 +26,7 @@ pub fn invoke_set(input: &str) -> Result<(), SshdConfigError> {
2426
}
2527
}
2628

29+
#[cfg(windows)]
2730
fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_arguments: Option<bool>, shell_arguments: Option<Vec<String>>) -> Result<(), SshdConfigError> {
2831
if let Some(shell) = shell {
2932
let shell_path = Path::new(&shell);
@@ -65,6 +68,11 @@ fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_a
6568
Ok(())
6669
}
6770

71+
#[cfg(not(windows))]
72+
pub fn set_default_shell() -> Result<(), SshdConfigError> {
73+
Err(SshdConfigError::InvalidInput("Windows registry operations not applicable to this platform".to_string()))
74+
}
75+
6876
fn set_registry(name: &str, data: RegistryValueData) -> Result<(), SshdConfigError> {
6977
let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some(name.to_string()), Some(data))?;
7078
registry_helper.set()?;

sshdconfig/tests/defaultshell.tests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,19 @@ Describe 'Default Shell Configuration Tests' -Skip:(!$IsWindows) {
198198
}
199199
}
200200
}
201+
202+
Describe 'Default Shell Configuration Error Handling on Non-Windows Platforms' -Skip:($IsWindows) {
203+
It 'Should return error for set command' {
204+
$inputConfig = @{ shell = $null } | ConvertTo-Json
205+
206+
$out = sshdconfig set --input $inputConfig 2>&1
207+
$LASTEXITCODE | Should -Not -Be 0
208+
$out | Should -BeLike '*not applicable to this platform*'
209+
}
210+
211+
It 'Should return error for get command' {
212+
$out = sshdconfig get 2>&1
213+
$LASTEXITCODE | Should -Not -Be 0
214+
$out | Should -BeLike '*not applicable to this platform*'
215+
}
216+
}

0 commit comments

Comments
 (0)