Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add schema subcommand to get JSON schema #20

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ntreg/src/registry_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl Iterator for RegistrySubkeys {
)
};

if status == ERROR_NO_MORE_ITEMS as i32 {
if status == ERROR_NO_MORE_ITEMS {
return None;
}

Expand Down
1 change: 1 addition & 0 deletions registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ clap = { version = "3.2", features = ["derive"] }
crossterm = { version = "0.24.0" }
ntreg = { path = "../ntreg" }
ntstatuserror = { path = "../ntstatuserror" }
schemars = { version = "0.8.12" }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }

Expand Down
11 changes: 8 additions & 3 deletions registry/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ pub enum SubCommand {
#[clap(short, long, required = true, help = "The string to find.")]
find: String,
#[clap(short, long, help = "Recursively find.")]
recurse: Option<bool>,
recurse: bool,
#[clap(short, long, help = "Only find keys.")]
keys_only: Option<bool>,
keys_only: bool,
#[clap(short, long, help = "Only find values.")]
values_only: Option<bool>,
values_only: bool,
},
#[clap(name = "config", about = "Manage registry configuration.", arg_required_else_help = true)]
Config {
#[clap(subcommand)]
subcommand: ConfigSubCommand,
},
#[clap(name = "schema", about = "Retrieve JSON sceham.")]
SteveL-MSFT marked this conversation as resolved.
Show resolved Hide resolved
SteveL-MSFT marked this conversation as resolved.
Show resolved Hide resolved
Schema {
#[clap(short, long, help = "Pretty print JSON.")]
pretty: bool,
}
}
9 changes: 6 additions & 3 deletions registry/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum EnsureKind {
Present,
Absent,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum RegistryValueData {
String(String),
ExpandString(String),
Expand All @@ -16,9 +17,11 @@ pub enum RegistryValueData {
QWord(u64),
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename = "Registry")]
pub struct RegistryConfig {
#[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(rename = "keyPath")]
pub key_path: String,
#[serde(rename = "valueName")]
Expand Down
13 changes: 12 additions & 1 deletion registry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::env;
use args::Arguments;
use atty::Stream;
use clap::Parser;
use schemars::schema_for;
use std::{io::{self, Read}, process::exit};

use crate::config::RegistryConfig;
Expand Down Expand Up @@ -149,7 +150,17 @@ fn main() {
if !in_desired_state {
exit(EXIT_NOT_IN_DESIRED_STATE);
}
}
},
args::SubCommand::Schema { pretty } => {
let schema = schema_for!(RegistryConfig);
let json = if pretty {
serde_json::to_string_pretty(&schema).unwrap()
}
else {
serde_json::to_string(&schema).unwrap()
};
println!("{}", json);
},
}

exit(EXIT_SUCCESS);
Expand Down
3 changes: 3 additions & 0 deletions registry/src/regconfighelper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use ntreg::{registry_key::RegistryKey, registry_value::RegistryValueData as NtRe
use ntstatuserror::{NtStatusError, NtStatusErrorKind};
use std::fmt::{Display, Formatter};

const ID: &str = "https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json";

#[derive(Debug, PartialEq, Eq)]
pub enum RegistryError {
NtStatus(NtStatusError),
Expand Down Expand Up @@ -35,6 +37,7 @@ pub fn config_get(config: &RegistryConfig) -> Result<String, RegistryError> {
};

let mut reg_result = RegistryConfig {
id: Some(ID.to_string()),
key_path: config.key_path.clone(),
value_name: None,
value_data: None,
Expand Down
4 changes: 2 additions & 2 deletions registry/tests/registry.config.get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Describe 'Registry config get tests' {
$LASTEXITCODE | Should -Be 0
$result = $out | ConvertFrom-Json
$result.keyPath | Should -Be 'HKLM\Software\Microsoft\Windows\CurrentVersion'
($result.psobject.properties | Measure-Object).Count | Should -Be 1
($result.psobject.properties | Measure-Object).Count | Should -Be 2
}

It 'Can get a registry value' -Skip:(!$IsWindows) {
Expand All @@ -25,6 +25,6 @@ Describe 'Registry config get tests' {
$result.keyPath | Should -Be 'HKLM\Software\Microsoft\Windows\CurrentVersion'
$result.valueName | Should -Be 'ProgramFilesPath'
$result.valueData.ExpandString | Should -Be '%ProgramFiles%'
($result.psobject.properties | Measure-Object).Count | Should -Be 3
($result.psobject.properties | Measure-Object).Count | Should -Be 4
}
}
2 changes: 1 addition & 1 deletion registry/tests/registry.config.set.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Describe 'registry config set tests' {
$result.keyPath | Should -Be 'HKCU\1\2\3'
$result.valueName | Should -Be 'Hello'
$result.valueData.String | Should -Be 'World'
($result.psobject.properties | Measure-Object).Count | Should -Be 3
($result.psobject.properties | Measure-Object).Count | Should -Be 4
}

It 'Can set a key to be absent' -Skip:(!$IsWindows) {
Expand Down