Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to DSC_CONFIG_ROOT env var #342

Merged
merged 9 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dsc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ crossterm = { version = "0.27" }
ctrlc = { version = "3.4.0" }
dsc_lib = { path = "../dsc_lib" }
jsonschema = "0.17"
path-absolutize = { version = "3.1.1" }
schemars = { version = "0.8.12" }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
Expand Down
12 changes: 9 additions & 3 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,15 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, stdin:
ConfigSubCommand::Test { document, path, .. } |
ConfigSubCommand::Validate { document, path, .. } |
ConfigSubCommand::Export { document, path, .. } => {
let config_path = path.clone().unwrap_or_default();
set_dscconfigroot(&config_path);
get_input(document, stdin, path)
let mut new_path = path;
let opt_new_path;
if path.is_some()
{
let config_path = path.clone().unwrap_or_default();
opt_new_path = Some(set_dscconfigroot(&config_path));
new_path = &opt_new_path;
}
get_input(document, stdin, new_path)
}
};

Expand Down
44 changes: 36 additions & 8 deletions dsc/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use dsc_lib::{
}
};
use jsonschema::JSONSchema;
use path_absolutize::Absolutize;
use schemars::{schema_for, schema::RootSchema};
use serde_json::Value;
use std::collections::HashMap;
Expand All @@ -37,7 +38,7 @@ use syntect::{
parsing::SyntaxSet,
util::{as_24_bit_terminal_escaped, LinesWithEndings}
};
use tracing::{Level, debug, error, trace};
use tracing::{Level, debug, error, warn, trace};
use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, Layer};
use tracing_indicatif::IndicatifLayer;

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

pub fn set_dscconfigroot(config_path: &str)
/// Sets `DSC_CONFIG_ROOT` env var and makes path absolute.
///
/// # Arguments
///
/// * `config_path` - Full path to the config file
///
/// # Returns
///
/// Absolute full path to the config file.
pub fn set_dscconfigroot(config_path: &str) -> String
{
let path = Path::new(config_path);
let config_root = match path.parent()
{
Some(dir_path) => { dir_path.to_str().unwrap_or_default().to_string()},
_ => String::new()

// make path absolute
let Ok(full_path) = path.absolutize() else {
error!("Error making config path absolute");
exit(EXIT_DSC_ERROR);
};

let Some(config_root_path) = full_path.parent() else {
// this should never happen because path was absolutized
error!("Error reading config path parent");
exit(EXIT_DSC_ERROR);
};

let env_var = "DSC_CONFIG_ROOT";

// warn if env var is already set/used
if env::var(env_var).is_ok() {
warn!("The current value of '{env_var}' env var will be overridden");
}

// Set env var so child processes (of resources) can use it
debug!("Setting 'DSCConfigRoot' env var as '{}'", config_root);
env::set_var("DSCConfigRoot", config_root.clone());
let config_root = config_root_path.to_str().unwrap_or_default();
debug!("Setting '{env_var}' env var as '{}'", config_root);
env::set_var(env_var, config_root);

// return absolutized path
full_path.to_str().unwrap_or_default().to_string()
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TestClassResource
}
else
{
$this.Prop1 = $env:DSCConfigRoot
$this.Prop1 = $env:DSC_CONFIG_ROOT
}
$this.EnumProp = [EnumPropEnumeration]::Expected
return $this
Expand Down
4 changes: 2 additions & 2 deletions powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Describe 'PowerShell adapter resource tests' {
- name: Class-resource Info
type: PSTestModule/TestClassResource
properties:
Name: "[envvar('DSCConfigRoot')]"
Name: "[envvar('DSC_CONFIG_ROOT')]"
"@

$config_path = "$TestDrive/test_config.dsc.yaml"
Expand All @@ -132,7 +132,7 @@ Describe 'PowerShell adapter resource tests' {
- name: Class-resource Info
type: PSTestModule/TestClassResource
properties:
Name: "[envvar('DSCConfigRoot')]"
Name: "[envvar('DSC_CONFIG_ROOT')]"
"@
$out = $yaml | dsc config get
$LASTEXITCODE | Should -Be 0
Expand Down
Loading