diff --git a/Cargo.lock b/Cargo.lock index a28c5aa..b56d9dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2381,7 +2381,7 @@ dependencies = [ [[package]] name = "peach-config" -version = "0.1.21" +version = "0.1.23" dependencies = [ "clap", "env_logger 0.6.2", diff --git a/peach-config/Cargo.toml b/peach-config/Cargo.toml index f3168de..679d4d9 100644 --- a/peach-config/Cargo.toml +++ b/peach-config/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-config" -version = "0.1.21" +version = "0.1.23" authors = ["Andrew Reid ", "Max Fowler "] edition = "2018" description = "Command line tool for installing, updating and configuring PeachCloud" diff --git a/peach-config/src/generate_manifest.rs b/peach-config/src/generate_manifest.rs index 78f0d87..d69b12e 100644 --- a/peach-config/src/generate_manifest.rs +++ b/peach-config/src/generate_manifest.rs @@ -1,40 +1,32 @@ -use regex::Regex; use serde::{Deserialize, Serialize}; use snafu::ResultExt; use std::collections::HashMap; use std::fs; -use crate::constants::HARDWARE_CONFIG_FILE; +use crate::constants::{HARDWARE_CONFIG_FILE, SERVICES}; use crate::error::{FileReadError, FileWriteError, PeachConfigError}; use crate::utils::get_output; use crate::RtcOption; +/// Helper function which returns the version of a package currently installed, +/// as an Ok(String) if found, and as an Err if not found +pub fn get_package_version_number(package: &str) -> Result { + let version = get_output(&["dpkg-query", "--showformat=${Version}", "--show", package])?; + Ok(version) +} + /// Returns a HashMap of all the peach-packages which are currently installed /// mapped to their version number e.g. { "peach-probe": "1.2.0", "peach-network": "1.4.0" } pub fn get_currently_installed_microservices() -> Result, PeachConfigError> { - // gets a list of all packages currently installed with dpkg - let packages = get_output(&["dpkg", "-l"])?; - - // this regex matches packages which contain the word peach in them - // and has two match groups - // 1. the first match group gets the package name - // 2. the second match group gets the version number of the package - let re: Regex = Regex::new(r"\S+\s+(\S*peach\S+)\s+(\S+).*\n").unwrap(); - - // the following iterator, iterates through the captures matched via the regex - // and for each capture, creates a value in the hash map, - // which maps the name of the package, to its version number - // e.g. { "peach-probe": "1.2.0", "peach-network": "1.4.0" } - let peach_packages: HashMap = re - .captures_iter(&packages) - .filter_map(|cap| { - let groups = (cap.get(1), cap.get(2)); - match groups { - (Some(package), Some(version)) => { - Some((package.as_str().to_string(), version.as_str().to_string())) - } - _ => None, + // gets a list of all packages currently installed with dpkg-query + let peach_packages: HashMap = SERVICES + .iter() + .filter_map(|service| { + let version = get_package_version_number(service); + match version { + Ok(v) => Some((service.to_string(), v)), + Err(_) => None, } }) .collect();