Skip to content

Commit

Permalink
修复node写入失败导致程序终止的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xml committed Apr 29, 2024
1 parent 44f52fb commit 868ca72
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Config {
match &self.value {
Some(val) => {
let raw_val = (self.handler)(&val);
crate::utils::write_line(&self.node, &raw_val).unwrap();
crate::utils::write_line(&self.node, &raw_val)?;
Ok(())
},
None => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "val is none")),
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,19 @@ fn main() -> std::io::Result<()> {
for config in &configs {
for local_config in &local_configs {
if config.node == local_config.node && local_config.value != None && config.value != local_config.value && local_config.writeable() {
need_change.push(config.clone());
let mut config_new = local_config.clone();
config_new.value = config.value.clone();
need_change.push(config_new);
}
}
}

for config in need_change {
if let Some(val) = &config.value {
println!("apply: {} = {}", config.node, val);
config.apply().expect(&format!("ERROR: apply {} failed", config.node))
if let Err(e) = config.apply() {
println!("ERROR: apply {} failed {}", config.node, e)
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub fn read_line(path: &str) -> std::io::Result<String> {

pub fn write_line(path: &str, val: &str) -> std::io::Result<()> {
let mut input = File::create(path)?;
input.write_all(val.as_bytes())
let val_line = format!("{}\n", val);
input.write_all(val_line.as_bytes())
}

pub fn read_all_line(path: &str) -> std::io::Result<String> {
Expand Down

0 comments on commit 868ca72

Please sign in to comment.