Skip to content

Commit b69b024

Browse files
committed
Merge branch 'update-reference-docs' of https://github.com/Gijsreyn/operation-methods into update-reference-docs
2 parents c8e9c51 + d287999 commit b69b024

29 files changed

+453
-278
lines changed

build.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ if ($null -ne $packageType) {
158158
Remove-Item temp:/rustup-init.exe -ErrorAction Ignore
159159
}
160160
}
161+
else {
162+
Write-Verbose -Verbose "Rust found, updating..."
163+
& $rustup update
164+
}
161165

162166
$BuildToolsPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC"
163167

dsc/Cargo.lock

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dsc"
3-
version = "3.1.0-preview.6"
3+
version = "3.1.0-preview.7"
44
edition = "2021"
55

66
[profile.release]

dsc/locales/en-us.toml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ Press any key to close this window"""
5555
[resolve]
5656
processingInclude = "Processing Include input"
5757
invalidInclude = "Failed to deserialize Include input"
58-
failedToReadFile = "Failed to read file"
59-
failedToOpenFile = "Failed to open included file"
60-
invalidFileContent = "Invalid UTF-8 sequence in included file"
61-
invalidFile = "Failed to read the configuration file as YAML or JSON"
58+
failedToReadFile = "Failed to read file '%{path}': %{error}"
59+
failedToOpenFile = "Failed to open included file '%{path}': %{error}"
60+
invalidFileContent = "Invalid UTF-8 sequence in included file '%{path}': %{error}"
61+
invalidFile = "Failed to read the configuration file as YAML or JSON '%{path}': %{error}"
62+
invalidContent = "Invalid content provided, expected JSON or YAML: %{error}"
6263
resolvingParameters = "Resolving parameters from file"
63-
failedParseParametersFile = "Failed to parse parameters file or content to JSON"
64-
couldNotReadParametersFile = "Could not read parameters file"
65-
invalidPath = "Include path must not contain '..'"
64+
failedParseParametersFile = "Failed to parse parameters file '%{path}'or content to JSON: %{error}"
65+
couldNotReadParametersFile = "Could not read parameters file '%{path}': %{error}"
66+
invalidParametersContent = "Invalid parameters content provided, expected JSON or YAML: %{error}"
67+
invalidPath = "Include path '%{path}' must not contain '..'"
6668
failedGetCurrentDirectory = "Failed to get current directory"
6769
noParameters = "No parameters specified"
6870

@@ -71,6 +73,7 @@ implementedAs = "implemented as"
7173
invalidOperationOnAdapter = "Can not perform this operation on the adapter itself"
7274
setInputEmpty = "Desired input is empty"
7375
testInputEmpty = "Expected input is required"
76+
jsonError = "JSON: %{err}"
7477

7578
[subcommand]
7679
actualStateNotObject = "actual_state is not an object"
@@ -106,6 +109,7 @@ tableHeader_capabilities = "Capabilities"
106109
tableHeader_adapter = "RequireAdapter"
107110
tableHeader_description = "Description"
108111
invalidManifest = "Error in manifest for"
112+
jsonArrayNotSupported = "JSON array output format is only supported for `--all'"
109113

110114
[util]
111115
failedToConvertJsonToString = "Failed to convert JSON to string"

dsc/src/args.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ pub enum OutputFormat {
1515
Yaml,
1616
}
1717

18+
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
19+
pub enum GetOutputFormat {
20+
Json,
21+
JsonArray,
22+
PrettyJson,
23+
Yaml,
24+
}
25+
1826
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
1927
pub enum ListOutputFormat {
2028
Json,
@@ -195,7 +203,7 @@ pub enum ResourceSubCommand {
195203
#[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")]
196204
file: Option<String>,
197205
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
198-
output_format: Option<OutputFormat>,
206+
output_format: Option<GetOutputFormat>,
199207
},
200208
#[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)]
201209
Set {

dsc/src/resolve.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,34 +77,34 @@ pub fn get_contents(input: &str) -> Result<(Option<String>, String), String> {
7777
match file.read_to_end(&mut buffer) {
7878
Ok(_) => (),
7979
Err(err) => {
80-
return Err(format!("{} '{include_path:?}': {err}", t!("resolve.failedToReadFile")));
80+
return Err(t!("resolve.failedToReadFile", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
8181
}
8282
}
8383
},
8484
Err(err) => {
85-
return Err(format!("{} '{include_path:?}': {err}", t!("resolve.failedToOpenFile")));
85+
return Err(t!("resolve.failedToOpenFile", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
8686
}
8787
}
8888
// convert the buffer to a string
8989
let include_content = match String::from_utf8(buffer) {
9090
Ok(input) => input,
9191
Err(err) => {
92-
return Err(format!("{} '{include_path:?}': {err}", t!("resolve.invalidFileContent")));
92+
return Err(t!("resolve.invalidFileContent", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
9393
}
9494
};
9595

9696
match parse_input_to_json(&include_content) {
9797
Ok(json) => json,
9898
Err(err) => {
99-
return Err(format!("{} '{include_path:?}': {err}", t!("resolve.invalidFile")));
99+
return Err(t!("resolve.invalidFile", path = include_path.to_string_lossy(), error = err.to_string()).to_string());
100100
}
101101
}
102102
},
103103
IncludeKind::ConfigurationContent(text) => {
104104
match parse_input_to_json(&text) {
105105
Ok(json) => json,
106106
Err(err) => {
107-
return Err(format!("{}: {err}", t!("resolve.invalidFile")));
107+
return Err(t!("resolve.invalidContent", error = err.to_string()).to_string());
108108
}
109109
}
110110
}
@@ -120,21 +120,21 @@ pub fn get_contents(input: &str) -> Result<(Option<String>, String), String> {
120120
let parameters_json = match parse_input_to_json(&parameters) {
121121
Ok(json) => json,
122122
Err(err) => {
123-
return Err(format!("{} '{parameters_file:?}': {err}", t!("resolve.failedParseParametersFile")));
123+
return Err(t!("resolve.failedParseParametersFile", path = parameters_file.to_string_lossy(), error = err.to_string()).to_string());
124124
}
125125
};
126126
Some(parameters_json)
127127
},
128128
Err(err) => {
129-
return Err(format!("{} '{parameters_file:?}': {err}", t!("resolve.couldNotReadParametersFile")));
129+
return Err(t!("resolve.couldNotReadParametersFile", path = parameters_file.to_string_lossy(), error = err.to_string()).to_string());
130130
}
131131
}
132132
},
133133
Some(IncludeParametersKind::ParametersContent(text)) => {
134134
let parameters_json = match parse_input_to_json(&text) {
135135
Ok(json) => json,
136136
Err(err) => {
137-
return Err(format!("{}: {err}", t!("resolve.failedParseParametersFile")));
137+
return Err(t!("resolve.invalidParametersContent", error = err.to_string()).to_string());
138138
}
139139
};
140140
Some(parameters_json)
@@ -154,7 +154,7 @@ fn normalize_path(path: &Path) -> Result<PathBuf, String> {
154154
} else {
155155
// check that no components of the path are '..'
156156
if path.components().any(|c| c == std::path::Component::ParentDir) {
157-
return Err(format!("{}: {path:?}", t!("resolve.invalidPath")));
157+
return Err(t!("resolve.invalidPath", path = path.to_string_lossy()).to_string());
158158
}
159159

160160
// use DSC_CONFIG_ROOT env var as current directory

dsc/src/resource_command.rs

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

4-
use crate::args::OutputFormat;
4+
use crate::args::{GetOutputFormat, OutputFormat};
55
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, write_object};
66
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
77
use dsc_lib::configure::add_resource_export_results_to_configuration;
@@ -47,7 +47,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&O
4747
}
4848
}
4949

50-
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputFormat>) {
50+
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&GetOutputFormat>) {
5151
let input = String::new();
5252
let Some(resource) = get_resource(dsc, resource_type) else {
5353
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
@@ -68,6 +68,18 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
6868
}
6969
};
7070

71+
if format == Some(&GetOutputFormat::JsonArray) {
72+
let json = match serde_json::to_string(&export_result.actual_state) {
73+
Ok(json) => json,
74+
Err(err) => {
75+
error!("{}", t!("resource_command.jsonError", err = err));
76+
exit(EXIT_JSON_ERROR);
77+
}
78+
};
79+
write_object(&json, Some(&OutputFormat::Json), false);
80+
return;
81+
}
82+
7183
let mut include_separator = false;
7284
for instance in export_result.actual_state
7385
{
@@ -78,10 +90,15 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
7890
let json = match serde_json::to_string(&get_result) {
7991
Ok(json) => json,
8092
Err(err) => {
81-
error!("JSON Error: {err}");
93+
error!("{}", t!("resource_command.jsonError", err = err));
8294
exit(EXIT_JSON_ERROR);
8395
}
8496
};
97+
let format = match format {
98+
Some(&GetOutputFormat::PrettyJson) => Some(&OutputFormat::PrettyJson),
99+
Some(&GetOutputFormat::Yaml) => Some(&OutputFormat::Yaml),
100+
_ => Some(&OutputFormat::Json),
101+
};
85102
write_object(&json, format, include_separator);
86103
include_separator = true;
87104
}

dsc/src/subcommand.rs

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

4-
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, ListOutputFormat, OutputFormat, ResourceSubCommand};
4+
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, GetOutputFormat, ListOutputFormat, OutputFormat, ResourceSubCommand};
55
use crate::resolve::{get_contents, Include};
66
use crate::resource_command::{get_resource, self};
77
use crate::tablewriter::Table;
@@ -590,7 +590,17 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
590590
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
591591
else {
592592
let parsed_input = get_input(input.as_ref(), path.as_ref());
593-
resource_command::get(&dsc, resource, &parsed_input, output_format.as_ref());
593+
let format = match output_format {
594+
Some(GetOutputFormat::Json) => Some(OutputFormat::Json),
595+
Some(GetOutputFormat::JsonArray) => {
596+
error!("{}", t!("subcommand.jsonArrayNotSupported"));
597+
exit(EXIT_INVALID_ARGS);
598+
},
599+
Some(GetOutputFormat::PrettyJson) => Some(OutputFormat::PrettyJson),
600+
Some(GetOutputFormat::Yaml) => Some(OutputFormat::Yaml),
601+
None => None,
602+
};
603+
resource_command::get(&dsc, resource, &parsed_input, format.as_ref());
594604
}
595605
},
596606
ResourceSubCommand::Set { resource, input, file: path, output_format } => {

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Describe 'tests for resource discovery' {
9696
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $manifest
9797
$out = dsc resource list 2>&1
9898
write-verbose -verbose ($out | Out-String)
99-
$out | Should -Match 'WARN.*?Validation.*?Invalid manifest.*?version'
99+
$out | Should -Match 'WARN.*?Validation.*?invalid version' -Because ($out | Out-String)
100100
}
101101
finally {
102102
$env:DSC_RESOURCE_PATH = $oldPath
@@ -220,4 +220,35 @@ Describe 'tests for resource discovery' {
220220
$env:DSC_RESOURCE_PATH = $oldPath
221221
}
222222
}
223+
224+
It 'DSC_RESOURCE_PATH should be used for executable lookup' {
225+
$dscTest = Get-Command dscecho -ErrorAction Stop
226+
$target = if ($IsWindows) {
227+
'echoIt.exe'
228+
} else {
229+
'echoIt'
230+
}
231+
Copy-Item -Path "$($dscTest.Source)" -Destination "$testdrive\$target"
232+
$manifest = Get-Content -Raw -Path "$(Split-Path -Path $dscTest.Source -Parent)\echo.dsc.resource.json" | ConvertFrom-Json
233+
$manifest.type = 'Test/MyEcho'
234+
$manifest.get.executable = $target
235+
$manifest.set = $null
236+
$manifest.test = $null
237+
$manifest.schema.command.executable = $target
238+
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value ($manifest | ConvertTo-Json -Depth 10)
239+
240+
$oldPath = $env:DSC_RESOURCE_PATH
241+
try {
242+
$env:DSC_RESOURCE_PATH = $testdrive
243+
$out = dsc resource get -r 'Test/MyEcho' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
244+
$LASTEXITCODE | Should -Be 0
245+
$out.actualState.output | Should -BeExactly 'Custom'
246+
dsc resource get -r 'Microsoft.DSC.Debug/Echo' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
247+
$LASTEXITCODE | Should -Be 7
248+
Get-Content -Raw -Path "$testdrive/error.txt" | Should -Match "ERROR.*?Resource not found"
249+
}
250+
finally {
251+
$env:DSC_RESOURCE_PATH = $oldPath
252+
}
253+
}
223254
}

dsc/tests/dsc_extension_discover.tests.ps1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
Describe 'Discover extension tests' {
55
BeforeAll {
66
$oldPath = $env:PATH
7-
$separator = [System.IO.Path]::PathSeparator
87
$toolPath = Resolve-Path -Path "$PSScriptRoot/../../extensions/test/discover"
9-
$env:PATH = "$toolPath$separator$oldPath"
8+
$env:PATH = "$toolPath" + [System.IO.Path]::PathSeparator + $oldPath
109
}
1110

1211
AfterAll {
@@ -75,10 +74,10 @@ Describe 'Discover extension tests' {
7574
Set-Content -Path "$TestDrive/test.dsc.extension.json" -Value $extension_json
7675
Copy-Item -Path "$toolPath/discover.ps1" -Destination $TestDrive | Out-Null
7776
Copy-Item -Path "$toolPath/resources" -Destination $TestDrive -Recurse | Out-Null
78-
$env:DSC_RESOURCE_PATH = $TestDrive
77+
$env:DSC_RESOURCE_PATH = "$TestDrive" + [System.IO.Path]::PathSeparator + (Split-Path (Get-Command pwsh).Source -Parent)
7978
try {
8079
$out = dsc extension list | ConvertFrom-Json
81-
$out.Count | Should -Be 1
80+
$out.Count | Should -Be 1 -Because ($out | Out-String)
8281
$out.type | Should -Be 'Test/DiscoverRelative'
8382
$out = dsc resource list 2> $TestDrive/error.log
8483
write-verbose -verbose (Get-Content -Path "$TestDrive/error.log" -Raw)

0 commit comments

Comments
 (0)