Skip to content

Commit 812a65a

Browse files
committed
add better error if path is accidentally passed to -d
1 parent e3918ca commit 812a65a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

dsc/src/util.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,17 @@ pub fn parse_input_to_json(value: &str) -> String {
367367
pub fn get_input(input: &Option<String>, stdin: &Option<String>, path: &Option<String>) -> String {
368368
let value = match (input, stdin, path) {
369369
(Some(_), Some(_), None) | (None, Some(_), Some(_)) => {
370-
error!("Error: Cannot specify both stdin and --input or --path");
370+
error!("Error: Cannot specify both stdin and --document or --path");
371371
exit(EXIT_INVALID_ARGS);
372372
},
373373
(Some(input), None, None) => {
374374
debug!("Reading input from command line parameter");
375+
376+
// see if user accidentally passed in a file path
377+
if Path::new(input).exists() {
378+
error!("Error: Document provided is a file path, use --path instead");
379+
exit(EXIT_INVALID_INPUT);
380+
}
375381
input.clone()
376382
},
377383
(None, Some(stdin), None) => {
@@ -413,7 +419,7 @@ pub fn get_input(input: &Option<String>, stdin: &Option<String>, path: &Option<S
413419
///
414420
/// # Arguments
415421
///
416-
/// * `config_path` - Full path to the config file
422+
/// * `config_path` - Full path to the config file
417423
///
418424
/// # Returns
419425
///
@@ -428,14 +434,14 @@ pub fn set_dscconfigroot(config_path: &str) -> String
428434
exit(EXIT_DSC_ERROR);
429435
};
430436

431-
let Some(config_root_path) = full_path.parent() else {
437+
let Some(config_root_path) = full_path.parent() else {
432438
// this should never happen because path was absolutized
433439
error!("Error reading config path parent");
434440
exit(EXIT_DSC_ERROR);
435441
};
436442

437443
let env_var = "DSC_CONFIG_ROOT";
438-
444+
439445
// warn if env var is already set/used
440446
if env::var(env_var).is_ok() {
441447
warn!("The current value of '{env_var}' env var will be overridden");

dsc/tests/dsc_args.tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,10 @@ resources:
271271
$r.requireAdapter.StartsWith("Test") | Should -Be $true
272272
$r.kind | Should -Be "Resource"
273273
}
274+
275+
It 'passing filepath to document arg should error' {
276+
$configFile = Resolve-Path $PSScriptRoot/../examples/osinfo.dsc.json
277+
$stderr = dsc config get -d $configFile 2>&1
278+
$stderr | Should -Match '.*?--path.*?'
279+
}
274280
}

0 commit comments

Comments
 (0)