Skip to content

Commit bd47bff

Browse files
authored
Merge pull request #342 from anmenaga/fix_335
Updates to DSC_CONFIG_ROOT env var
2 parents ef6b663 + 311e4cb commit bd47bff

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

dsc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ crossterm = { version = "0.27" }
1818
ctrlc = { version = "3.4.0" }
1919
dsc_lib = { path = "../dsc_lib" }
2020
jsonschema = "0.17"
21+
path-absolutize = { version = "3.1.1" }
2122
schemars = { version = "0.8.12" }
2223
serde = { version = "1.0", features = ["derive"] }
2324
serde_json = { version = "1.0", features = ["preserve_order"] }

dsc/src/subcommand.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,15 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, stdin:
205205
ConfigSubCommand::Test { document, path, .. } |
206206
ConfigSubCommand::Validate { document, path, .. } |
207207
ConfigSubCommand::Export { document, path, .. } => {
208-
let config_path = path.clone().unwrap_or_default();
209-
set_dscconfigroot(&config_path);
210-
get_input(document, stdin, path)
208+
let mut new_path = path;
209+
let opt_new_path;
210+
if path.is_some()
211+
{
212+
let config_path = path.clone().unwrap_or_default();
213+
opt_new_path = Some(set_dscconfigroot(&config_path));
214+
new_path = &opt_new_path;
215+
}
216+
get_input(document, stdin, new_path)
211217
}
212218
};
213219

dsc/src/util.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use dsc_lib::{
2525
}
2626
};
2727
use jsonschema::JSONSchema;
28+
use path_absolutize::Absolutize;
2829
use schemars::{schema_for, schema::RootSchema};
2930
use serde_json::Value;
3031
use std::collections::HashMap;
@@ -37,7 +38,7 @@ use syntect::{
3738
parsing::SyntaxSet,
3839
util::{as_24_bit_terminal_escaped, LinesWithEndings}
3940
};
40-
use tracing::{Level, debug, error, trace};
41+
use tracing::{Level, debug, error, warn, trace};
4142
use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, Layer};
4243
use tracing_indicatif::IndicatifLayer;
4344

@@ -408,16 +409,43 @@ pub fn get_input(input: &Option<String>, stdin: &Option<String>, path: &Option<S
408409
parse_input_to_json(&value)
409410
}
410411

411-
pub fn set_dscconfigroot(config_path: &str)
412+
/// Sets `DSC_CONFIG_ROOT` env var and makes path absolute.
413+
///
414+
/// # Arguments
415+
///
416+
/// * `config_path` - Full path to the config file
417+
///
418+
/// # Returns
419+
///
420+
/// Absolute full path to the config file.
421+
pub fn set_dscconfigroot(config_path: &str) -> String
412422
{
413423
let path = Path::new(config_path);
414-
let config_root = match path.parent()
415-
{
416-
Some(dir_path) => { dir_path.to_str().unwrap_or_default().to_string()},
417-
_ => String::new()
424+
425+
// make path absolute
426+
let Ok(full_path) = path.absolutize() else {
427+
error!("Error making config path absolute");
428+
exit(EXIT_DSC_ERROR);
429+
};
430+
431+
let Some(config_root_path) = full_path.parent() else {
432+
// this should never happen because path was absolutized
433+
error!("Error reading config path parent");
434+
exit(EXIT_DSC_ERROR);
418435
};
419436

437+
let env_var = "DSC_CONFIG_ROOT";
438+
439+
// warn if env var is already set/used
440+
if env::var(env_var).is_ok() {
441+
warn!("The current value of '{env_var}' env var will be overridden");
442+
}
443+
420444
// Set env var so child processes (of resources) can use it
421-
debug!("Setting 'DSCConfigRoot' env var as '{}'", config_root);
422-
env::set_var("DSCConfigRoot", config_root.clone());
445+
let config_root = config_root_path.to_str().unwrap_or_default();
446+
debug!("Setting '{env_var}' env var as '{}'", config_root);
447+
env::set_var(env_var, config_root);
448+
449+
// return absolutized path
450+
full_path.to_str().unwrap_or_default().to_string()
423451
}

powershell-adapter/Tests/PSTestModule/TestClassResource.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TestClassResource
4141
}
4242
else
4343
{
44-
$this.Prop1 = $env:DSCConfigRoot
44+
$this.Prop1 = $env:DSC_CONFIG_ROOT
4545
}
4646
$this.EnumProp = [EnumPropEnumeration]::Expected
4747
return $this

powershell-adapter/Tests/powershellgroup.config.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Describe 'PowerShell adapter resource tests' {
107107
- name: Class-resource Info
108108
type: PSTestModule/TestClassResource
109109
properties:
110-
Name: "[envvar('DSCConfigRoot')]"
110+
Name: "[envvar('DSC_CONFIG_ROOT')]"
111111
"@
112112

113113
$config_path = "$TestDrive/test_config.dsc.yaml"
@@ -132,7 +132,7 @@ Describe 'PowerShell adapter resource tests' {
132132
- name: Class-resource Info
133133
type: PSTestModule/TestClassResource
134134
properties:
135-
Name: "[envvar('DSCConfigRoot')]"
135+
Name: "[envvar('DSC_CONFIG_ROOT')]"
136136
"@
137137
$out = $yaml | dsc config get
138138
$LASTEXITCODE | Should -Be 0

0 commit comments

Comments
 (0)