Skip to content

Commit

Permalink
fix config parsing error when no "os" key is set
Browse files Browse the repository at this point in the history
  • Loading branch information
timopruesse committed Jun 24, 2022
1 parent 89647ed commit c536660
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "machine_setup"
version = "0.7.0"
version = "0.7.1"
edition = "2021"
authors = ["Timo Prüße <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/config/json_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn parse_json(path: &Path) -> Result<TaskList, String> {
}
let commands = commands.unwrap();

let os_list = get_os_list(&values["os"]);
let os_list = get_os_list(values.get("os").unwrap_or(&Value::Null));
if let Err(os_err) = os_list {
return Err(os_err);
}
Expand Down
31 changes: 20 additions & 11 deletions src/config/yaml_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,22 @@ fn convert_to_config_value(yaml: &Yaml) -> ConfigValue {
}

fn get_os_list(value: &Yaml) -> Result<Vec<Os>, String> {
if value.is_null() {
return Ok(vec![]);
if let Some(str_value) = value.as_str() {
return Ok(vec![Os::from_str(str_value).unwrap()]);
}

if value.is_array() {
return Ok(value
.as_vec()
.unwrap()
if let Some(arr_value) = value.as_vec() {
return Ok(arr_value
.iter()
.map(|os| Os::from_str(os.as_str().unwrap()).unwrap())
.collect());
}

let str_value = value.as_str();
if str_value.is_none() {
return Err(format!("{:?} is in the wrong format", value));
if value.is_null() {
return Ok(vec![]);
}

Ok(vec![Os::from_str(str_value.unwrap()).unwrap()])
Err(format!("os: {:?}", value))
}

fn get_commands(value: &Yaml) -> Result<Vec<Command>, String> {
Expand Down Expand Up @@ -126,7 +123,19 @@ fn parse_yaml(path: &Path) -> Result<TaskList, String> {
}
let commands = commands.unwrap();

let os_list = get_os_list(&value["os"]);
let map = value.as_hash();
if map.is_none() {
return Err(format!(
"{}: task entries are not an object",
key.as_str().unwrap()
));
}
let map = map.unwrap();

let os_list = get_os_list(
map.get(&Yaml::String(String::from("os")))
.unwrap_or(&Yaml::Null),
);
if let Err(os_err) = os_list {
return Err(os_err);
}
Expand Down

0 comments on commit c536660

Please sign in to comment.