Skip to content

Commit 304815c

Browse files
author
Andrew
committed
metaconfig 2
1 parent d889da8 commit 304815c

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

dsc/copy_files.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
settings.dsc.json
2+
default_settings.v1.dsc.json

dsc/default_settings.v1.dsc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"resource_path": {
3+
"allow_env_override": true,
4+
"append_env_path": true,
5+
"directories": []
6+
}
7+
}

dsc/settings.dsc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"resource_path": {
3+
"allow_env_override": true,
4+
"append_env_path": true,
5+
"directories": []
6+
}
7+
}

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl CommandDiscovery {
6666
}
6767
}
6868

69-
return Err(DscError::Operation("Could not read resource_path setting".to_string()));
69+
Err(DscError::Operation("Could not read resource_path setting".to_string()))
7070
}
7171

7272
fn get_resource_paths() -> Result<Vec<PathBuf>, DscError>

dsc_lib/src/util.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ pub fn parse_input_to_json(value: &str) -> Result<String, DscError> {
4444
}
4545
}
4646

47+
/// Will search setting files for the specified setting.
48+
///
49+
/// # Arguments
50+
///
51+
/// * `value_name` - The name of the setting.
52+
///
53+
/// # Errors
54+
///
55+
/// Will return `Err` if could not find requested setting.
4756
pub fn get_setting(value_name: &str) -> Result<serde_json::Value, DscError> {
4857

4958
const SETTINGS_FILE_NAME: &str = "settings.dsc.json";
@@ -57,36 +66,36 @@ pub fn get_setting(value_name: &str) -> Result<serde_json::Value, DscError> {
5766

5867
// First, get setting from the default settings file
5968
settings_file_path = exe_home.join(DEFAULT_SETTINGS_FILE_NAME);
60-
if let Ok(v) = load_value_from_json(&settings_file_path, &value_name) {
69+
if let Ok(v) = load_value_from_json(&settings_file_path, value_name) {
6170
result = v;
62-
debug!("Found setting '{}' in {}", &value_name, settings_file_path.to_string_lossy())
71+
debug!("Found setting '{}' in {}", &value_name, settings_file_path.to_string_lossy());
6372
} else {
64-
debug!("Did not find setting '{}' in {}", &value_name, settings_file_path.to_string_lossy())
73+
debug!("Did not find setting '{}' in {}", &value_name, settings_file_path.to_string_lossy());
6574
}
6675

6776
// Second, get setting from the active settings file overwriting previous value
6877
settings_file_path = exe_home.join(SETTINGS_FILE_NAME);
69-
if let Ok(v) = load_value_from_json(&settings_file_path, &value_name) {
78+
if let Ok(v) = load_value_from_json(&settings_file_path, value_name) {
7079
result = v;
71-
debug!("Found setting '{}' in {}", &value_name, settings_file_path.to_string_lossy())
80+
debug!("Found setting '{}' in {}", &value_name, settings_file_path.to_string_lossy());
7281
} else {
73-
debug!("Did not find setting '{}' in {}", &value_name, settings_file_path.to_string_lossy())
82+
debug!("Did not find setting '{}' in {}", &value_name, settings_file_path.to_string_lossy());
7483
}
7584
} else {
7685
debug!("Can't get dsc executable path");
7786
}
7887

7988
// Third, get setting from the policy settings file overwriting previous value
8089
settings_file_path = PathBuf::from(get_settings_policy_file_path());
81-
if let Ok(v) = load_value_from_json(&settings_file_path, &value_name) {
90+
if let Ok(v) = load_value_from_json(&settings_file_path, value_name) {
8291
result = v;
83-
debug!("Found setting '{}' in {}", &value_name, settings_file_path.to_string_lossy())
92+
debug!("Found setting '{}' in {}", &value_name, settings_file_path.to_string_lossy());
8493
} else {
85-
debug!("Did not find setting '{}' in {}", &value_name, settings_file_path.to_string_lossy())
94+
debug!("Did not find setting '{}' in {}", &value_name, settings_file_path.to_string_lossy());
8695
}
8796

8897
if result == serde_json::Value::Null {
89-
return Err(DscError::NotSupported(format!("Could not find '{}' in settings", value_name).to_string()));
98+
return Err(DscError::NotSupported(format!("Could not find '{value_name}' in settings").to_string()));
9099
}
91100

92101
Ok(result)
@@ -96,15 +105,17 @@ pub fn load_value_from_json(path: &PathBuf, value_name: &str) -> Result<serde_js
96105
let file = File::open(path)?;
97106
let reader = BufReader::new(file);
98107
let root: serde_json::Value = match serde_json::from_reader(reader) {
99-
Ok(j) => j,
100-
Err(err) => {
101-
return Err(DscError::Json(err));
102-
}
103-
};
108+
Ok(j) => j,
109+
Err(err) => {
110+
return Err(DscError::Json(err));
111+
}
112+
};
104113

105-
for (key, value) in root.as_object().unwrap() {
106-
if *key == value_name {
107-
return Ok(value.clone())
114+
if let Some(r) = root.as_object() {
115+
for (key, value) in r {
116+
if *key == value_name {
117+
return Ok(value.clone())
118+
}
108119
}
109120
}
110121

0 commit comments

Comments
 (0)