From a347e4726d7ed00ea081c0243524b262dea57e0a Mon Sep 17 00:00:00 2001 From: notplants Date: Wed, 20 Apr 2022 12:07:32 -0400 Subject: [PATCH] Fix manifest --- Cargo.lock | 2 +- peach-config/Cargo.toml | 2 +- peach-config/src/generate_manifest.rs | 47 ++++++++++++--------------- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a28c5aa..821964a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2381,7 +2381,7 @@ dependencies = [ [[package]] name = "peach-config" -version = "0.1.21" +version = "0.1.22" dependencies = [ "clap", "env_logger 0.6.2", diff --git a/peach-config/Cargo.toml b/peach-config/Cargo.toml index f3168de..093e431 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.22" 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..d755ee2 100644 --- a/peach-config/src/generate_manifest.rs +++ b/peach-config/src/generate_manifest.rs @@ -1,43 +1,38 @@ -use regex::Regex; use serde::{Deserialize, Serialize}; use snafu::ResultExt; use std::collections::HashMap; use std::fs; +use log::debug; -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; + +pub fn get_package_version_number(package: &str) -> Result { + let version = get_output(&["dpkg-query", "--showformat='${Version}'", "--show", package])?; + debug!("version: {:?}", version); + 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_option_vec: Vec> = SERVICES.iter().map(|service| { + let version = get_package_version_number(service); + match version { + Ok(v) => { + Some((service.to_string(), v.to_string())) } - }) - .collect(); + Err(_) => { + None + } + } + }).collect(); + let peach_packages: HashMap = peach_packages_option_vec.into_iter().flat_map(|e| e).collect(); // finally the hashmap of packages and version numbers is returned Ok(peach_packages)