2022-10-07 18:54:36 +00:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
|
|
|
engine::{Command, EngineState, Stack},
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
|
|
|
|
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
2022-10-07 18:54:36 +00:00
|
|
|
};
|
|
|
|
use winreg::{enums::*, RegKey};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RegistryQuery;
|
|
|
|
|
|
|
|
impl Command for RegistryQuery {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"registry query"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("registry query")
|
2022-12-21 19:20:46 +00:00
|
|
|
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
2022-10-07 18:54:36 +00:00
|
|
|
.switch("hkcr", "query the hkey_classes_root hive", None)
|
|
|
|
.switch("hkcu", "query the hkey_current_user hive", None)
|
|
|
|
.switch("hklm", "query the hkey_local_machine hive", None)
|
|
|
|
.switch("hku", "query the hkey_users hive", None)
|
|
|
|
.switch("hkpd", "query the hkey_performance_data hive", None)
|
|
|
|
.switch("hkpt", "query the hkey_performance_text hive", None)
|
|
|
|
.switch("hkpnls", "query the hkey_performance_nls_text hive", None)
|
|
|
|
.switch("hkcc", "query the hkey_current_config hive", None)
|
|
|
|
.switch("hkdd", "query the hkey_dyn_data hive", None)
|
|
|
|
.switch(
|
|
|
|
"hkculs",
|
|
|
|
"query the hkey_current_user_local_settings hive",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.required("key", SyntaxShape::String, "registry key to query")
|
|
|
|
.optional(
|
|
|
|
"value",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"optionally supply a registry value to query",
|
|
|
|
)
|
|
|
|
.category(Category::System)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Query the Windows registry."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
|
|
"Currently supported only on Windows systems."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
2023-02-05 21:17:46 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-10-07 18:54:36 +00:00
|
|
|
registry_query(engine_state, stack, call)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Query the HKEY_CURRENT_USER hive",
|
|
|
|
example: "registry query --hkcu environment",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Query the HKEY_LOCAL_MACHINE hive",
|
|
|
|
example: r"registry query --hklm 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn registry_query(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
2023-02-05 21:17:46 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-12-24 13:41:57 +00:00
|
|
|
let call_span = call.head;
|
|
|
|
|
2022-10-07 18:54:36 +00:00
|
|
|
let registry_key: Spanned<String> = call.req(engine_state, stack, 0)?;
|
|
|
|
let registry_key_span = ®istry_key.clone().span;
|
|
|
|
let registry_value: Option<Spanned<String>> = call.opt(engine_state, stack, 1)?;
|
|
|
|
|
2023-10-08 21:52:37 +00:00
|
|
|
let reg_hive = get_reg_hive(call)?;
|
|
|
|
let reg_key = reg_hive.open_subkey(registry_key.item)?;
|
2022-10-07 18:54:36 +00:00
|
|
|
|
|
|
|
if registry_value.is_none() {
|
|
|
|
let mut reg_values = vec![];
|
|
|
|
for (name, val) in reg_key.enum_values().flatten() {
|
2022-12-24 13:41:57 +00:00
|
|
|
let (nu_value, reg_type) = reg_value_to_nu_value(val, call_span);
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
reg_values.push(Value::record(
|
|
|
|
record! {
|
|
|
|
"name" => Value::string(name, call_span),
|
|
|
|
"value" => nu_value,
|
|
|
|
"type" => Value::string(format!("{:?}", reg_type), call_span),
|
|
|
|
},
|
|
|
|
*registry_key_span,
|
|
|
|
))
|
2022-10-07 18:54:36 +00:00
|
|
|
}
|
|
|
|
Ok(reg_values.into_pipeline_data(engine_state.ctrlc.clone()))
|
|
|
|
} else {
|
|
|
|
match registry_value {
|
|
|
|
Some(value) => {
|
|
|
|
let reg_value = reg_key.get_raw_value(value.item.as_str());
|
|
|
|
match reg_value {
|
|
|
|
Ok(val) => {
|
2022-12-24 13:41:57 +00:00
|
|
|
let (nu_value, reg_type) = reg_value_to_nu_value(val, call_span);
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
Ok(Value::record(
|
|
|
|
record! {
|
|
|
|
"name" => Value::string(value.item, call_span),
|
|
|
|
"value" => nu_value,
|
|
|
|
"type" => Value::string(format!("{:?}", reg_type), call_span),
|
|
|
|
},
|
|
|
|
value.span,
|
|
|
|
)
|
2022-10-07 18:54:36 +00:00
|
|
|
.into_pipeline_data())
|
|
|
|
}
|
2022-12-07 03:48:39 +00:00
|
|
|
Err(_) => Err(ShellError::GenericError(
|
|
|
|
"Unable to find registry key/value".to_string(),
|
|
|
|
format!("Registry value: {} was not found", value.item),
|
|
|
|
Some(value.span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
)),
|
2022-10-07 18:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-24 13:41:57 +00:00
|
|
|
None => Ok(Value::nothing(call_span).into_pipeline_data()),
|
2022-10-07 18:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-08 21:52:37 +00:00
|
|
|
fn get_reg_hive(call: &Call) -> Result<RegKey, ShellError> {
|
|
|
|
let flags: Vec<_> = [
|
|
|
|
"hkcr", "hkcu", "hklm", "hku", "hkpd", "hkpt", "hkpnls", "hkcc", "hkdd", "hkculs",
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.filter(|flag| call.has_flag(flag))
|
|
|
|
.collect();
|
|
|
|
if flags.len() > 1 {
|
2022-10-07 18:54:36 +00:00
|
|
|
return Err(ShellError::GenericError(
|
|
|
|
"Only one registry key can be specified".into(),
|
|
|
|
"Only one registry key can be specified".into(),
|
2023-10-08 21:52:37 +00:00
|
|
|
Some(call.head),
|
2022-10-07 18:54:36 +00:00
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
));
|
|
|
|
}
|
2023-10-09 08:19:20 +00:00
|
|
|
let hive = flags.first().copied().unwrap_or("hkcu");
|
2023-10-08 21:52:37 +00:00
|
|
|
let hkey = match hive {
|
|
|
|
"hkcr" => HKEY_CLASSES_ROOT,
|
|
|
|
"hkcu" => HKEY_CURRENT_USER,
|
|
|
|
"hklm" => HKEY_LOCAL_MACHINE,
|
|
|
|
"hku" => HKEY_USERS,
|
|
|
|
"hkpd" => HKEY_PERFORMANCE_DATA,
|
|
|
|
"hkpt" => HKEY_PERFORMANCE_TEXT,
|
|
|
|
"hkpnls" => HKEY_PERFORMANCE_NLSTEXT,
|
|
|
|
"hkcc" => HKEY_CURRENT_CONFIG,
|
|
|
|
"hkdd" => HKEY_DYN_DATA,
|
|
|
|
"hkculs" => HKEY_CURRENT_USER_LOCAL_SETTINGS,
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::NushellFailedSpanned {
|
|
|
|
msg: "Entered unreachable code".into(),
|
|
|
|
label: "Unknown registry hive".into(),
|
|
|
|
span: call.head,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(RegKey::predef(hkey))
|
2022-10-07 18:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reg_value_to_nu_value(
|
|
|
|
reg_value: winreg::RegValue,
|
2022-12-24 13:41:57 +00:00
|
|
|
call_span: Span,
|
2022-10-07 18:54:36 +00:00
|
|
|
) -> (nu_protocol::Value, winreg::enums::RegType) {
|
|
|
|
match reg_value.vtype {
|
2022-12-24 13:41:57 +00:00
|
|
|
REG_NONE => (Value::nothing(call_span), reg_value.vtype),
|
2022-10-07 18:54:36 +00:00
|
|
|
REG_SZ => (
|
2023-10-21 23:50:34 +00:00
|
|
|
Value::string(reg_value.to_string(), call_span),
|
2022-10-07 18:54:36 +00:00
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_EXPAND_SZ => (
|
2023-10-21 23:50:34 +00:00
|
|
|
Value::string(reg_value.to_string(), call_span),
|
2022-10-07 18:54:36 +00:00
|
|
|
reg_value.vtype,
|
|
|
|
),
|
2022-12-24 13:41:57 +00:00
|
|
|
REG_BINARY => (Value::binary(reg_value.bytes, call_span), reg_value.vtype),
|
2022-10-07 18:54:36 +00:00
|
|
|
REG_DWORD => (
|
|
|
|
Value::int(
|
|
|
|
unsafe { *(reg_value.bytes.as_ptr() as *const u32) } as i64,
|
2022-12-24 13:41:57 +00:00
|
|
|
call_span,
|
2022-10-07 18:54:36 +00:00
|
|
|
),
|
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_DWORD_BIG_ENDIAN => (
|
|
|
|
Value::int(
|
|
|
|
unsafe { *(reg_value.bytes.as_ptr() as *const u32) } as i64,
|
2022-12-24 13:41:57 +00:00
|
|
|
call_span,
|
2022-10-07 18:54:36 +00:00
|
|
|
),
|
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_LINK => (
|
2023-10-21 23:50:34 +00:00
|
|
|
Value::string(reg_value.to_string(), call_span),
|
2022-10-07 18:54:36 +00:00
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_MULTI_SZ => (
|
2023-10-21 23:50:34 +00:00
|
|
|
Value::string(reg_value.to_string(), call_span),
|
2022-10-07 18:54:36 +00:00
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_RESOURCE_LIST => (
|
2023-10-21 23:50:34 +00:00
|
|
|
Value::string(reg_value.to_string(), call_span),
|
2022-10-07 18:54:36 +00:00
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_FULL_RESOURCE_DESCRIPTOR => (
|
2023-10-21 23:50:34 +00:00
|
|
|
Value::string(reg_value.to_string(), call_span),
|
2022-10-07 18:54:36 +00:00
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_RESOURCE_REQUIREMENTS_LIST => (
|
2023-10-21 23:50:34 +00:00
|
|
|
Value::string(reg_value.to_string(), call_span),
|
2022-10-07 18:54:36 +00:00
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
REG_QWORD => (
|
|
|
|
Value::int(
|
|
|
|
unsafe { *(reg_value.bytes.as_ptr() as *const u32) } as i64,
|
2022-12-24 13:41:57 +00:00
|
|
|
call_span,
|
2022-10-07 18:54:36 +00:00
|
|
|
),
|
|
|
|
reg_value.vtype,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|