Skip to content

Commit eff46ae

Browse files
committed
fix clippy
1 parent 456b89f commit eff46ae

File tree

7 files changed

+43
-57
lines changed

7 files changed

+43
-57
lines changed

build.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ $filesForWindowsPackage = @(
6060
'RunCommandOnSet.dsc.resource.json',
6161
'RunCommandOnSet.exe',
6262
'sshdconfig.exe',
63-
'sshdconfig.dsc.resource.json',
63+
'sshd.dsc.resource.json',
64+
'sshd_config.dsc.resource.json',
6465
'windowspowershell.dsc.resource.json',
6566
'wmi.dsc.resource.json',
6667
'wmi.resource.ps1',
@@ -87,7 +88,8 @@ $filesForLinuxPackage = @(
8788
'RunCommandOnSet.dsc.resource.json',
8889
'runcommandonset',
8990
'sshdconfig',
90-
'sshdconfig.dsc.resource.json'
91+
'sshd.dsc.resource.json',
92+
'sshd_config.dsc.resource.json'
9193
)
9294

9395
$filesForMacPackage = @(
@@ -109,7 +111,8 @@ $filesForMacPackage = @(
109111
'RunCommandOnSet.dsc.resource.json',
110112
'runcommandonset',
111113
'sshdconfig',
112-
'sshdconfig.dsc.resource.json'
114+
'sshd.dsc.resource.json',
115+
'sshd_config.dsc.resource.json'
113116
)
114117

115118
# the list of files other than the binaries which need to be executable

registry_lib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl RegistryHelper {
5757
///
5858
/// * `RegistryError` - The error that occurred.
5959
pub fn new(key_path: &str, value_name: Option<String>, value_data: Option<RegistryValueData>) -> Result<Self, RegistryError> {
60-
let (hive, subkey) = get_hive_from_path(&key_path)?;
60+
let (hive, subkey) = get_hive_from_path(key_path)?;
6161
let config = Registry {
6262
key_path: key_path.to_string(),
6363
value_name,

sshdconfig/src/args.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
use clap::{Parser, Subcommand};
5+
use schemars::JsonSchema;
56
use serde::{Deserialize, Serialize};
67

78
#[derive(Parser)]
@@ -12,18 +13,23 @@ pub struct Args {
1213

1314
#[derive(Subcommand)]
1415
pub enum Command {
15-
/// Export sshd_config
16+
/// Export `sshd_config`
1617
Export,
17-
/// Get default shell, eventually to be used for sshd_config and repeatable keywords
18+
/// Get default shell, eventually to be used for `sshd_config` and repeatable keywords
1819
Get,
19-
/// Set default shell, eventually to be used for sshd_config and repeatable keywords
20+
Schema {
21+
// Used to inform which schema to generate
22+
#[clap(long, hide = true)]
23+
as_global: bool,
24+
},
25+
/// Set default shell, eventually to be used for `sshd_config` and repeatable keywords
2026
Set {
2127
#[clap(short = 'i', long, help = "input to set in sshd_config")]
2228
input: String
2329
},
2430
}
2531

26-
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
32+
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
2733
pub struct DefaultShell {
2834
pub shell: Option<String>,
2935
pub cmd_option: Option<String>,

sshdconfig/src/get.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,37 +74,3 @@ fn get_default_shell() -> Result<(), SshdConfigError> {
7474
println!("{output}");
7575
Ok(())
7676
}
77-
78-
#[cfg(test)]
79-
mod tests {
80-
use super::*;
81-
use registry_lib::config::RegistryValueData;
82-
83-
#[test]
84-
fn test_parse_shell_command() {
85-
let (shell, args) = parse_shell_command(r#"C:\Program Files\PowerShell\pwsh.exe -NoProfile"#);
86-
assert_eq!(shell, r#"C:\Program Files\PowerShell\pwsh.exe"#);
87-
assert_eq!(args, vec!["-NoProfile"]);
88-
}
89-
90-
#[test]
91-
fn test_parse_shell_command_with_quotes() {
92-
let (shell, args) = parse_shell_command(r#""C:\Program Files\PowerShell\pwsh.exe" -NoProfile -Command"#);
93-
assert_eq!(shell, r#"C:\Program Files\PowerShell\pwsh.exe"#);
94-
assert_eq!(args, vec!["-NoProfile", "-Command"]);
95-
}
96-
97-
#[test]
98-
fn test_extract_string_value_string() {
99-
let value = RegistryValueData::String("test".to_string());
100-
let result = extract_string_value(value, "test_field").unwrap();
101-
assert_eq!(result, Some("test".to_string()));
102-
}
103-
104-
#[test]
105-
fn test_extract_string_value_multistring() {
106-
let value = RegistryValueData::MultiString(vec!["first".to_string(), "second".to_string()]);
107-
let result = extract_string_value(value, "test_field").unwrap();
108-
assert_eq!(result, Some("first".to_string()));
109-
}
110-
}

sshdconfig/src/main.rs

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

4-
use args::{Args, Command};
54
use clap::{Parser};
5+
use schemars::schema_for;
6+
7+
use args::{Args, Command, DefaultShell};
68
use export::invoke_export;
79
use get::invoke_get;
10+
use parser::SshdConfigParser;
811
use set::invoke_set;
912

1013
mod args;
@@ -23,6 +26,15 @@ fn main() {
2326
Command::Export => invoke_export(),
2427
Command::Get => invoke_get(),
2528
Command::Set { input } => invoke_set(input),
29+
Command::Schema { as_global } => {
30+
let schema = if *as_global {
31+
schema_for!(DefaultShell)
32+
} else {
33+
schema_for!(SshdConfigParser)
34+
};
35+
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
36+
Ok(())
37+
}
2638
};
2739

2840
if let Err(e) = result {

sshdconfig/src/set.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,38 @@ use crate::error::SshdConfigError;
1212
/// # Errors
1313
///
1414
/// This function will return an error if the desired settings cannot be applied.
15-
pub fn invoke_set(input: &String) -> Result<(), SshdConfigError> {
15+
pub fn invoke_set(input: &str) -> Result<(), SshdConfigError> {
1616
match serde_json::from_str::<DefaultShell>(input) {
1717
Ok(default_shell) => {
18-
set_default_shell(&default_shell.shell, &default_shell.cmd_option, &default_shell.escape_arguments, &default_shell.shell_arguments)
18+
set_default_shell(default_shell.shell, default_shell.cmd_option, default_shell.escape_arguments, default_shell.shell_arguments)
1919
},
2020
Err(e) => {
2121
// TODO: handle other commands like repeatable keywords or sshd_config modifications
22-
return Err(SshdConfigError::InvalidInput(format!("Failed to parse input as DefaultShell: {}", e)));
22+
Err(SshdConfigError::InvalidInput(format!("Failed to parse input as DefaultShell: {e}")))
2323
}
2424
}
2525
}
2626

27-
fn set_default_shell(shell: &Option<String>, cmd_option: &Option<String>, escape_arguments: &Option<bool>, shell_arguments: &Option<Vec<String>>) -> Result<(), SshdConfigError> {
27+
fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_arguments: Option<bool>, shell_arguments: Option<Vec<String>>) -> Result<(), SshdConfigError> {
2828
if let Some(shell) = shell {
29-
let shell_path = Path::new(shell);
30-
if shell_path.is_relative() {
31-
if shell_path.components().any(|c| c == std::path::Component::ParentDir) {
32-
return Err(SshdConfigError::InvalidInput("shell path must not be relative".to_string()));
33-
}
29+
let shell_path = Path::new(&shell);
30+
if shell_path.is_relative() && shell_path.components().any(|c| c == std::path::Component::ParentDir) {
31+
return Err(SshdConfigError::InvalidInput("shell path must not be relative".to_string()));
3432
}
3533
if !shell_path.exists() {
36-
return Err(SshdConfigError::InvalidInput(format!("shell path does not exist: {}", shell)));
34+
return Err(SshdConfigError::InvalidInput(format!("shell path does not exist: {shell}")));
3735
}
3836

3937
let mut shell_data = shell.clone();
4038
if let Some(shell_args) = shell_arguments {
4139
let args_str = shell_args.join(" ");
42-
shell_data = format!("{} {}", shell, args_str);
40+
shell_data = format!("{shell} {args_str}");
4341
}
4442

4543
set_registry("DefaultShell", RegistryValueData::String(shell_data))?;
4644
} else {
4745
remove_registry("DefaultShell")?;
48-
};
46+
}
4947

5048

5149
if let Some(cmd_option) = cmd_option {
@@ -56,7 +54,7 @@ fn set_default_shell(shell: &Option<String>, cmd_option: &Option<String>, escape
5654

5755
if let Some(escape_args) = escape_arguments {
5856
let mut escape_data = 0;
59-
if *escape_args {
57+
if escape_args {
6058
escape_data = 1;
6159
}
6260
set_registry("DefaultShellEscapeArguments", RegistryValueData::DWord(escape_data))?;

sshdconfig/sshd.dsc.resource.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"command": {
2424
"executable": "sshdconfig",
2525
"args": [
26-
"schema"
26+
"schema",
27+
"--as-global"
2728
]
2829
}
2930
}

0 commit comments

Comments
 (0)