Add light abstraction
This is to simplify code and eliminate repetition
@@ -2,56 +2,50 @@ use super::{KernelInterface, KernelInterfaceError};
use failure::Error;
+
impl KernelInterface {
- //Sets an arbitrary UCI variable on OpenWRT
- pub fn set_uci_var(&self, key: &str, value: &str) -> Result<bool, Error> {
- let output = self.run_command("uci", &["set", &format!("{}={}", key, value)])?;
+
+ fn run_uci(&self, command: &str, args: &[&str]) -> Result<(), Error>{
+ let output = self.run_command(command, args)?;
if !output.stderr.is_empty() {
return Err(KernelInterfaceError::RuntimeError(format!(
"recieved error while setting UCI: {}",
String::from_utf8(output.stderr)?
)).into());
}
+ Ok(())
+
+}
+
+ //Sets an arbitrary UCI variable on OpenWRT
+ pub fn set_uci_var(&self, key: &str, value: &str) -> Result<bool, Error> {
+ self.run_uci("uci", &["set", &format!("{}={}", key, value)])?;
Ok(true)
+
}
//Adds an arbitrary UCI variable on OpenWRT
pub fn add_uci_var(&self, key: &str, value: &str) -> Result<bool, Error> {
- let output = self.run_command("uci", &["add", key, value])?;
- if !output.stderr.is_empty() {
- return Err(KernelInterfaceError::RuntimeError(format!(
- "recieved error while setting UCI: {}",
- String::from_utf8(output.stderr)?
- )).into());
- }
+ self.run_uci("uci", &["add", key, value])?;
Ok(true)
+
}
//Sets an arbitrary UCI list on OpenWRT
pub fn set_uci_list(&self, key: &str, value: &[&str]) -> Result<bool, Error> {
self.del_uci_var(&key);
for v in value {
- let output = self.run_command("uci", &["add_list", &format!("{}={}", &key, &v)])?;
- if !output.stderr.is_empty() {
- return Err(KernelInterfaceError::RuntimeError(format!(
- "recieved error while setting UCI: {}",
- String::from_utf8(output.stderr)?
- )).into());
- }
+ self.run_uci("uci", &["add_list", &format!("{}={}", &key, &v)])?;
+
}
Ok(true)
}
//Deletes an arbitrary UCI variable on OpenWRT
pub fn del_uci_var(&self, key: &str) -> Result<bool, Error> {
- let output = self.run_command("uci", &["delete", &key])?;
- if !output.stderr.is_empty() {
- return Err(KernelInterfaceError::RuntimeError(format!(
- "recieved error while setting UCI: {}",
- String::from_utf8(output.stderr)?
- )).into());
- }
+ self.run_uci("uci", &["delete", &key])?;
Ok(true)
+
}
//Retrieves the value of a given UCI path, could be one or multiple values
