MCPcopy Index your code
hub / github.com/PowerShell/DSC / get_setting

Function get_setting

lib/dsc-lib/src/util.rs:112–160  ·  view source on GitHub ↗

Will search setting files for the specified setting. Performance implication: Use this function economically as every call opens/reads several config files. TODO: cache the config # Arguments `value_name` - The name of the setting. # Errors Will return `Err` if could not find requested setting.

(value_name: &str)

Source from the content-addressed store, hash-verified

110///
111/// Will return `Err` if could not find requested setting.
112pub fn get_setting(value_name: &str) -> Result<DscSettingValue, DscError> {
113
114 const SETTINGS_FILE_NAME: &str = "dsc.settings.json";
115 // Note that default settings file has root nodes as settings schema version that is specific to this version of dsc
116 const DEFAULT_SETTINGS_FILE_NAME: &str = "dsc_default.settings.json";
117 const DEFAULT_SETTINGS_SCHEMA_VERSION: &str = "1";
118
119 let mut result: DscSettingValue = DscSettingValue::default();
120 let mut settings_file_path : PathBuf;
121
122 if let Some(exe_home) = get_exe_path()?.parent() {
123 // First, get setting from the default settings file
124 settings_file_path = exe_home.join(DEFAULT_SETTINGS_FILE_NAME);
125 if let Ok(v) = load_value_from_json(&settings_file_path, DEFAULT_SETTINGS_SCHEMA_VERSION) {
126 if let Some(n) = v.get(value_name) {
127 result.setting = n.clone();
128 debug!("{}", t!("util.foundSetting", name = value_name, path = settings_file_path.to_string_lossy()));
129 }
130 } else {
131 debug!("{}", t!("util.notFoundSetting", name = value_name, path = settings_file_path.to_string_lossy()));
132 }
133
134 // Second, get setting from the active settings file overwriting previous value
135 settings_file_path = exe_home.join(SETTINGS_FILE_NAME);
136 if let Ok(v) = load_value_from_json(&settings_file_path, value_name) {
137 result.setting = v;
138 debug!("{}", t!("util.foundSetting", name = value_name, path = settings_file_path.to_string_lossy()));
139 } else {
140 debug!("{}", t!("util.notFoundSetting", name = value_name, path = settings_file_path.to_string_lossy()));
141 }
142 } else {
143 debug!("{}", t!("util.failedToGetExePath"));
144 }
145
146 // Third, get setting from the policy
147 settings_file_path = PathBuf::from(get_settings_policy_file_path());
148 if let Ok(v) = load_value_from_json(&settings_file_path, value_name) {
149 result.policy = v;
150 debug!("{}", t!("util.foundSetting", name = value_name, path = settings_file_path.to_string_lossy()));
151 } else {
152 debug!("{}", t!("util.notFoundSetting", name = value_name, path = settings_file_path.to_string_lossy()));
153 }
154
155 if (result.setting == serde_json::Value::Null) && (result.policy == serde_json::Value::Null) {
156 return Err(DscError::NotSupported(t!("util.settingNotFound", name = value_name).to_string()));
157 }
158
159 Ok(result)
160}
161
162fn load_value_from_json(path: &PathBuf, value_name: &str) -> Result<serde_json::Value, DscError> {
163 let file = File::open(path)?;

Callers 2

enable_tracingFunction · 0.85

Calls 5

get_exe_pathFunction · 0.85
load_value_from_jsonFunction · 0.85
defaultFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected