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 5 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
14 changes: 10 additions & 4 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::args::{ConfigSubCommand, DscType, OutputFormat, ResourceSubCommand};
use crate::resource_command::{get_resource, self};
use crate::Stream;
use crate::tablewriter::Table;
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_INPUT, EXIT_JSON_ERROR, EXIT_VALIDATION_FAILED, get_schema, write_output, get_input, set_dscconfigroot, validate_json};
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_INPUT, EXIT_JSON_ERROR, EXIT_VALIDATION_FAILED, get_schema, write_output, get_input, absolutize_and_set_dscconfigroot, validate_json};
use dsc_lib::configure::{Configurator, ErrorAction, config_result::ResourceGetResult};
use dsc_lib::dscerror::DscError;
use dsc_lib::dscresources::invoke_result::{
Expand Down 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(absolutize_and_set_dscconfigroot(&config_path));
new_path = &opt_new_path;
}
anmenaga marked this conversation as resolved.
Show resolved Hide resolved
get_input(document, stdin, new_path)
}
};

Expand Down
31 changes: 23 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};

pub const EXIT_SUCCESS: i32 = 0;
Expand Down Expand Up @@ -406,16 +407,30 @@ 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)
pub fn absolutize_and_set_dscconfigroot(config_path: &str) -> String
anmenaga marked this conversation as resolved.
Show resolved Hide resolved
{
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 full_path = path.absolutize().unwrap_or_default();
anmenaga marked this conversation as resolved.
Show resolved Hide resolved
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