Skip to content

Commit b6c6983

Browse files
authored
Merge pull request #561 from anmenaga/exitcode_fix
Set a non-zero exit code when a resource not found
2 parents efce322 + f6c5496 commit b6c6983

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

dsc/assertion.dsc.resource.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"3": "JSON Serialization error",
5050
"4": "Invalid input format",
5151
"5": "Resource instance failed schema validation",
52-
"6": "Command cancelled"
52+
"6": "Command cancelled",
53+
"7": "Resource not found"
5354
},
5455
"validate": {
5556
"executable": "dsc",

dsc/group.dsc.resource.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"3": "JSON Serialization error",
4848
"4": "Invalid input format",
4949
"5": "Resource instance failed schema validation",
50-
"6": "Command cancelled"
50+
"6": "Command cancelled",
51+
"7": "Resource not found"
5152
},
5253
"validate": {
5354
"executable": "dsc",

dsc/parallel.dsc.resource.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"3": "JSON Serialization error",
5151
"4": "Invalid input format",
5252
"5": "Resource instance failed schema validation",
53-
"6": "Command cancelled"
53+
"6": "Command cancelled",
54+
"7": "Resource not found"
5455
},
5556
"validate": {
5657
"executable": "dsc",

dsc/src/resource_command.rs

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

44
use crate::args::OutputFormat;
5-
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output};
5+
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, add_type_name_to_json, write_output};
66
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
77
use dsc_lib::configure::add_resource_export_results_to_configuration;
88
use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}};
@@ -18,7 +18,7 @@ use std::process::exit;
1818
pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Option<OutputFormat>) {
1919
let Some(mut resource) = get_resource(dsc, resource_type) else {
2020
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
21-
return
21+
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
2222
};
2323

2424
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
@@ -60,7 +60,7 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option<OutputForm
6060
let mut input = String::new();
6161
let Some(mut resource) = get_resource(dsc, resource_type) else {
6262
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
63-
return
63+
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
6464
};
6565

6666
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
@@ -112,7 +112,7 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
112112

113113
let Some(mut resource) = get_resource(dsc, resource_type) else {
114114
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
115-
return
115+
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
116116
};
117117

118118
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
@@ -158,7 +158,7 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &O
158158

159159
let Some(mut resource) = get_resource(dsc, resource_type) else {
160160
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
161-
return
161+
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
162162
};
163163

164164
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
@@ -199,7 +199,7 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &O
199199
pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
200200
let Some(mut resource) = get_resource(dsc, resource_type) else {
201201
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
202-
return
202+
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
203203
};
204204

205205
debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
@@ -230,7 +230,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
230230
pub fn schema(dsc: &DscManager, resource_type: &str, format: &Option<OutputFormat>) {
231231
let Some(resource) = get_resource(dsc, resource_type) else {
232232
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
233-
return
233+
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
234234
};
235235
if resource.kind == Kind::Adapter {
236236
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
@@ -260,7 +260,7 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option<OutputF
260260
let mut input = String::new();
261261
let Some(dsc_resource) = get_resource(dsc, resource_type) else {
262262
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
263-
return
263+
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
264264
};
265265

266266
if dsc_resource.kind == Kind::Adapter {

dsc/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub const EXIT_JSON_ERROR: i32 = 3;
5050
pub const EXIT_INVALID_INPUT: i32 = 4;
5151
pub const EXIT_VALIDATION_FAILED: i32 = 5;
5252
pub const EXIT_CTRL_C: i32 = 6;
53+
pub const EXIT_DSC_RESOURCE_NOT_FOUND: i32 = 7;
5354

5455
pub const DSC_CONFIG_ROOT: &str = "DSC_CONFIG_ROOT";
5556
pub const DSC_TRACE_LEVEL: &str = "DSC_TRACE_LEVEL";

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,22 @@ Describe 'tests for resource discovery' {
150150
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly "Lookup table found resource 'testclassresource/testclassresource' in adapter 'Microsoft.DSC/PowerShell'"
151151
$env:PSModulePath = $oldPSModulePath
152152
}
153+
154+
It 'Verify non-zero exit code when resource not found' {
155+
156+
$out = dsc resource get -r abc/def
157+
$LASTEXITCODE | Should -Be 7
158+
$out = dsc resource get --all -r abc/def
159+
$LASTEXITCODE | Should -Be 7
160+
$out = 'abc' | dsc resource set -r abc/def
161+
$LASTEXITCODE | Should -Be 7
162+
$out = 'abc' | dsc resource test -r abc/def
163+
$LASTEXITCODE | Should -Be 7
164+
$out = 'abc' | dsc resource delete -r abc/def
165+
$LASTEXITCODE | Should -Be 7
166+
$out = dsc resource export -r abc/def
167+
$LASTEXITCODE | Should -Be 7
168+
$out = dsc resource schema -r abc/def
169+
$LASTEXITCODE | Should -Be 7
170+
}
153171
}

0 commit comments

Comments
 (0)